UVA1482 LA5059 Playing With StonesSG函数
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA1482 LA5059 Playing With StonesSG函数相关的知识,希望对你有一定的参考价值。
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 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 (T ≤ 100) denoting the number of testcases. Each testcase begins with an integer N (1 ≤ N ≤ 100) the number of piles. The next line contains N integers a1, a2, a3, . . . , aN (1 ≤ ai ≤ 2 ∗ 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
问题链接:UVA1482 LA5059 Playing With Stones
问题简述:给定n堆石头,每次从其中一堆取石头,最多其一堆的一半石头,至少取一个石头。问是否先手必胜?
问题分析:SG函数问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA1482 LA5059 Playing With Stones */
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL SG(LL n)
{
return (n & 1LL) ? SG(n / 2) : n / 2;
}
int main()
{
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
LL a, ans = 0;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a);
ans ^= SG(a);
}
puts(ans ? "YES" : "NO");
}
return 0;
}
以上是关于UVA1482 LA5059 Playing With StonesSG函数的主要内容,如果未能解决你的问题,请参考以下文章