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;
}
相關文章
- HDU 2897-邂逅明下(博弈-SG函式打表找規律)函式
- 打表找規律
- HDU 1847-Good Luck in CET-4 Everybody!(博弈-SG函式/找規律)Go函式
- NIM遊戲/SG函式遊戲函式
- HDU 6298 Maximum Multiple(找規律)
- HDU 4951 Multiplication table(找規律)
- 博弈學習(一) NIM + SG函式函式
- SG 函式初步 HDU 1536 && HDU 1944函式
- 博弈論基礎之sg函式與nim函式
- HDU1729 Stone Game (SG函式)GAM函式
- HDU5139 Formula (找規律+離線處理)ORM
- HDU-6415 Rikka with Nash Equilibrium (DP/找規律)UI
- HDU 5439 Aggregated Counting(找規律+預處理)
- HDU 1846-Brave Game(巴什博弈-SG函式)GAM函式
- 取石子的幾個找sg函式的問題函式
- HDU 1848 Fibonacci again and again(SG函式)AI函式
- HDU 1847 Good Luck in CET-4 Everybody!(找規律版巴什博奕)Go
- POJ 2311-Cutting Game(Nim博弈-sg函式/記憶化搜尋)GAM函式
- HDU 1848 Fibonacci again and again (尼姆博弈+sg函式)AI函式
- HDU 2082-找單詞(母函式-有限次)函式
- HDU4342 History repeat itself數學規律題
- Touring cities (找規律 哈密爾頓迴路)
- 8.13(優先佇列貪心維護+打表找規律+對頂堆優先佇列+DFS減枝+貪心dp)佇列
- 數學規律題,數論知識:hdu1792
- 博弈論進階之SG函式函式
- LeetCode-6. Z字形變換(找規律)LeetCode
- codeforces 340CTourist Problem(找規律數學題)
- ZOJ 3876 May Day Holiday(找規律||打表-基姆拉爾森計算公式(求某年某月某日星期幾))公式
- HUNAN -11566 Graduation Examination(找規律)NaN
- Gym - 101532A Subarrays Beauty(位操作找規律)
- 杭電ACM hdu 2082 找單詞 解題報告(母函式)ACM函式
- SG_007_CHAPTER 4 (匯聚函式)APT函式
- 【組合數+找規律】codeforces 815B - Karen and Test
- HDU 1709 The Balance(母函式)函式
- HDU2588GCD(尤拉函式)GC函式
- 【博弈論】組合遊戲及SG函式淺析遊戲函式
- hdu 1088 Write a simple HTML BrowserHTML
- QOJ7789-一道位運算找規律好題