http://acm.hznu.edu.cn/OJ/problem.php?cid=1263&pid=12

http://acm.hznu.edu.cn/OJ/problem.php?id=2591

题意:石子游戏,要不取一堆,要不取x个且gcd(x,该堆石子个数)=1,问谁会赢。

题解:显然符合NIM游戏,只要能算出sg函数值即可判断。

  打表寻找规律,可以发现当x为质数时,sg[x]=x是第几个质数+1

  x不为质数时,sg[x]=sg[p]px的最小质因子。

  写一个类似线性筛的循环即可,线性筛保证每个数字都是被最小质因子筛到。

对每堆的sg函数值异或;

如果最后结果为零则后手赢

否则先手

参考文章:

https://www.cnblogs.com/exponent/articles/2141477.html

https://www.cnblogs.com/MyNameIsPc/p/7597366.html

https://blog.csdn.net/yew1eb/article/details/38775419

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q;
int ans,cnt,flag,temp,sum;
int a[N];
int pre[N];
bool prime[N];
int sg[N];
char str;
struct node{};
void init(){
    prime[0]=prime[1]=1;
    sg[1]=1;
    for(int i=2;i<N;i++){
        if(!prime[i]){
            pre[++cnt]=i;
            sg[i]=cnt+1;

        }
        for(int j=1;j<=cnt&&i*pre[j]<N;j++){
            prime[i*pre[j]]=1;
            sg[i*pre[j]]=sg[pre[j]];
            if(i%pre[j]==0){
                break;
            }
        }//cout<<i<<" "<<sg[i]<<endl;
    }
}


int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            ans^=sg[a[i]];
        }
        if(ans){
            cout<<"Subconscious is our king!"<<endl;
        }else{
            cout<<"Long live with King Johann!"<<endl;
        }
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}