1. 程式人生 > >UVA 1482 Playing With Stones

UVA 1482 Playing With Stones

esp long png 分享 遞推 div color put fine

技術分享圖片

(藍書裏有這個題貌似)

一言不合就打表,可以發現sg數組是個分形的,所以可以推出遞推式:

1.x是偶數時,sg(x)=x/2

2.否則,sg(x)=sg(x/2)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#define ll long long
using namespace std;
ll sg(ll x){
    return (x&1
)?sg(x>>1):x>>1; } int T,n; ll tot,a; int main(){ scanf("%d",&T); while(T--){ tot=0; scanf("%d",&n); while(n--){ scanf("%lld",&a); tot^=sg(a); } if(tot) puts("YES"); else puts("NO"); }
return 0; }

UVA 1482 Playing With Stones