多校6 1003 HDU5795 A Simple Nim (sg函数)

思路:直接打表找sg函数的值,找规律,没有什么技巧

还想了很久的,把数当二进制看,再类讨二进制中1的个数是必胜或者必败状态。。。。

打表:

 1 // #pragma comment(linker, "/STACK:102c000000,102c000000")
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <sstream>
 6 #include <string>
 7 #include <algorithm>
 8 #include <list>
 9 #include <map>
10 #include <vector>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <cstdlib>
15 // #include <conio.h>
16 using namespace std;
17 #define clc(a,b) memset(a,b,sizeof(a))
18 #define inf 0x3f3f3f3f
19 #define lson l,mid,rt<<1
20 // #define rson mid+1,r,rt<<1|1
21 const int N = 1e5+10;
22 const int M = 1e6+10;
23 const int MOD = 1e9+7;
24 #define LL long long
25 #define LB long double
26 // #define mi() (l+r)>>1
27 double const pi = acos(-1);
28 const double eps = 1e-8;
29 void fre(){freopen("in.txt","r",stdin);}
30 inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;}
31 
32 int g[1010];   
33 bool vis[1010];
34 int sg(int x){
35     if(g[x] != -1) return g[x];
36     if(x == 0) return 0;
37     if(x == 1) return 1;
38     if(x == 2) return 2;
39     if(x == 3) return 3;
40     clc(vis,0);
41     vis[0]=1;
42     for(int i = 1; i < x; i++){
43         for(int j=1; i+j<x; j++){
44             int t = 0;
45             int a = sg(i), b = sg(j),c=sg(x-i-j);
46             t ^= a;
47             t ^= b;
48             t^=c;
49             vis[a] = vis[b] =vis[c]=1;
50             vis[t] = 1;
51         }
52         vis[g[i]]=1;
53     }
54     for(int i = 0;; i++) if(!vis[i])
55             return i;
56 }
57 int main(){
58     int n;
59     clc(g,-1);
60     for(int i = 1; i <= 100; i++){
61         g[i] = sg(i);
62         printf("%d %d\n", i, g[i]);
63     }
64     return 0;
65 }

 

代码:

 1 // #pragma comment(linker, "/STACK:102c000000,102c000000")
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <sstream>
 6 #include <string>
 7 #include <algorithm>
 8 #include <list>
 9 #include <map>
10 #include <vector>
11 #include <queue>
12 #include <stack>
13 #include <cmath>
14 #include <cstdlib>
15 // #include <conio.h>
16 using namespace std;
17 #define clc(a,b) memset(a,b,sizeof(a))
18 #define inf 0x3f3f3f3f
19 #define lson l,mid,rt<<1
20 // #define rson mid+1,r,rt<<1|1
21 const int N = 1e6+10;
22 const int M = 1e6+10;
23 const int MOD = 1e9+7;
24 #define LL long long
25 #define LB long double
26 // #define mi() (l+r)>>1
27 double const pi = acos(-1);
28 const double eps = 1e-8;
29 void fre(){freopen("in.txt","r",stdin);}
30 inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;}
31 
32 int sg(int x){
33     if(x%8==0) return x-1;
34     if(x%8==7) return x+1;
35     return x;
36 }
37 int main(){
38     int T,n;
39     scanf("%d",&T);
40     while(T--){
41         int sum=0;
42         scanf("%d",&n);
43         for(int i=1;i<=n;i++){
44             int x;
45             scanf("%d",&x);
46             sum^=sg(x);
47         }
48         printf("%s\n",sum?"First player wins.":"Second player wins.");
49     }
50     return 0;
51 }

 

posted @ 2016-08-04 17:52  yyblues  阅读(221)  评论(0编辑  收藏  举报