HDU 3537 Daizhenyang's Coin

来源:互联网 发布:酷炫爆炸js网站效果 编辑:程序博客网 时间:2024/05/29 05:56

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3537


题意:翻硬币游戏,如果一枚硬币正面朝上,你可以将它翻过来,并且自选0~2颗它左边的硬币,改变它们的状态(正反交替)。谁不能翻谁输。


思路:定义硬币全反时的sg为0,现在单个游戏就是只有一枚硬币朝上,其余都是反,其他情况都可以由单一游戏组合而成。如果位置i有一个正面朝上的硬币,其余都是反,那么这枚硬币翻过来的时候,它可以到达的局面是1、sg = 0   2、sg(j)j位置的硬币朝上,其余都是反(j<i) 3、出现两个正面朝上的硬币,变成组合游戏sg(j)和sg(k)。我们把sg函数打表出来就可以找到规律。

hihocoder上有这道题的简化版,讲的比较详细

http://hihocoder.com/problemset/problem/1172


#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <iostream>#include <algorithm>#include <stack>#include <map>#include <set>#include <vector>#include <sstream>#include <queue>#include <utility>using namespace std;#define rep(i,j,k) for (int i=j;i<=k;i++)#define Rrep(i,j,k) for (int i=j;i>=k;i--)#define Clean(x,y) memset(x,y,sizeof(x))#define LL long long#define ULL unsigned long long#define inf 0x7fffffff#define mod 100000007const int maxn = 1009;int sg[maxn];map<LL,int> flag;LL cal( LL x ){    int ans = 0;    LL temp = x;    while( temp )    {        if ( temp & 1 )  ans++;        temp >>= 1;    }    if ( ans & 1 ) return x * 2;    return x*2+1;}void init(){    bool f[maxn];    sg[0] = 0;    rep(i,1,100)    {        Clean(f,false);        rep(j,0,i-1) f[ sg[j] ] = true;        rep(j,0,i-1)            rep( k,0,i-1 )            if ( j != k )                f[ sg[j] ^ sg[k] ] = true;        rep(j,0,maxn-1)            if ( !f[ j ] )            {                sg[i] = j;                break;            }        printf("%d : %d   %I64d\n",i-1,sg[i] , cal(i-1));    }}int main(){    //init();    LL n,temp;    LL ans;    while( scanf("%I64d",&n) == 1 )    {        flag.clear();        ans = 0;        rep(i,1,n)        {            scanf("%I64d",&temp);            if ( !flag[temp] )                ans ^= cal( temp );            flag[temp] = 1;        }        puts( ans==0 ? "Yes" : "No" );    }    return 0;}


0 0
原创粉丝点击