多校6 1003 HDU5795 A Simple Nim

思路:直接打表找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 }
时间: 2024-10-13 05:19:07

多校6 1003 HDU5795 A Simple Nim的相关文章

hdu-5795 A Simple Nim(组合游戏)

题目链接: A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 181    Accepted Submission(s): 119 Problem Description Two players take turns picking candies from n heaps,the player who picks

hdu5795 A Simple Nim 求nim求法,打表找sg值规律 给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空。求先手必胜,还是后手必胜。

/** 题目:A Simple Nim 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给定n堆石子,每堆有若干石子,两个人轮流操作,每次操作可以选择任意一堆取走任意个石子(不可以为空) 或者选择一堆,把它分成三堆,每堆不为空.求先手必胜,还是后手必胜. 思路: 组合游戏Nim: 计算出每一堆的sg值,然后取异或.异或和>0那么先手,否则后手. 对于每一堆的sg值求解方法: 设:sg(x)表示x个石子的sg值.sg(x) = mex{sg

hdu5795 多校第六场 a simple nim

http://acm.hdu.edu.cn/showproblem.php?pid=5795 可以进行一堆分三堆操作的nim多堆问题 nim问题说到底也是sg函数的问题,sg函数求的是当前状态所无法到达的最小状态,这个状态本质是由自己编号的,一般情况下根据石子数目编号,但是不可以采用不同的两套标准.对于分三堆,比如3->(1,2)似乎无法进行sg函数编号,实际上两堆的sg等于两堆独立sg的异或,但是又值得注意的是sg函数所能到达的状态的含义是当前局面的子和子子...局面. 所以sg打表我是模仿这

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int

HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 48 Problem Description Two players take turns picking candies from n heaps,the player who picks the las

HDU 4937 (杭电多校 #7 1003题)Lucky Number(瞎搞)

题目地址:HDU 4937 多校的题以后得重视起来...每道题都错好多次...很考察细节.比如这道....WA了无数次.... 这题的思路自己真心想不到...这题是将进制后的数分别是1位,2位,3位和更多位的分开来计算. 当是1位的时候,显然只有3到6,此时只能是-1 当是2位的时候,可以转换成一元一次方程求解 当是3位的时候,可以转换成一元二次方程求解 当是4位的时候,此时最多也只有7000个数,7000^3接近1e12.所以剩下的直接枚举进制数来判断即可. 代码如下: #include <i

HDU 5795 A Simple Nim 打表求SG函数的规律

A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).T

HDU5795A Simple Nim SG定理

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 980    Accepted Submission(s): 573 Problem Description Two players take turns picking candies from n heaps,the player who picks the l

?HDU 5795 A Simple Nim(简单Nim)

HDU 5795 A Simple Nim(简单Nim) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On e