其他分享
首页 > 其他分享> > 2018 - 2019 CTU Open Contest H. Split Game 【SG函数】

2018 - 2019 CTU Open Contest H. Split Game 【SG函数】

作者:互联网

H. Split Game

time limit per test 1.0 s memory limit per test 256 MB input standard input output standard output

For a long time, rich clientele of Binary Casino has been requesting a new way to gamble their money. To fulfill their wishes, the director of Binary Casino decided to introduce a new game called Split Your Tokens.

This game is played only when a customer is about to exit the casino. Instead of exchanging tokens won during his visit, he may take up casino's challenge and bet all of his earned tokens on winning this game. Should the customer lose, all of his tokens are lost in favor of the casino.

When the game starts, the customer splits his tokens into NN piles with not necessarily same amount of tokens in each pile. The customer and the casino then exchange turns ­ in this game we denote the customer as the first player and the casino as the second player. Each player in his turn decides which pile he wants to split and chooses a positive integer KK which is smaller than the size of the selected pile. Then the player splits the selected pile into as many piles of size KK as possible. If any tokens remain, they form another pile on their own. A player loses the game when he can not do any more splitting. The customer (first player) always plays first.

The director of Binary Casino is however not sure, whether this game will be profitable for the casino in the long term. Your task is thus to determine, for a given configuration of piles, which player wins when both players play optimally.

Input

The first line contains one integer NN (1≤N≤20001≤N≤2000), the number of piles. The second line contains a sequence of NN integers PiPi (1≤Pi≤20001≤Pi≤2000), PiPi represents the number of tokens in the ii-th pile.

Output

Output a single line with either "First" or "Second", depending on which player wins the game if both play optimally.

Examples input
3
1 2 3
output
First
input
3
1 2 2
output
Second

 

 题意概括:

给出 N 堆物品, 两个玩家轮流选择将其中一个大小为 M 的堆 分成最多的 大小为 K (K < 当前选择的堆的大小)的堆,若 M%K 有剩余,则剩余的自成一堆。

最后所有堆大小都为 1 时不可再分,不能再分堆的玩家输。

解题思路:

因为SG函数的作用就是把博弈的状态当成一个点,然后形成一张 有向图,后继状态也就是后继结点,通过转移图上结点的状态最后求的起始点的结果。

以往 都是选择某一堆 取走若干 然后留下一堆,所以 结点的后继结点就对应剩下的那个状态。也就是说 把每一堆单独作为一个 NIM游戏,最后再考虑所有堆最后异或的结果。

不过 这里是选择某一堆 然后分成若干个小堆,就相当于又变成了一个 NIM游戏,不过考虑到分成的若干小堆 有相同的 和 不同的两部分,相同的直接判奇偶即可,如果有不同的(即分剩下的)再异或上这种状态即可。也就是先把每一堆作为一个 NIM 游戏,每分一次就又玩一次 NIM 游戏。

 

AC code:

 1 #include <bits/stdc++.h>
 2 #define inc(i, j, k) for(int i = j; i <= k; i++)
 3 #define rep(i, j, k) for(int i = j; i < k; i++)
 4 #define F(x) ((x)/3+((x)%3==1?0:tb))
 5 #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
 6 #define INF 0x3f3f3f3f
 7 #define LL long long
 8 #define MEM(i, j) memset(i, j, sizeof(i));
 9 #define gcd(i, j) __gcd(i, j)
10 using namespace std;
11 const int MAXN = 2e5+10;
12 const int MM = 2e3+10;
13 int sg[MAXN];
14 int vis[MAXN];
15 
16 void getsg()
17 {
18     int cnt, res;
19     sg[1] = 0;
20     MEM(vis, -1);
21     inc(i, 2, MM){
22         rep(j, 1, i){
23             cnt = i/j;
24             res = i%j;
25             if(cnt%2) vis[sg[res]^sg[j]] = i;
26             else vis[sg[res]] = i;
27         }
28         int k = 0;
29         while(vis[k] == i && k < MM) k++;
30         sg[i] = k;
31     }
32 }
33 
34 int main()
35 {
36     getsg();
37     int N;
38     scanf("%d", &N);
39     int ans = 0, tp;
40     while(N--){
41         scanf("%d", &tp);
42         ans^=sg[tp];
43     }
44     if(ans) puts("First");
45     else puts("Second");
46     return 0;
47 }
View Code

 

 

标签:customer,CTU,Contest,casino,tokens,player,game,pile,SG
来源: https://www.cnblogs.com/ymzjj/p/10731877.html