hdu 5795 A Simple Nim(2016 Multi-University Training Contest 6——博弈)
2016-08-08 21:44
543 查看
题目大意:http://acm.hdu.edu.cn/showproblem.php?pid=5795
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 550 Accepted Submission(s): 363
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).To make the game more interesting,players
can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
Input
Intput contains multiple test cases. The first line is an integer 1≤T≤100,
the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n−1],
representing heaps with s[0],s[1],...,s[n−1] objects
respectively.(1≤n≤106,1≤s[i]≤109)
Output
For each test case,output a line whick contains either"First player wins."or"Second player wins".
Sample Input
2
2
4 4
3
1 2 4
Sample Output
Second player wins.
First player wins.
Author
UESTC
Source
2016 Multi-University Training Contest 6
题目大意:
两人于最优策略对n堆物品进行抓取,不能抓取的人输。
1、从同一堆中取任意个(>=0)
2、把一堆分成任意三堆(每一堆至少有一个)
解题思路:
sg[0]=0
当x=8k+7时sg[x]=8k+8,
当x=8k+8时sg[x]=8k+7,
其余时候sg[x]=x;(k>=0)
打表找规律可得,数学归纳法可证。
先附上打表代码。
AC代码。
A Simple Nim
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 550 Accepted Submission(s): 363
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).To make the game more interesting,players
can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.
Input
Intput contains multiple test cases. The first line is an integer 1≤T≤100,
the number of test cases. Each case begins with an integer n, indicating the number of the heaps, the next line contains N integers s[0],s[1],....,s[n−1],
representing heaps with s[0],s[1],...,s[n−1] objects
respectively.(1≤n≤106,1≤s[i]≤109)
Output
For each test case,output a line whick contains either"First player wins."or"Second player wins".
Sample Input
2
2
4 4
3
1 2 4
Sample Output
Second player wins.
First player wins.
Author
UESTC
Source
2016 Multi-University Training Contest 6
题目大意:
两人于最优策略对n堆物品进行抓取,不能抓取的人输。
1、从同一堆中取任意个(>=0)
2、把一堆分成任意三堆(每一堆至少有一个)
解题思路:
sg[0]=0
当x=8k+7时sg[x]=8k+8,
当x=8k+8时sg[x]=8k+7,
其余时候sg[x]=x;(k>=0)
打表找规律可得,数学归纳法可证。
先附上打表代码。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int sg[500]; int dfs(int n) { if(sg !=-1) return sg ; int vis[500]; memset(vis,0,sizeof(vis)); for(int i=0; i<n; i++) { vis[dfs(i)]=1; } for(int i=1; i<n; i++) { for(int j=1; j+i<n; j++) { vis[dfs(i)^dfs(j)^dfs(n-i-j)]=1; } } int i; for(i=0; i<500; i++) if(vis[i]==0) break; return sg =i; } int main () { memset(sg,-1,sizeof(sg)); sg[0]=0; for(int i=1; i<=50; i++) { sg[i]=dfs(i); printf("%d:%d\n",i,sg[i]); } return 0; }
AC代码。
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int main () { int t; scanf("%d",&t); while (t--) { int n,q; scanf("%d",&n); int ans=0; for (int i=1; i<=n; i++) { scanf("%d",&q); if (q%8==0) ans^=(q-1); else if(q%8==7) ans^=(q+1); else ans^=q; } if(ans==0) printf ("Second player wins.\n"); else printf ("First player wins.\n"); } return 0; }
相关文章推荐
- (HDU 5795)2016 Multi-University Training Contest 6 A Simple Nim (SG函数、博弈)
- HDU 5795 A Simple Nim(SG打表找规律)——2016 Multi-University Training Contest 6
- 2016 Multi-University Training Contest 6 1003 A Simple Nim (博弈sg函数)
- hdu 5754 2016 Multi-University Training Contest 3(各种博弈)
- HDU 5274 Chess(SG博弈)---2016 Multi-University Training Contest 1
- HDU 5754 Life Winner Bo(找规律 + 威佐夫博弈)--2016 Multi-University Training Contest 3
- 2016 Multi-University Training Contest 1 1002 hdu 5724 博弈
- HDU 5274 Chess(SG博弈)---2016 Multi-University Training Contest 1
- (HDU 5794)2016 Multi-University Training Contest 6 A Simple Chess (Lucas、容斥)
- hdu 5795 2016 Multi-University Training Contest 6(SG+打表找规律)
- (HDU 5724)2016 Multi-University Training Contest 1 Chess(SG函数、博弈)
- 2016 Multi-University Training Contest 1 1011(HDU 5733 计算几何)
- 2016 Multi-University Training Contest 1-1001---HDU 5723 Abandoned country(DFS+最小生成树)
- HDU 5724 Chess(2016 Multi-University Training Contest 1的1002题)
- 2016 Multi-University Training Contest 1-1011---HDU 5733 tetrahedron(计算几何)
- hdu 5349 MZL's simple problem 2015 Multi-University Training Contest 5
- 2016 Multi-University Training Contest 1 1004 hdu 5726 二分+RMQ
- (HDU 5733)2016 Multi-University Training Contest 1 tetrahedron(几何)
- HDU 5726 GCD (2016 Multi-University Training Contest 1)
- 2016 Multi-University Training Contest 1-1004---HDU 5726 GCD