Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Sprague Grundy Dividing Piles */
- /*********************** let the play begin() *********************************/
- int gr[MAX];int ans,f[MAX],root,pre[MAX];
- void grundy(int n){
- int y = 0;
- for(int i = 2;;i++){// Number of ways of dividing
- if(i*(i-1)>=2*n)break;
- int x = n - (i*(i-1))/2;
- if(x%i)continue;
- x/=i;
- y = pre[x-1]^pre[x+i-1];
- if(!y&&ans==-1&&n==root)ans = i;//if xor of second step is zero that means 1st player will win because
- // cout<<y<<endl;// second player starts from 2nd step. that's why !y comes .
- f[y] = n;//keep tracking the grundy values
- }
- int cc = 0;
- while(f[cc]==n)cc++;//mex of all grundies is grundy for n
- gr[n] = cc;
- pre[n] = cc;
- pre[n]^=pre[n-1];
- }
- int main()
- {
- booster()
- ///read("input.txt");
- int n;
- cin>>n;
- root = n;
- ans = -1;
- for(int i = 3;i<=n;i++){
- grundy(i);
- }
- if(gr[n])cout<<ans;
- else cout<<-1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement