HDU 5794 - A Simple Nim

题意:
    n堆石子,先拿光就赢,操作分为两种:
        1.任意一堆中拿走任意颗石子
        2.将任意一堆分成三小堆 ( 每堆至少一颗 )
        
分析:
    答案为每一堆的SG函数值异或和.
    故先打表寻找单堆SG函数规律.
    其中,若 x 可分为 三堆 a,b,c ,则 SG[x] 可转移至子状态 SG[a] ^ SG[b] ^ SG[c]  (三堆SG值异或和)
    
    打表后发现:
        SG[ 8*k - 1 ] = 8*k
        SG[ 8*k ] = 8*k - 1
        其余 SG[x] = x;
    
    则可直接得出答案

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int MAXN = 1000005;
 6 int T, n;
 7 int main()
 8 {
 9     scanf("%d", &T);
10     while ( T-- )
11     {
12         scanf("%d", &n);
13         int sg = 0;
14         for (int i = 1; i <= n; i++)
15         {
16             int x; scanf("%d", &x);
17             if (x % 8 == 0) sg ^= (x - 1) ;
18             else if ( (x + 1) % 8 == 0) sg ^= (x + 1) ;
19             else sg ^= x;
20         }
21         if(sg) puts("First player wins.");
22         else puts("Second player wins.");
23     }
24 }
 1 /*
 2 SG打表
 3 */
 4 #include <iostream>
 5 #include <cstring>
 6 using namespace std;
 7 int sg[105];
 8 int GetSG(int x)
 9 {
10     if (sg[x] != -1) return sg[x];
11     int vis[105];
12     memset(vis, 0, sizeof(vis));
13     for (int i = 0;i < x; i++)
14         vis[GetSG(i) ] = 1;
15     int a,b,c;
16     for(a = 1; a <= x; a++)
17         for(b = a; b <= x - a; b++)
18             for(c = b; c <= x - a - b; c++)
19                 if(a+b+c == x)
20                 vis[GetSG(a) ^ GetSG(b) ^ GetSG(c)] = 1;
21     for(int i = 0;; i++)
22         if(!vis[i])  return sg[x] = i;
23 }
24 int main()
25 {
26     memset(sg, -1, sizeof(sg));
27     sg[0] = 0;
28     for (int i = 1; i <= 50; i++)
29         if(sg[i] == -1) GetSG(i);
30     for(int i = 0; i <= 50; i++)
31         cout<<i<<" "<<sg[i]<<endl;
32 } 
时间: 2024-12-16 10:20:23

HDU 5794 - A Simple Nim的相关文章

HDU 5794 A Simple Nim 2016多校第六场1003

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5795 题意:给你n堆石子,每一堆有ai石头,每次有两种操作,第一种在任意一堆取出任意数量的石头但不能为0,第二种把一堆石头分成三堆任意数量的石头(不能为0),问是先手赢还是后手赢. 题解:这就是一个Nim游戏!那么Nim游戏一般跟SG函数有关,所以这道题打表找规律即可. 关于SG函数,在这里也说一下吧!毕竟第一次接触. Nim游戏与SG函数:http://baike.baidu.com/link?u

?HDU 5795 A Simple Nim(简单Nim)

HDU 5795 A Simple Nim(简单Nim) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On e

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 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 48 Problem Description Two players take turns picking candies from n heaps,the player who picks the las

hdu 5795 A Simple Nim (sg 博弈)

官方题解: A Simple Nim sg[0]=0 当x=8k+7时sg[x]=8k+8, 当x=8k+8时sg[x]=8k+7, 其余时候sg[x]=x:(k>=0) 打表找规律可得,数学归纳法可证. 具体的打表放在了代码里面 ,详见init函数 /*by*/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace s

hdu 5795 A Simple Nim 博弈sg函数

A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) 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 an

HDU 5794 A Simple Chess (Lucas + dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5794 多校这题转化一下模型跟cf560E基本一样,可以先做cf上的这个题. 题目让你求一个棋子开始在(1,1),只能像马一样走且往右下方走,不经过坏点,有多少种走法能到达(n,m)点. 比如n=6, m=5 有两个坏点,模型转换 如下图: 转换成简单模型之后,只要把棋子可能经过的坏点存到结构体中,按照x与y从小到大排序. dp[i]表示从起点到第i个坏点且不经过其他坏点的方案数. dp[i] = L

HDU 5794 A Simple Chess(杨辉三角+容斥原理+Lucas)

题目链接 A Simple Chess 打表发现这其实是一个杨辉三角…… 然后发现很多格子上方案数都是0 对于那写可能可以到达的点(先不考虑障碍点),我们先叫做有效的点 对于那些障碍,如果不在有效点上,则自动忽略 障碍$(A, B)$如果有效,那么就要进行如下操作: 以这个点为一个新的杨辉三角的顶点,算出目标点的坐标$(x, y)$. 目标点的答案减去$C(A, B) * C(x, y)$的值. 但是这样会造成重复计算,原因是障碍之间可能有相互影响的关系. 这个时候就要考虑容斥原理,DFS消除这

HDU 5794 A Simple Chess(卢卡斯定理 + 容斥原理)

传送门 A Simple Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 667    Accepted Submission(s): 168 Problem Description There is a n×m board, a chess want to go to the position (n,m) from the