【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<algorithm>
 5 #include<cmath>
 6 typedef long long ll;
 7 using namespace std;
 8 #define N   110
 9 #define oo  10000000
10 #define MOD 1000000007
11
12 ll sg(ll x)
13 {
14     if(x&1) return sg(x/2);
15     return x/2;
16 }
17
18 int main()
19 {
20     int cas;
21     scanf("%d",&cas);
22     while(cas--)
23     {
24         int n;
25         scanf("%d",&n);
26         ll ans=0;
27         for(int i=1;i<=n;i++)
28         {
29             ll x;
30             scanf("%lld",&x);
31             ans^=sg(x);
32         }
33         if(ans) printf("YES\n");
34          else printf("NO\n");
35     }
36     return 0;
37 }
38     

原文地址:https://www.cnblogs.com/myx12345/p/9952452.html

时间: 2024-12-22 18:36:37

【LA5059】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 /

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

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

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.

Light OJ 1296 - Again Stone Game (博弈sg函数递推)

F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the

hdu 2999 sg函数(简单博弈)

Stone Game, Why are you always there? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 393    Accepted Submission(s): 132 Problem Description "Alice and Bob are playing stone game...""E

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

poj 2960 S-Nim(SG函数)

S-Nim Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3694   Accepted: 1936 Description Arthur and his sister Caroll have been playing a game called Nim for some time now. Nim is played as follows: The starting position has a number of h

S-Nim(hdu1536+SG函数)

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5317    Accepted Submission(s): 2288 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now