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 / 2);
}

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		long long ans = 0;
		for (int i = 0; i < n; i++) {
			scanf("%lld", &num);
			ans ^= SG(num);
  		}
  		printf("%s\n", ans == 0 ? "NO" : "YES");
 	}
	return 0;
}

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

时间: 2024-09-29 13:00:03

UVA 1482 - Playing With Stones(SG打表规律)的相关文章

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

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> #

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 witha1, a2, a3,..., aN number of stones. On each turn, a player must remove at least one stone from one

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.

【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

hdu 2147 kiki&#39;s game(DP(SG)打表找规律)

题意: n*m的棋盘,一枚硬币右上角,每人每次可将硬币移向三个方向之一(一格单位):左边,下边,左下边. 无法移动硬币的人负. 给出n和m,问,先手胜还是后手胜. 数据范围: n, m (0<n,m<=2000) 思路: dp[i][j]=0,说明从(i,j)这个点到时左下角先手败.dp[i][j]=1则先手胜. 然后记忆搜.但是记忆搜会超时. 搜完把整张表打出来,发现规律了,,,,然后,,,代码剩几行了. 代码: ///打表观察 /* int f[2005][2005]; int go(in

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

UVA 11927 - Games Are Important(sg函数)

UVA 11927 - Games Are Important 题目链接 题意:给定一个有向图,结点上有一些石头,两人轮流移动石头,看最后谁不能移动就输了,问先手还后手赢 思路:求出每个结点的sg函数,然后偶数个石头结点可以不用考虑,因为对于偶数情况,总步数肯定能保证是偶数,所以只要考虑奇数情况的结点 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <vector&g