UVA1482:Playing With Stones(SG)

Description

You and your friend are playing a game in which you and your friend take turns removing stones from piles. Initially there are N piles witha1a2a3,..., aN number
of stones. On each turn, a player must remove at least one stone from one pile but no more than half of the number of stones in that pile. The player who cannot make any moves is considered lost. For example, if there are three piles with 5, 1 and 2 stones,
then the player can take 1 or 2 stones from first pile, no stone from second pile, and only 1 stone from third pile. Note that the player cannot take any stones from the second pile as 1 is more than half of 1 (the size of that pile). Assume that you and your
friend play optimally and you play first, determine whether you have a winning move. You are said to have a winning move if after making that move, you can eventually win no matter what your friend does.

Input

The first line of input contains an integer T(T100) denoting
the number of testcases. Each testcase begins with an integer N(1N100) the
number of piles. The next line contains N integers a1a2a3,..., aN(1ai* 1018) the
number of stones in each pile.

Output

For each testcase, print ``YES" (without quote) if you have a winning move, or ``NO" (without quote) if you don?t have a winning move.

Sample Input

4
2
4 4
3
1 2 3
3
2 4 6
3
1 2 1

Sample Output

NO
YES
NO
YES


打表找规律得出SG表

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8

LL SG(LL x)
{
    if(x&1)
        return SG(x/2);
    return x/2;
}

int main()
{
    int n,t,i,j;
    LL a,ans;
    scanf("%d",&t);
    while(t--)
    {
        ans = 0;
        scanf("%d",&n);
        for(i = 0; i<n; i++)
        {
            scanf("%lld",&a);
            ans^=SG(a);
        }
        if(ans) printf("YES\n");
        else printf("NO\n");
    }

    return 0;
}

时间: 2024-08-25 23:34:51

UVA1482:Playing With Stones(SG)的相关文章

UVA 1482 - Playing With Stones(SG打表规律)

UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个,不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值很大,无法直接递推sg函数,打出前30项的sg函数找规律 代码: #include <stdio.h> #include <string.h> int t, n; long long num; long long SG(long long x) { return x % 2 == 0 ? x : SG(x /

UVALive 5059 C - Playing With Stones 博弈论Sg函数

C - Playing With Stones Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVALive 5059 Description You and your friend are playing a game in which you and your friend take turns removing stones from piles.

uva 1482 - Playing With Stones(Nim游戏)

题目链接: uva 1482 - Playing With Stones 题目大意:n堆石子,给定每堆石子的个数,两个人分别从操作,每次可以从一堆中取走至少一个石子,但是不能超过一半.如果不能操作则视为失败. 解题思路:对于每一堆式子来说,可以看作一个Nim游戏,但是SG(x)并不等于x,因为每次取石子不能超过一半,所以对于偶数SG(x)=x/2,对于奇数SG(x)=SG(x/2). 所以去Nim和即可. #include <cstdio> #include <cstring> #

UVA - 1482 Playing With Stones

Description You and your friend are playing a game in which you and your friend take turns removing stones from piles. Initially there are N piles with a1, a2, a3,..., aN number of stones. On each turn, a player must remove at least one stone from on

UVALive 5059 Playing With Stones(求sg函数)

题意和nim游戏差不多,就是取石子的时候最多只能拿原来的一半,比如一堆5个石子最多拿两个. 先用打表的方式看出前面一部分的sg值,然后找规律来做. 打表求sg值的程序才是最重要的. #include<cstdio> #include<cstring> #define ll long long int main() { int vis[50]; int sg[50]; sg[1] = 0; for(int i = 2; i <= 30; i++) { memset(vis, 0

LA 5059 (找规律 SG函数) Playing With Stones

题意: 有n堆石子,两个人轮流取,每次只能取一堆的至少一个至多一半石子,直到不能取为止. 判断先手是否必胜. 分析: 本题的关键就是求SG函数,可是直接分析又不太好分析,于是乎找规律. 经过一番“巧妙”的分析,有这样一个规律: 如果n是偶数,SG(n) = n / 2; 如果n是奇数,SG(n) = SG(n / 2); 这道题的意义不在于规律是什么,而是要自己能够写出求SG函数值的代码.顺便再体会一下mex(S)的含义. 1 #include <cstring> 2 3 const int

【LA5059】Playing With Stones (SG函数)

题意:有n堆石子,分别有a[i]个.两个游戏者轮流操作,每次可以选一堆,拿走至少一个石子,但不能拿走超过一半的石子. 谁不能拿石子就算输,问先手胜负情况 n<=100,1<=a[i]<=2e18 思路:打表找SG函数的规律 当n为偶数时,SG(n)=n/2 当n为奇数时,SG(n)=SG(n/2) 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algo

石油大训练 Little Sub and Johann (博弈SG打表找规律)

Little Sub and Johann 题目描述 Little Sub and Johann are good friends and they often play games together. Recently, they like playing with stones.They have n piles of stones initially and they should make one of following movements by turns:1.Erase a pil

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y