poj3480 John (nim博弈变形--SJ定理)

John

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2940   Accepted: 1432

Description

Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to eat several M&Ms of the same color. Then his opponent has to make a turn. And so on. Please note that each player
has to eat at least one M&M during his turn. If John (or his brother) will eat the last M&M from the box he will be considered as a looser and he will have to buy a new candy box.

Both of players are using optimal game strategy. John starts first always. You will be given information about M&Ms and your task is to determine a winner of such a beautiful game.

Input

The first line of input will contain a single integer T – the number of test cases. Next T pairs of lines will describe tests in a following format. The first line of each
test will contain an integer N – the amount of different M&M colors in a box. Next line will contain N integers Ai, separated by
spaces – amount of M&Ms of i-th color.

Constraints:

1 <= T <= 474,

1 <= N <= 47,

1 <= Ai <= 4747

Output

Output T lines each of them containing information about game winner. Print “John” if John will win the game or “Brother” in other case.

Sample Input

2
3
3 5 1
1
1

Sample Output

John
Brother

Source

Southeastern Europe 2007

题意:在一般的nim博弈上稍作修改,就是最后取完的输,其他一样;

分析:这就牵扯到一个牛逼的定理了....SJ定理,然后就和基础nim一样了。

我们提出定理里的两个限制:1、SG函数为不为0。2、有没有某单一游戏的SG函数大于1。

通过这两个限制,我们可以组合出4种情况:

(1)SG==0,有某单一游戏的SG>1。

(2)SG!=0,有某单一游戏的SG>1。(必胜SJ)

(3)SG==0,无某单一游戏的SG>1。(必胜SJ)

(4)SG!=0,无某单一游戏的SG>1。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9+7;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define N 50010
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main()
{
    int T,n,x;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int ans=0,cnt=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&x);
            ans ^= x;
            if(x > 1) cnt++;
        }
        if(ans==0)
        {
            if(cnt) cout<<"Brother"<<endl;
            else cout<<"John"<<endl;
        }
        else
        {
            if(cnt) cout<<"John"<<endl;
            else cout<<"Brother"<<endl;
        }
    }
    return 0;
}

时间: 2024-08-04 03:26:54

poj3480 John (nim博弈变形--SJ定理)的相关文章

HDU 1907 Nim博弈变形

1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形.设糖果数为1的叫孤独堆,糖果数大于1的叫充裕堆,设状态S0:a1^a2^..an!=0&&充裕堆=0,则先手必败(奇数个为1的堆,先手必败).S1:充裕堆=1,则先手必胜(若剩下的n-1个孤独堆个数为奇数个,那么将那个充裕堆全部拿掉,否则将那个充裕堆拿得只剩一个,这样的话先手必胜).T0:a1

BZOJ1022: [SHOI2008]小约翰的游戏John(Nim博弈)

Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到最后一粒石子的人算输.小约翰相当固执,他坚持认为先取的人有很大的优势,所以他总是先取石子,而他的哥哥就聪明多了,他从来没有在游戏中犯过错误.小约翰一怒之前请你来做他的参谋.自然,你应该先写一个程序,预测一下谁将获得游戏的胜利. Input 本题的输入由多组数据组成第一行包括一个整数T,

HDU 3032 (Nim博弈变形) Nim or not Nim?

博弈的题目,打表找规律还是相当有用的一个技巧. 这个游戏在原始的Nim游戏基础上又新加了一个操作,就是游戏者可以将一堆分成两堆. 这个SG函数值是多少并不明显,还是用记忆花搜索的方式打个表,规律就相当显然了. 1 #include <cstdio> 2 #include <cstring> 3 4 const int maxn = 100; 5 int sg[maxn * 2]; 6 bool vis[maxn * 2]; 7 8 int mex(int v) 9 { 10 if(

hdu 1730 Northcott Game (nim博弈变形)

# include <algorithm> # include <stdio.h> # include <string.h> # include <math.h> # include <iostream> using namespace std; int main() { int n,m,i,a,b,cot; while(~scanf("%d%d",&n,&m)) { cot=0; for(i=0; i<

Nowcoder 挑战赛23 B 游戏 ( NIM博弈、SG函数打表 )

题目链接 题意 : 中文题.点链接 分析 : 前置技能是 SG 函数.NIM博弈变形 每次可取石子是约数的情况下.那么就要打出 SG 函数 才可以去通过异或操作判断一个局面的胜负 打 SG 函数的时候.由于 N 很大 所以不能使用递归的方式打表.会爆栈 还有要预处理每个数的约数 打出 SG 函数之后 暴力判断初始局面的每堆石子取走约数后是否对答案产生贡献 #include<bits/stdc++.h> #define LL long long #define ULL unsigned long

POJ 3480 John(SJ定理博弈)题解

题意:n堆石头,拿走最后一块的输 思路:SJ定理:先手必胜当且仅当:(1)游戏的SG函数不为0且游戏中某个单一游戏的SG函数大于1:(2)游戏的SG函数为0且游戏中没有单一游戏的SG函数大于1. 参考:[博弈]Anti,Multi,Every-SG 代码: #include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<vector&g

[BZOJ1022] [SHOI2008] 小约翰的游戏John (SJ定理)

Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取,我们规定取到最后一粒石子的人算输.小约翰相当固执,他坚持认为先取的人有很大的优势,所以他总是先取石子,而他的哥哥就聪明多了,他从来没有在游戏中犯过错误.小约翰一怒之前请你来做他的参谋.自然,你应该先写一个程序,预测一下谁将获得游戏的胜利. Input 本题的输入由多组数据组成第一行包括一个整数T,

POJ 3480 &amp; HDU 1907 John(尼姆博弈变形)

题目链接: PKU:http://poj.org/problem?id=3480 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1907 Description Little John is playing very funny game with his younger brother. There is one big box filled with M&Ms of different colors. At first John has to e

【HDU 3590】 PP and QQ (博弈-Anti-SG游戏,SJ定理,树上删边游戏)

PP and QQ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 510    Accepted Submission(s): 256 Problem Description PP and QQ were playing games at Christmas Eve. They drew some Christmas trees on