zoukankan      html  css  js  c++  java
  • BZOJ 1022 小约翰的游戏

    Description

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

    Input

    本题的输入由多组数据组成,第一行包括一个整数T,表示输入总共有T组数据(T≤500)。每组数据的第一行包括一个整数N(N≤50),表示共有N堆石子,接下来有N个不超过5000的整数,分别表示每堆石子的数目。

    Output

    每组数据的输出占一行,每行输出一个单词。如果约翰能赢得比赛,则输出“John”,否则输出“Brother”,请注意单词的大小写。

    Sample Input

    2
    3
    3 5 1
    1
    1

    Sample Output

    John
    Brother

    HINT

    【数据规模】

    对于40%的数据,T ≤ 250。

    对于100%的数据,T ≤ 500。

    裸的anti-Nim游戏,结论:若石子堆全为1,根据奇偶性判断(偶数先手胜,否则后手胜);若有一个不等于一,根据异或值得到答案。
    证明:戳这里(2009贾志豪论文,提取码:1119)。
     1 #include<cstdio>
     2 #include<cstdlib>
     3 using namespace std;
     4 
     5 int ans,one,n;
     6 
     7 int main()
     8 {
     9     freopen("1022.in","r",stdin);
    10     freopen("1022.out","w",stdout);
    11     int T; scanf("%d",&T);
    12     while (T--)
    13     {
    14         ans = one = 0; int a;
    15         scanf("%d",&n);
    16         for (int i = 1;i <= n;++i) scanf("%d",&a),ans ^= a,one += (a == 1);
    17         if (one == n) printf("%s
    ",(one&1)?"Brother":"John");
    18         else printf("%s
    ",ans?"John":"Brother");
    19     }
    20     fclose(stdin); fclose(stdout);
    21     return 0;
    22 }
    View Code
     
  • 相关阅读:
    Kettle使用介绍——Kettle的安装与基本使用
    Every Tom,Dick and Harry. 不管张三李四。
    AOP
    Redis 常用命令学习一:通用的基本命令
    Python 解LeetCode:23. Merge k Sorted Lists
    Python 解LeetCode:33. Search in Rotated Sorted Array
    Python 解leetcode:48. Rotate Image
    小米Python后端面试题
    Python 解leetcode:49. Group Anagrams
    Python 解leetcode:3. Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/mmlz/p/4270059.html
Copyright © 2011-2022 走看看