Lightoj 1296 Again Stone Game (SG function)

Source: Internet
Author: User
Tags cas

Alice and Bob are playing a stone game. Initially there is n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player have to select any pile and should remove at least one and no more than half stones from that pile. So, for example if a pile contains of stones, then a player can take at least 1 and at the most 5 stones from that pile. If A pile contains 7 stones; At the most 3 stones from this pile can be removed.

Both Alice and Bob play perfectly. The player cannot make a valid move loses. Now you is given the information of the piles and the number of stones in all the piles, you have to find the player who Would win if both play optimally.

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

Each case is starts with a line containing an integer n (1≤n≤1000). The next line contains n space separated integers ranging in [1, 109]. The ith integer in this line denotes the number of stones in the ith pile.

Output
For each case, print the case number and the name of the "who'll win the game."

Sample Input
5
1
1
3
10 11 12
5
1 2 3) 4 5
2
4 9
3
1 3 9
Sample Output
Case 1:bob
Case 2:alice
Case 3:alice
Case 4:bob
Case 5:alice Test Instructions

There are n heap of stones, Alice Initiator, the two sides take turns, each time can be taken not more than half of the stone walk, who can not take the idea of who lose

Data range is very large, direct hit the table certainly not, that must have the law, then we first dozen SG value to look for the law

int sg[35],vis[35];
void Getsg ()
{
    int i,j;
    memset (sg,0,sizeof (SG));
    for (I=1; i<=30; i++)
    {
        memset (vis,0,sizeof (Vis));
        for (j=1; j<=i/2;j++)//j The number of stones that can be taken each time
            Vis[sg[i-j]]=1;//sg[i-j] indicates the successor status for
        (j=0; j<=30; j + +)
        {
            if (vis[j]==0)
            {
                sg[i]=j;
                Break;}}}}

Sg[1]=0
Sg[2]=1
Sg[3]=0
sg[4]=2
Sg[5]=1
Sg[6]=3
Sg[7]=0
Sg[8]=4
sg[9]=2
Sg[10]=5
Sg[11]=1
Sg[12]=6
Sg[13]=3
Sg[14]=7
Sg[15]=0
Sg[16]=8
Sg[17]=4
Sg[18]=9
sg[19]=2
sg[20]=10
Sg[21]=5
sg[22]=11
Sg[23]=1
Sg[24]=12
Sg[25]=6
Sg[26]=13
Sg[27]=3
Sg[28]=14
Sg[29]=7
Sg[30]=15
We find that the SG value of even I is divided by 2, which is SG[I]=I/2, and the SG value of odd i is the SG value of the number divided by 2, i.e. SG[I/2], so we can write the recursive function of the SG

 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <
string.h> #include <algorithm> #include <math.h> using namespace std;
    int sg (int n) {if (n%2==0) return N/2;
else SG (N/2);
    } int main () {int t,cas=1;
    scanf ("%d", &t);
        while (t--) {int n;
        scanf ("%d", &n);
        int ans=0;
            for (int i=0;i<n;i++) {int x;
            scanf ("%d", &x);
        ANS^=SG (x);
        } printf ("Case%d:", cas++);
        if (ans==0) printf ("bob\n");
    else printf ("alice\n");
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.