hdu 3980 Paint Chain sg函数
给一个长度为n的环, 两个人轮流涂色, 每次涂m个连续的, 无法继续涂了就输。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1061109567; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 int sg[1005], m; 20 int mex(int x) { 21 if(~sg[x]) 22 return sg[x]; 23 if(x<m) 24 return sg[x] = 0; 25 int vis[1000]; 26 mem(vis); 27 for(int i = 0; i<=x-m; i++) { 28 vis[mex(i)^mex(x-m-i)] = 1; //相当于把一堆个数为x的石子分成两堆, 一堆个数为i, 另一堆为x-m-i 29 } 30 int i = 0; 31 while(1) { 32 if(!vis[i]) 33 return sg[x] = i; 34 i++; 35 } 36 } 37 int main() 38 { 39 int t, n, cnt = 1; 40 cin>>t; 41 while(t--) { 42 mem1(sg); 43 scanf("%d%d", &n, &m); 44 printf("Case #%d: ", cnt++); 45 if(n<m) { 46 puts("abcdxyzk"); 47 continue; 48 } 49 sg[n-m] = mex(n-m); 50 if(!sg[n-m]) { 51 puts("aekdycoin"); 52 } else { 53 puts("abcdxyzk"); 54 } 55 } 56 return 0; 57 }