首页 > 代码库 > hdu 4203

hdu 4203

Doubloon Game

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


Problem Description
Being a pirate means spending a lot of time at sea. Sometimes, when there is not much wind, days can pass by without any activity. To pass the time between chores, pirates like to play games with coins.

An old favorite of the pirates is a game for two players featuring one stack of coins. In turn, each player takes a number of coins from the stack. The number of coins that a player takes must be a power of a given integer K (1, K, K^2, etcetera). The winner is the player to take the last coin(s).
Can you help the pirates gure out how the player to move can win in a given game situation?
 

 

Input
The first line of the input contains a single number: the number of test cases to follow. Each test case has the following format:
One line with two integers S and K, satisfying 1 <= S <= 10^9 and 1 <= K <= 100: the size of the stack and the parameter K, respectively.
 

 

Output
For every test case in the input, the output should contain one integer on a single line: the smallest number of coins that the player to move can take in order to secure the win. If there is no winning move, the output should be 0.
 

 

Sample Input
5
5 1
3 2
8 2
50 3
100 10
Sample Output
1
0
2
0
1
因为范围太大,不能直接暴力求sg值
打表找规律
当k为奇数时
sg值
0 1 0 1 0 1 0 1 0 1
当k为偶数时
k=2
1 2 0 1 2 0 1 2 0 1 2 0
k=4
1 0 1 2 0 1 0 1 2 0 1 0 1 2 0
k=6
1 0 1 0 1 2 0 1 0 1 0 1 2 0
打表找一下规律
 1 #include<iostream> 2 #include<string> 3 #include<cstdio> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<set> 8 #include<algorithm> 9 #include<cstring>10 #include<stdlib.h>11 #include<math.h>12 #include<map>13 using namespace std;14 #define ll long long15 int sg[1000],k;16 void getsg(){17 18     for(int i=1;i<=100;i++){19         int vit[1000];20         memset(vit,0,sizeof(vit));21         for(int j=1;j<=i;j*=k) vit[sg[i-j] ]=1;22         for(int j=0;;j++)23             if(!vit[j]){24                 sg[i]=j;break;25             }26     }27 }28 int fix(int x,int len){29     if(((x%len)==0)||(((x%len)%2==0)&&(x%len!=len-1))) return 1;30     else return 0;31 }32 int main(){33     int t,s;cin>>t;34     while(t--){35         cin>>s>>k;36         if(k&1){37             if(s&1) cout<<1<<endl;38             else cout<<0<<endl;39             continue;40         }41         int len=2*(k/2-1)+3;42         ll tmp=1;43         if(fix(s,len)){44             cout<<0<<endl;45             continue;46         }47         while(1){48             ll kk=s-tmp;49             kk%=len;50             if(fix(kk,len)){51                 cout<<tmp<<endl;52                 break;53             }54             tmp*=k;55         }56     }57 }

 

hdu 4203