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 maxn = 100;
 4 int SG[maxn], vis[maxn];
 5
 6 int main()
 7 {
 8     SG[1] = 0;
 9     for(int i = 2; i <= 30; i++)
10     {
11         memset(vis, 0, sizeof(vis));
12         for(int j = 1; j * 2 <= i; j++) vis[SG[i-j]] = 1;
13         for(int j = 0; ; j++) if(!vis[j])
14         {
15             SG[i] = j;
16             break;
17         }
18         printf("%d ", SG[i]);
19     }
20
21     return 0;
22 }

找规律

 1 #include <cstdio>
 2
 3 long long SG(long long n)
 4 {
 5     if(n & 1) return SG(n >> 1);
 6     else return (n >> 1);
 7 }
 8
 9 int main()
10 {
11     //freopen("in.txt", "r", stdin);
12     int T;
13     scanf("%d", &T);
14     while(T--)
15     {
16         int n;
17         scanf("%d", &n);
18         long long a, x = 0;
19         while(n--)
20         {
21             scanf("%lld", &a);
22             x ^= SG(a);
23         }
24         printf("%s\n", x ? "YES" : "NO");
25     }
26
27     return 0;
28 }

AC

时间: 2024-12-08 19:24:31

LA 5059 (找规律 SG函数) Playing With Stones的相关文章

【博弈论】【SG函数】【找规律】Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) E. Game of Stones

打表找规律即可. 1,1,2,2,2,3,3,3,3,4,4,4,4,4... 注意打表的时候,sg值不只与剩下的石子数有关,也和之前取走的方案有关. //#include<cstdio> //#include<set> //#include<cstring> //using namespace std; //bool vis[16]; //int n,SG[16][1<<16]; //int sg(int x,int moved) //{ // if(SG

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

BZOJ 1228 E&amp;G(sg函数+找规律)

把一对石子堆看出一个子游戏.打出子游戏的sg表找规律.. 这个规律我是一定找不出来的... 对于i,j,如果 (i-1)%pow(2,k+1) < pow(2,k) (j-1)%pow(2,k+1) < pow(2,k) 那么最小的k值就是sg值. # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector

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.

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int

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 /

HDU 5795 A Simple Nim 打表求SG函数的规律

A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).T

HDU 3032 Nim or not Nim?(博弈 SG打表找规律)

传送门 Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1457    Accepted Submission(s): 720 Problem Description Nim is a two-player mathematic game of strategy in which players take

Nim or not Nim? hdu3032 SG值打表找规律

Nim or not Nim? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 858    Accepted Submission(s): 412 Problem Description Nim is a two-player mathematic game of strategy in which players take turns