HDU Nim or not Nim? (Nim,sg函数)

题意:给出几堆石子数量,每次可以取走一堆中任意数量的石头,也可以将一堆分成两堆,而不取。最后取走者胜。

思路:石子数量很大,不能直接算,sg打表找出规律:正常情况下a[i]=i,但是有例外的,就是i%4=0和i%4=3的sg值是交换了的,所以要算某个状态的sg值时,若模4为0,则进行自减,若模4为3则进行自加,这样就得到了sg值。最后再求全部异或和。若0,则先手输。否则先手胜。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=105, limit=1004;
 4 int a[N],n,sg[limit]={0,1,2};
 5 bool B[limit];
 6 int main()
 7 {
 8     //freopen("input.txt", "r", stdin);
 9     for(int i=3; i<limit; i++)
10     {
11         memset(B,0,sizeof(B));
12         for(int j=0; j<i; j++)    B[sg[j]]=1;
13         int up=i/2;
14         for(int j=1; j<=up; j++)
15             B[sg[j]^sg[i-j]]=1;
16         for(int j=0;; j++)
17             if(!B[j])
18             {
19                 sg[i]=j;
20                 break;
21             }
22     }
23     for(int i=0; i<100; i++)
24         cout<<sg[i]<<" ";
25     cout<<endl;
26     return 0;
27 }

打表代码

 1 #include <stdio.h>
 2 int a, n, t, ans, i;
 3 int main()
 4 {
 5     scanf("%d",&t);
 6     while(t--)
 7     {
 8         scanf("%d",&n);ans=0;
 9         while(n--)
10         {
11             scanf("%d",&a);
12             if(a%4==0)    --a;
13             else if(a%4==3)    ++a;
14             ans^=a;
15         }
16         if(ans)    printf("Alice\n");
17         else    printf("Bob\n");
18     }
19     return 0;
20 }

AC代码

时间: 2024-08-29 01:45:23

HDU Nim or not Nim? (Nim,sg函数)的相关文章

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

[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

HDU 1524 - A Chess Game(sg函数 + dfs)

A Chess Game Problem Description Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed

codeforces 768 E 变形NIM博弈/手写sg函数

题意:给你n堆石头,在同一堆石头下不能取两次相同的数目,问能否后手胜 题解:设一堆石头最多能取k次不同的石头数目,有nim博弈可以知道只要每一堆石头能取的次数异或起来为0则为必败局,则YES #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <map> #include <queue> #include <ve

HDU 3032 Nim or not Nim?(sg函数)

题目链接 暴力出来,竟然眼花了以为sg(i) = i啊....看表要认真啊!!! #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define LL __int64 int dp[10001]; int sg(int x) { int flag[10001],temp,i; if(dp[x] >= 0) return dp[x]; memset(flag,

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): 1056    Accepted Submission(s): 523 Problem Description Nim is a two-player mathematic game of strategy in which players take turn

HDU 3032 Nim or not Nim? (sg函数求解)

Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provide

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 博弈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 3032 Nim or not Nim?(sg函数博弈)

题目地址:HDU 3032 这题是很好用来练习sg函数打表的一题. 下面是sg函数值打表代码: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include &