我们发现,当 时,有如下结论:
- 若 ,则 WSH 胜
- 若 ,则 LQX 胜
对于第一条结论
- LQX 最小写 4,那么 WSH 胜。
对于第二条结论
- 若 为合数,LQX 写 ,然后 WSH 最少写 ,LQX 胜。
- 若 为质数,则 必为合数,同理 LQX 胜。
当 时,设 WSH 写下了
- 若 ,即 时,同上,WSH 胜。
- 若 ,同上,LQX 胜。
我们现在只需要判断 中是否有质数即可。利用 Miller-Rabin 算法可以高效判断。
时间复杂度
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read() {
ll x = 0, w = 1; char ch = getchar();
while (ch < '0' || '9' < ch) { if (ch == '-') w = -w; ch = getchar(); }
while ('0' <= ch && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); }
return x * w;
}
inline void write(int x) {
if (x < 0) { putchar('-'); write(-x); return ; }
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
ll n;
int k;
const ll base[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
ll fmul(ll x, ll y, ll mod) {
ll res = 0;
x %= mod;
while (y) {
if (y & 1)
res = (res + x) % mod;
x = (x << 1) % mod;
y >>= 1;
}
return res;
}
ll fpow(ll x, ll y, ll mod) {
ll res = 1;
x %= mod;
while (y) {
if (y & 1)
res = fmul(res, x, mod);
x = fmul(x, x, mod);
y >>= 1;
}
return res;
}
bool f(ll n, ll a) {
ll d = n - 1;
int s = 0;
while (!(d & 1)) {
s++;
d >>= 1;
}
ll x = fpow(a, d, n);
if (x == 1 || x == n - 1)
return true;
for (int r = 1; r < s; r++) {
x = fmul(x, x, n);
if (x == n - 1)
return true;
if (x == 1)
return false;
}
return false;
}
bool isp(ll n) {
if (n < 2)
return false;
if (n == 2 || n == 3)
return true;
if (!(n % 2) || !(n % 3))
return false;
for (int i = 0; i < 7; i++) {
if (base[i] >= n)
continue;
if (!f(n, base[i]))
return false;
}
return true;
}
int T;
int main() {
T = read();
while (T--) {
n = read(), k = read();
if (k == 1) {
if (n <= 3)
puts("WSH");
else
puts("LQX");
} else {
if (isp(n) || isp(n - 1) || isp(n - 2) || isp(n - 3))
puts("WSH");
else
puts("LQX");
}
}
return 0;
}北京人大附中 万书豪&李洽昕 99