HDU 5795 A Simple Nim (SG函式+打表找規律)
A Simple Nim
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1129 Accepted Submission(s): 637
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
POINT:
遊戲的和的SG函式值是它的所有子游戲的SG函式值的異或(重要!!)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
/*int sg[10005]= {0,1,2};//打表
int vis[10005];
void play()
{
for(int i=3;i<=100;i++)
{
memset(vis,0,sizeof vis);
for(int j=1;j<=i;j++)
{
for(int k=1;k<=i;k++)
{
if(j+k<i)
{
vis[sg[j]^sg[k]^sg[i-j-k]]=1;
}
}
}
for(int j=0;j<i;j++)
{
vis[sg[j]]=1;
}
for(int j=0;;j++)
{
if(!vis[j])
{
sg[i]=j;
break;
}
}
}
for(int i=0;i<=100;i++)
{
printf("%d %d\n",i,sg[i]);
}
}
*/
ll sg(ll n)
{
if(n%8==0) return n-1;
else if((n+1)%8==0) return n+1;
else return n;
}
int main()
{
// play();
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
ll res=0;
while(n--)
{
ll a;
scanf("%lld",&a);
res=res^sg(a);
}
if(res==0) printf("Second player wins.\n");
else printf("First player wins.\n");
}
return 0;
}
相關文章
- 打表找規律
- NIM遊戲/SG函式遊戲函式
- HDU 6298 Maximum Multiple(找規律)
- 博弈論基礎之sg函式與nim函式
- HDU 1848 Fibonacci again and again(SG函式)AI函式
- HDU-6415 Rikka with Nash Equilibrium (DP/找規律)UI
- HDU 1848 Fibonacci again and again (尼姆博弈+sg函式)AI函式
- HDU 6415(dp/找規律-2018多校第九場1001)
- HDU 2197 本原串 (規律+快速冪)
- HUNAN -11566 Graduation Examination(找規律)NaN
- HDU 6063 RXD and math (打表)
- LeetCode-6. Z字形變換(找規律)LeetCode
- Touring cities (找規律 哈密爾頓迴路)
- Gym - 101532A Subarrays Beauty(位操作找規律)
- HDU 1709 The Balance(母函式)函式
- ECNU OJ 3354 領外賣(博弈-SG函式)函式
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- LightOj1296Again Stone Game(手推SG函式)AIGAM函式
- ZOJ Monthly, January 2019 - A Little Sub and Pascal's Triangle(找規律)
- 【博弈論】組合遊戲及SG函式淺析遊戲函式
- HDU 母函式簡單題 - 找單詞/Ignatius and the Princess III/Square Coins/Holding Bin-Laden Captive!函式APT
- 博弈論:公平組合遊戲(Nim 遊戲 & SG 定理)學習筆記遊戲筆記
- 兩個需要求 sg 函式的樹上博弈問題函式
- HDU 1792 - A New Change Problem(規律,最大不能組合數及其個數)
- QOJ7789-一道位運算找規律好題
- HDU 5794 A Simple Chess (lucas定理+費馬小定理)
- 普通函式與函式模板呼叫規則函式
- 虛擬函式,虛擬函式表函式
- 普通函式與函式模板呼叫規則2函式
- 【LeetCode動態規劃#04】不同的二叉搜尋樹(找規律,有點像智力題)LeetCode動態規劃
- 函式正規化入門(惰性求值與函式式狀態)函式
- JavaScript 表單、物件、函式JavaScript物件函式
- leedcode-單詞規律
- 三、凸透鏡成像規律
- LeetCode-單詞規律LeetCode
- php之正規表示式函式總結PHP函式
- 建構函式方式建立正規表示式函式
- Python正規表示式 findall函式詳解Python函式