HDU 3970 Paint Chain (博弈,SG函数)
Paint Chain
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 909 Accepted Submission(s): 325
Problem Description
Aekdycoin and abcdxyzk are playing a game. They get a circle chain with some beads. Initially none of the beads is painted. They take turns to paint the chain. In Each turn one player must paint a unpainted beads. Whoever is unable to paint in his turn lose the game. Aekdycoin will take the first move.
Now, they thought this game is too simple, and they want to change some rules. In each turn one player must select a certain number of consecutive unpainted beads to paint. The other rules is The same as the original. Who will win under the rules ?You may assume that both of them are so clever.
Now, they thought this game is too simple, and they want to change some rules. In each turn one player must select a certain number of consecutive unpainted beads to paint. The other rules is The same as the original. Who will win under the rules ?You may assume that both of them are so clever.
Input
First line contains T, the number of test cases. Following T line contain 2 integer N, M, indicate the chain has N beads, and each turn one player must paint M consecutive beads. (1 <= N, M <= 1000)
Output
For each case, print "Case #idx: " first where idx is the case number start from 1, and the name of the winner.
Sample Input
2
3 1
4 2
Sample Output
Case #1: aekdycoin
Case #2: abcdxyzk
Author
jayi
Source
用SG函数搞一遍就可以了。
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 19:20:19 4 File Name :E:\2013ACM\比赛练习\2013-11-17\H.cpp 5 ************************************************ */ 6 7 #include <stdio.h> 8 #include <string.h> 9 #include <iostream> 10 #include <algorithm> 11 #include <vector> 12 #include <queue> 13 #include <set> 14 #include <map> 15 #include <string> 16 #include <math.h> 17 #include <stdlib.h> 18 #include <time.h> 19 using namespace std; 20 const int MAXN = 1010; 21 int sg[MAXN]; 22 bool vis[MAXN]; 23 int m; 24 int mex(int n) 25 { 26 if(sg[n] != -1)return sg[n]; 27 if(n < m)return sg[n] = 0; 28 memset(vis,false,sizeof(vis)); 29 for(int i = m;i <= n;i++) 30 vis[mex(i-m)^mex(n-i)] = true; 31 for(int i = 0;;i++) 32 if(vis[i] == false) 33 { 34 sg[n] = i; 35 break; 36 } 37 return sg[n]; 38 } 39 40 int main() 41 { 42 //freopen("in.txt","r",stdin); 43 //freopen("out.txt","w",stdout); 44 int T; 45 int n; 46 scanf("%d",&T); 47 int iCase = 0; 48 while(T--) 49 { 50 scanf("%d%d",&n,&m); 51 iCase++; 52 if(n < m) 53 { 54 printf("Case #%d: abcdxyzk\n",iCase); 55 continue; 56 } 57 n -= m; 58 memset(sg,-1,sizeof(sg)); 59 for(int i = 0;i <= n;i++) 60 sg[i] = mex(i); 61 if(sg[n] == 0)printf("Case #%d: aekdycoin\n",iCase); 62 else printf("Case #%d: abcdxyzk\n",iCase); 63 } 64 return 0; 65 }
人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想