1. 程式人生 > >hdu-3980-nim博弈/sg函數

hdu-3980-nim博弈/sg函數

rst sub tin bre sim others tput sea names

Paint Chain

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2465 Accepted Submission(s): 880


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.

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 2011 Multi-University Training Contest 14 - Host by FZU

一串長度為n的圓形珠子,每次可以找連續的m個白色珠子染色,不能操作者輸,問先手狀態。

   先計算出線性狀態下的遊戲的sg函數,再計算與真實狀態的sg函數時判斷sg[n-m]=sg[n-m]==0?1:0。

由於子狀態只有一種。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int sg[1010];
 4 bool vis[1010];
 5 int main(){
 6     int t,n,m,i,j,k;
 7     cin>>t;
 8     for(int cas=1;cas<=t;++cas){
 9         cin>>n>>m;
10         printf("Case #%d: ",cas);
11         if(n<m){
12             puts("abcdxyzk");
13             continue;
14         }
15         for(i=0;i<m;++i) sg[i]=0;
16         for(i=m;i<=n;++i){
17             memset(vis,0,sizeof(vis));
18             for(j=1;j+m-1<=i;j++){
19                 vis[sg[j-1]^sg[i-m-j+1]]=1;
20             }
21             for(j=0;;j++){
22                 if(!vis[j]){
23                     sg[i]=j;
24                     break;
25                 }
26             }
27         }
28         for(i=0;i<=n;++i){
29             if(sg[i]==0) sg[i]=1;
30             else sg[i]=0;
31         }
32         sg[n-m]?puts("aekdycoin"):puts("abcdxyzk");
33     }
34     return 0;
35 }

hdu-3980-nim博弈/sg函數