zoukankan      html  css  js  c++  java
  • LightOJ1199 Partition Game

    Alice and Bob are playing a strange game. The rules of the game are:

    1. Initially there are n piles.
    2. A pile is formed by some cells.
    3. Alice starts the game and they alternate turns.
    4. In each tern a player can pick any pile and divide it into two unequal piles.
    5. If a player cannot do so, he/she loses the game.

    Now you are given the number of cells in each of the piles, you have to find the winner of the game if both of them play optimally.

    Input

    Input starts with an integer T (≤ 1000), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 100). The next line contains n integers, where the ith integer denotes the number of cells in the ith pile. You can assume that the number of cells in each pile is between 1 and 10000.

    Output

    For each case, print the case number and 'Alice' or 'Bob' depending on the winner of the game.

    Sample Input

    3

    1

    4

    3

    1 2 3

    1

    7

    Sample Output

    Case 1: Bob

    Case 2: Alice

    Case 3: Bob

    题解:对于每一个数下一个拆分为 (1,n-1),(2,n-2) ...  个

    SG函数推到即可;

    参考代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 const int maxn=10010;
     5 int T,n,SG[maxn],x,ans,vis[maxn]; 
     6 void getSG(int n)
     7 {
     8     for(int i=1;i<=n;++i)
     9     {
    10         memset(vis,0,sizeof vis);
    11         for(int j=1;j*2<i;++j) if(i!=j*2) vis[SG[j]^SG[i-j]]=1;
    12         for(int j=0;j<maxn;++j){ if(!vis[j]) { SG[i]=j;break; } }
    13     }
    14 }
    15 int main()
    16 {
    17     scanf("%d",&T);
    18     getSG(maxn);
    19     for(int cas=1;cas<=T;++cas)
    20     {
    21         scanf("%d",&n);ans=0;
    22         for(int i=1;i<=n;++i)
    23         {
    24             scanf("%d",&x);
    25             ans^=SG[x];
    26         }
    27         if(ans) printf("Case %d: Alice
    ",cas);
    28         else printf("Case %d: Bob
    ",cas);
    29     }
    30     return 0;    
    31 } 
    View Code
  • 相关阅读:
    Linux嵌入式 -- 内核
    Linux嵌入式 -- 内核
    utf8和utf8mb4区别
    二叉树的实现
    python资源大全2
    树与树算法
    二叉树
    70.最小生成树
    68.营救问题(广搜)
    67.迷宫问题(广搜)
  • 原文地址:https://www.cnblogs.com/csushl/p/10388784.html
Copyright © 2011-2022 走看看