HDU 3537 daizhenyang's coin (game-SG)

Source: Internet
Author: User

Daizhenyang's coin
Problem descriptionwe know that daizhenyang is chasing a girlfriend. as we all know, whenever you chase a beautiful girl, there'll always be an opponent, or a rival. in order to take one step ahead in this chasing process, daizhenyang decided to prove to the girl that he's better and more intelligent than any other chaser. so he arranged a simple game: coin flip game. he invited the girl to be the judge.
In this game, n coins are set in a row, where n is smaller than 10 ^ 8. they took turns to flip coins, to flip one coin from head-up to tail-up or the other way around. each turn, one can choose 1, 2 or 3 Coins to flip, but the rightmost selected must be head-up before flipping operation. if one cannot make such a flip, he lost.
As we all know, daizhenyang is a very smart guy (he's famous for his 26 problems and graph theory unified theory-network flow does it all ). so he will always choose the optimal strategy to win the game. and it's a very bad news for all the competitors.
But the girl did not want to see that happen so easily, because she's not sure about her feelings towards him. so she wants to make daizhenyang lose this game. she knows daizhenyang will be the first to play the game. your task is to help her determine whether her arrangement is a losable situation for daizhenyang.
For simplicity, you are only told the position of head-up coins. and due to the girl's complicated emotions, the same coin may be described twice or more times. the other coins are tail-up, of course.
Coins are numbered from left to right, beginning with 0.
 
Inputmultiple test cases, for each test case, the first line contains only one integer N (0 <= n <= 100), representing the number of head-up coins. the second line has n integers A1, A2... An (0 <= ak <10 ^ 8) indicating the An-th coin is head up.
 
Outputoutput a line for each test case, if it's a losable situation for daizhenyang can, print "yes", otherwise output "no" instead.
 
Sample Input
01040 1 2 3
 
Sample output
YesNoYes
 
Source2010 ACM-ICPC multi-university training Contest (11) -- host by BUPT

Question:

There is a row of coins that tell you the positions of n front-up coins. You can choose any position 1 ~ Flip the three coins, but ask if your first hand will lose.


Solution:

Find rule by SG

SG 1 2 4 7 8 11 13 14

X 0 1 2 3 4 5 6 7

Find the rule. sg [X]. If the number of binary 1 of X is odd, SG [x] = 2 * X; otherwise, SG [x] = 2 * x + 1;

Then, the SG values are different or ultimately the answer.


Solution code:

#include <iostream>#include <cstdio>#include <set>using namespace std;int sg(int x){    int ans=0,tmp=x;    while( x>0 ){        if( (x&1) ) ans++;        x/=2;    }    if( (ans&1) ) return 2*tmp;    else return 2*tmp+1;}int main(){    int n,x;    while(scanf("%d",&n)!=EOF){        int ans=0,x;        set <int> mys;        for(int i=0;i<n;i++){            scanf("%d",&x);            mys.insert(x);        }        for(set <int>::iterator it=mys.begin();it!=mys.end();it++){            ans^=sg(*it);        }        if(ans==0) printf("Yes\n");        else printf("No\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.