hdu 5011 nim博弈变形

约定不等于承诺〃 2022-08-08 14:51 252阅读 0赞

Game

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1008 Accepted Submission(s): 657

Problem Description

Here is a game for two players. The rule of the game is described below:

● In the beginning of the game, there are a lot of piles of beads.

● Players take turns to play. Each turn, player choose a pile i and remove some (at least one) beads from it. Then he could do nothing or split pile i into two piles with a beads and b beads.(a,b > 0 and a + b equals to the number of beads of pile i after removing)

● If after a player’s turn, there is no beads left, the player is the winner.

Suppose that the two players are all very clever and they will use optimal game strategies. Your job is to tell whether the player who plays first can win the game.

Input

There are multiple test cases. Please process till EOF.

For each test case, the first line contains a postive integer n(n < 10 5) means there are n piles of beads. The next line contains n postive integer, the i-th postive integer a i(a i < 2 31) means there are a i beads in the i-th pile.

Output

For each test case, if the first player can win the game, ouput “Win” and if he can’t, ouput “Lose”

Sample Input

  1. 1
  2. 1
  3. 2
  4. 1 1
  5. 3
  6. 1 2 3

Sample Output

  1. Win
  2. Lose
  3. Lose

Source

2014 ACM/ICPC Asia Regional Xi’an Online

题意:给定n堆石子,每堆石子有ai个,两个人轮流取石子,轮到某个人时,可以选择某一堆,然后取这堆的至少一个石子,取完后可以将这堆石子分成两对,

最后全部取完的人胜利。

分析: 试了好多数据,发现分堆是不影响的,于是就是经典的nim博弈。 因此只要计算XOR异或值,就可以判断胜负。

首先一旦从XOR为零的状态取走至少一颗石子,XOR就一定会变成非零,因此,可以证明必败态只能转移到必胜态。

接下来,我们来证明必胜态总是能转移到某个必败态,观察XOR的二进制表示的最高位的1,选取石子的二进制来表示对应位也为1的某堆石子。只要从中取走使得该位变为0,且其余XOR中的1也反转的数量的石子,XOR就可以变为0。

  1. #include<bitset>
  2. #include<map>
  3. #include<vector>
  4. #include<cstdio>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<string>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<queue>
  12. #include<set>
  13. #define inf 0x3f3f3f3f
  14. #define mem(a,x) memset(a,x,sizeof(a))
  15. using namespace std;
  16. typedef long long ll;
  17. typedef pair<int,int> pii;
  18. inline int in()
  19. {
  20. int res=0;char c;
  21. while((c=getchar())<'0' || c>'9');
  22. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  23. return res;
  24. }
  25. const int N=100010;
  26. ll a[N];
  27. int main()
  28. {
  29. int n;
  30. while(~scanf("%d",&n))
  31. {
  32. ll ans=0;
  33. for(int i=0;i<n;i++)
  34. {
  35. scanf("%I64d",&a[i]);
  36. ans^=a[i];
  37. }
  38. puts(ans==0? "Lose" : "Win");
  39. }
  40. return 0;
  41. }

发表评论

表情:
评论列表 (有 0 条评论,252人围观)

还没有评论,来说两句吧...

相关阅读

    相关 博弈论之Nim 博弈

    写在前面: 此类问题一般有如下特点: > 1、博弈模型为两人轮流决策的非合作博弈。即两人轮流进行决策,并且两人都使用最优策略来获取胜利。 > > 2、博弈是有限的。即无论