Hdoj 5088 revenge of Nim ii bit operation

Source: Internet
Author: User


Bitwise operation ....

Revenge of Nim II Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 229 accepted submission (s): 79


Problem descriptionnim is a mathematical game of strategy in which two 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.
--- Wikipedia

Today, Nim takes revenge on you, again. as you know, the rule of Nim game is rather unfair, only the Nim-sum (percentage) of the sizes of the heaps is zero will the first player lose. to ensure the fairness of the game, the second player has a chance to move some (can be zero) heaps before the game starts, but he has to move one heap entirely, I. e. not partially. of course, he can't move all heaps out, at L East one heap shoshould be left for playing. Will the second player have the chance to win this time?
Inputthe first line contains a single integer T, indicating the number of test cases.

Each test case begins with an integer N, indicating the number of heaps. Then n integer AI follows, indicating the number of each heap.

[Technical Specification]
1. 1 <= T <= 100
2. 1 <= n <= 1 000
3. 1 <= AI <= 1 000 000 000 000
Outputfor each test case, output "yes" if the second player can win by moving some (can be zero) heaps out, otherwise "no ".
Sample Input
31232 2 251 2 3 4 5
 
Sample output
NoYesYesHintFor the third test case, the second player can move heaps with 4 and 5 objects out, so the nim-sum of the sizes of the left heaps is 1⊕2⊕3 = 0. 
 
Sourcebestcoder round #16

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long int LL;int n;LL c[60];LL a[1100];int main(){    int T_T;    scanf("%d",&T_T);    while(T_T--)    {        memset(c,0,sizeof(c));        scanf("%d",&n);        for(int i=1;i<=n;i++)            cin>>a[i];        bool flag=false;        for(int i=1;i<=n;i++)        {            for(int j=60;j>=0;j--)            {                if(c[j]==0&&(a[i]&(1LL<<j)))                {                    c[j]=a[i];                    break;                }                else if(a[i]&(1LL<<j))                {                    a[i]^=c[j];                    if(a[i]==0) flag=true;                }            }        }        if(flag==true) puts("Yes");        else puts("No");    }    return 0;}



Hdoj 5088 revenge of Nim ii bit operation

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.