HDU-3032

Problem Description
    Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.

    Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.

    Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.

Input
    Input 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 ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)

Output
    For each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.

Sample Input
2
3
2 2 3
2
3 3

Sample Output
Alice
Bob

Nim or not Nim?

  题目就不说了,解题思路就是SG先打表找规律,简单的尼姆博奕

打表代码 :

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 #define maxn 1000005
 5
 6 int sg[maxn];
 7 bool vis[maxn];
 8
 9
10 void GetSG()
11 {
12     for(int i = 1 ; i <= 100 ; i++)
13     {
14         memset(vis , 0 , sizeof(vis));
15         for(int j = 0 ; j < i ; j++)
16             vis[sg[j]] = 1;
17         if(i >= 2)
18         {
19             for(int j = 1 ; j < i ; j++)
20                     vis[ sg[j] ^ sg[i-j] ] = 1;
21         }
22         int mid = 0;
23         while(vis[mid])
24             mid++;
25         sg[i] = mid;
26         printf("%d : %d\n",i,sg[i]);
27     }
28 }
29
30
31 int main()
32 {
33     GetSG();
34     return 0;
35
36 }

打表结果 

1 : 1
2 : 2
3 : 4
4 : 3
5 : 5
6 : 6
7 : 8
8 : 7
9 : 9
10 : 10
11 : 12
12 : 11
13 : 13
14 : 14
15 : 16
16 : 15
17 : 17
18 : 18
19 : 20
20 : 19
21 : 21
22 : 22
23 : 24
24 : 23
25 : 25
26 : 26
27 : 28
28 : 27
29 : 29
30 : 30
31 : 32
32 : 31
33 : 33
34 : 34
35 : 36
36 : 35
37 : 37
38 : 38
39 : 40
40 : 39
41 : 41
42 : 42
43 : 44
44 : 43
45 : 45
46 : 46
47 : 48
48 : 47
49 : 49
50 : 50
51 : 52
52 : 51
53 : 53
54 : 54
55 : 56
56 : 55
57 : 57
58 : 58
59 : 60
60 : 59
61 : 61
62 : 62
63 : 64
64 : 63
65 : 65
66 : 66
67 : 68
68 : 67
69 : 69
70 : 70
71 : 72
72 : 71
73 : 73
74 : 74
75 : 76
76 : 75
77 : 77
78 : 78
79 : 80
80 : 79
81 : 81
82 : 82
83 : 84
84 : 83
85 : 85
86 : 86
87 : 88
88 : 87
89 : 89
90 : 90
91 : 92
92 : 91
93 : 93
94 : 94
95 : 96
96 : 95
97 : 97
98 : 98
99 : 100
100 : 99

Result

  由于是刚开始接触这种类型的尼姆博弈,说实话对SG还不是很理解,还不能灵活的应用,所以这篇随笔目前也没啥注释,

待我对这类问题有了更清楚的理解,再回来看看这吧。

AC Code :

 1 #include <bits/stdc++.h>
 2
 3 int GetSG(int num)
 4 {
 5     if(0 == num % 4)
 6         return num - 1;
 7     if(3 == num % 4)
 8         return num + 1;
 9     return num;
10 }
11
12 int main()
13 {
14     int t;
15     scanf("%d",&t);
16     while(t--)
17     {
18         int n , res = 0 , num;
19         scanf("%d",&n);
20         for(int i = 1 ; i <= n ; i++)
21         {
22             scanf("%d" , &num);
23             res ^= GetSG(num);
24         }
25         if(res)
26             printf("Alice\n");
27         else
28             printf("Bob\n");
29     }
30     return 0;
31 }

  其实这个题和 HDU-5795 http://acm.split.hdu.edu.cn/showproblem.php?pid=5795 神似,(同样的多校赛的题,年份不一样//滑稽)不过就是那个题可以分为3个更小的堆(其实我是先做那个题才做这个的),同样打表找规律,代码稍微修改下就能过,这里就不多说了。

时间: 2024-11-01 20:22:44

HDU-3032的相关文章

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

HDU 3032 Nim or not Nim?(sg函数博弈)

题目地址:HDU 3032 这题是很好用来练习sg函数打表的一题. 下面是sg函数值打表代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include &

HDU 3032 Nim or not Nim?(sg函数)

题目链接 暴力出来,竟然眼花了以为sg(i) = i啊....看表要认真啊!!! #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define LL __int64 int dp[10001]; int sg(int x) { int flag[10001],temp,i; if(dp[x] >= 0) return dp[x]; memset(flag,

hdu 3032 Nim or not Nim? sg函数

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1056    Accepted Submission(s): 523 Problem Description Nim is a two-player mathematic game of strategy in which players take turn

HDU 3032 Nim or not Nim? (sg函数求解)

Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provide

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

hdu 3032 sg打表找规律 *

有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 打表代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 const int N=1000010; 7 int sg[N];

hdu 3032 Nim or not Nim? 博弈论,,,网上搜的题解让我大开眼界,原来还可以这样A题

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1076    Accepted Submission(s): 532 Problem Description Nim is a two-player mathematic game of strategy in which players take tur

hdu 3032 Nim or not Nim? (SG,然后找规律)

题意: n堆石头,每堆石头个数:s[1]...s[n]. 每人每次可以选择在一堆中取若干个(不能不取),或者把一堆石头分成两堆(两堆要都有石头). 无法操作者负. 数据范围: (1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1) 思路: S[i]太大了,直接求SG铁定TLE,所以先把SG打出来看看找一下规律. 分堆也挺好理解,把它抽象成游戏图去想就清晰了. 直接看代码. 代码: int sg[10005]; int s[10005]; ///打表发现当x=0,4k+1,4k+2

HDU 3032 Nim or not Nim? [Multi-SG]

传送门 题意: nim游戏,多了一种操作:将一堆分成两堆 Multi-SG游戏规定,在符合拓扑原则的前提下,一个单一游戏的后继可以为多个单一游戏. 仍然可以使用$SG$函数 然后本题规模很大,手动打一下表,发现$%4=3$时$sg(x)=x+1$,$%4=0$时$sg(x)=x-1$,其他不变 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #incl