HDU 3032 Nim or not Nim Nim博弈 规律
2016-04-26 20:29
330 查看
Nim or not Nim?
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
3032
Appoint description:
System Crawler (2016-04-24)
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
如果不是数学大牛,基本上不太可能通过理论分析来求解这道题目,目前我们只知道求解这道题目需要计算每一堆物品的SG值,但这又不是传统的Nim博弈,加上数据范围非常大,那看来我们只有通过规律来求解了,这也是许多博弈问题的解法,幸运的是,一般这种问题的结论都会非常简单。
既然要找规律,那我们就要先手工算出一些SG值,看能否从中看出一些规律,这就考验大家的基础知识了。
就这道题目而言,由于每一堆物品的处理方式是相同的,因此我们可以只考虑一堆物品。
g(0)=0,g(1)=1;这是很显然的
g(2)=2;因为其后继状态为0,1;分解为(1,1)。g(2)=mex(0,1)=2
g(3)=4;其后继为0,1,2,;分解为(1,2)。g(3)=mex(0,1,2,3)=4
g(4)=3;后继为0,1,2,3;分解为(1,3),(2,2),g(4)=mex(0,1,2,4,5)=3
g(5)=5;
g(6)=6;
g(7)=8;
......
也许你已经看出了什么,也许你还是一头雾水,我首先说一下结论
if(n%4==3)
return n+1;
else if(n%4==0)
return n-1;
else
return n;
如果实在看不出来,还可以打一个表,数据量更多的情况下会更容易观察
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
using namespace std;
int get_sg(int n)
{
if(n%4==3)
return n+1;
else if(n%4==0)
return n-1;
else
return n;
}
int main()
{
int T,N,heap;
int ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
ans=0;
while(N--)
{
scanf("%d",&heap);
ans^=get_sg(heap);
}
printf("%s\n",ans==0?"Bob":"Alice");
}
return 0;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
3032
Appoint description:
System Crawler (2016-04-24)
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
如果不是数学大牛,基本上不太可能通过理论分析来求解这道题目,目前我们只知道求解这道题目需要计算每一堆物品的SG值,但这又不是传统的Nim博弈,加上数据范围非常大,那看来我们只有通过规律来求解了,这也是许多博弈问题的解法,幸运的是,一般这种问题的结论都会非常简单。
既然要找规律,那我们就要先手工算出一些SG值,看能否从中看出一些规律,这就考验大家的基础知识了。
就这道题目而言,由于每一堆物品的处理方式是相同的,因此我们可以只考虑一堆物品。
g(0)=0,g(1)=1;这是很显然的
g(2)=2;因为其后继状态为0,1;分解为(1,1)。g(2)=mex(0,1)=2
g(3)=4;其后继为0,1,2,;分解为(1,2)。g(3)=mex(0,1,2,3)=4
g(4)=3;后继为0,1,2,3;分解为(1,3),(2,2),g(4)=mex(0,1,2,4,5)=3
g(5)=5;
g(6)=6;
g(7)=8;
......
也许你已经看出了什么,也许你还是一头雾水,我首先说一下结论
if(n%4==3)
return n+1;
else if(n%4==0)
return n-1;
else
return n;
如果实在看不出来,还可以打一个表,数据量更多的情况下会更容易观察
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
using namespace std;
int get_sg(int n)
{
if(n%4==3)
return n+1;
else if(n%4==0)
return n-1;
else
return n;
}
int main()
{
int T,N,heap;
int ans;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
ans=0;
while(N--)
{
scanf("%d",&heap);
ans^=get_sg(heap);
}
printf("%s\n",ans==0?"Bob":"Alice");
}
return 0;
}
相关文章推荐
- 为什么我们不能坚持到底?
- nVIDIA显卡命名规律
- 人才成长有其自身的规律
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000