Little Sub and Johann
生活随笔
收集整理的這篇文章主要介紹了
Little Sub and Johann
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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]且p是x的最小質因子。
? 寫一個類似線性篩的循環即可,線性篩保證每個數字都是被最小質因子篩到。
對每堆的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 DEBUGfreopen("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 DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }?
總結
以上是生活随笔為你收集整理的Little Sub and Johann的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Little Sub and AA
- 下一篇: Game 23