1. 程式人生 > >【LA5059】Playing With Stones (SG函數)

【LA5059】Playing With Stones (SG函數)

play 操作 col pla int 一半 main 超過一半 mes

題意:有n堆石子,分別有a[i]個。兩個遊戲者輪流操作,每次可以選一堆,拿走至少一個石子,但不能拿走超過一半的石子。

誰不能拿石子就算輸,問先手勝負情況

n<=100,1<=a[i]<=2e18

思路:打表找SG函數的規律

當n為偶數時,SG(n)=n/2

當n為奇數時,SG(n)=SG(n/2)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 typedef long
long ll; 7 using namespace std; 8 #define N 110 9 #define oo 10000000 10 #define MOD 1000000007 11 12 ll sg(ll x) 13 { 14 if(x&1) return sg(x/2); 15 return x/2; 16 } 17 18 int main() 19 { 20 int cas; 21 scanf("%d",&cas); 22 while(cas--) 23 { 24 int n; 25 scanf("%d
",&n); 26 ll ans=0; 27 for(int i=1;i<=n;i++) 28 { 29 ll x; 30 scanf("%lld",&x); 31 ans^=sg(x); 32 } 33 if(ans) printf("YES\n"); 34 else printf("NO\n"); 35 } 36 return 0; 37 } 38

【LA5059】Playing With Stones (SG函數)