poj1067-取石子游戏-wythoff博弈
打表找规律失败,搜了一下原来是wythoff博弈
1 /*--------------------------------------------------------------------------------------*/ 2 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <ctype.h> 7 #include <cstdlib> 8 #include <cstdio> 9 #include <vector> 10 #include <string> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <set> 15 #include <map> 16 17 //debug function for a N*M array 18 #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\ 19 {for(int j=0;j<(M);j++){\ 20 printf("%d",G[i][j]);}printf("\n");} 21 //debug function for int,float,double,etc. 22 #define debug_var(X) cout<<#X"="<<X<<endl; 23 #define LL long long 24 const int INF = 0x3f3f3f3f; 25 const LL LLINF = 0x3f3f3f3f3f3f3f3f; 26 const double eps = 1e-8; 27 /*--------------------------------------------------------------------------------------*/ 28 using namespace std; 29 30 int N,M,T; 31 int mem[1000][1000] = {0}; 32 const double q = (1+sqrt(5.0)) / 2.0; 33 34 void display(int x) 35 { 36 if(x==0) return ; 37 display(x>>1); 38 putchar((x&1) + '0'); 39 } 40 41 int sg(int a,int b) 42 { 43 if(mem[a][b] != -1) return mem[a][b]; 44 if(a == 0 && b == 0)return 0; 45 set<int> st ; 46 for(int i=1;i<=a;i++) 47 { 48 st.insert(sg(a-i,b)); 49 } 50 for(int i=1;i<=b;i++) 51 { 52 st.insert(sg(a,b-i)); 53 } 54 for(int i=1;i<=min(a,b);i++) 55 { 56 st.insert(sg(a-i,b-i)); 57 } 58 int g = 0; 59 while(st.find(g) != st.end()) g++; 60 return mem[a][b] = g; 61 } 62 63 int Wythoff(int a,int b) 64 { 65 if( a > b) swap(a,b); 66 int k = b - a; 67 if(a == (int)(k*q)) return 0; 68 else return 1; 69 } 70 71 int main() 72 { 73 int a,b; 74 while(~scanf("%d%d",&a,&b)) 75 { 76 printf("%d\n",Wythoff(a,b)); 77 } 78 }