HDU 3032.Nim or not Nim?【SG函数打表找规律】【4月11】

来源:互联网 发布:淘宝退货会影响权重吗 编辑:程序博客网 时间:2024/06/05 20:49

Nim or not Nim?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1436    Accepted Submission(s): 706


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
232 2 323 3
 

Sample Output
AliceBob
 
理解nim跟sg,然后打表找一下规律:

sg[4*k] = 4*k - 1;

sg[4*k +1]  = 4*k + 1;

sg[4*k + 2] = 4*k + 2;

sg[4*k + 3] = 4*k - 1;

#include<iostream>#include<cstdio>using namespace std;int T, N, x, ans;int main(){    scanf("%d", &T);    while(T--)    {        ans = 0;        scanf("%d", &N);        while(N--)        {            scanf("%d", &x);            if(x%4 == 0) ans ^= (x-1);            else if(x%4 == 3) ans ^= (x+1);            else ans ^= x;        }        if(ans == 0) cout <<"Bob"<< endl;        else cout <<"Alice"<< endl;    }    return 0;}


0 0
原创粉丝点击