-
题意:有 n 堆石子,两个玩家轮流操作,每次操作只能去掉一堆石子或者在一堆有x个石子的石堆中移除y个石子,并且满足 gcd(x, y) = 1。直到一个玩家无法操作,则另一个玩家胜利。问先手赢还是后手赢。
-
显然符合NIM游戏,只要能算出sg函数值即可判断。算出每个石堆的SG函数,然后求其异或和。如果结果为零则后手赢,否则先手赢。
打表寻找规律,可以发现当x为质数时,sg[x]=x是第几个质数+1。
当x不为质数时,sg[x]=sg[p]且p是x的最小质因子。sg[1] = 1写一个类似线性筛的循环即可,线性筛保证每个数字都是被最小质因子筛到。
//考察NIM游戏,以及找规律?证明sg函数取值就留给自行思考。
#include<bits/stdc++.h>
#define pi acos(-1)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e6 + 10;
const LL mod = 1000000007;
int a[maxn];
int prime[maxn];
int sg[maxn], fac[maxn];
int is()
{
memset(prime, 1, sizeof(prime));
for(int i=2;i*i<maxn;i++){
if(prime[i]){
for(int j=i+i;j<maxn;j+=i){
prime[j]=0;
if(fac[j] == 0)fac[j] = i;
}
}
}
}
void init()
{
is();
int tot = 1;
sg[1] = 1;
for(int i=2; i<maxn; i++){
if(prime[i]) sg[i] = ++tot;
else sg[i] = sg[fac[i]];
}
}
int main()
{
init();
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
//printf("%d ", a[i]);
}//puts("");
for(int i=1; i<=n; i++){
a[i] = sg[a[i]];
}
int ans = a[1];
for(int i=2; i<=n; i++){
ans = ans ^ a[i];
}
if(ans == 0) {
printf("Long live with King Johann!\n");
}
else printf("Subconscious is our king!\n");
}
return 0;
}
/*
2
4
1 10 5 7
4
9 2 3 6
*/