Nim or not Nim?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2513    Accepted Submission(s): 1300

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 provided they all come from the same heap.

Nim is usually played as a misere game, in which the player to take the last object loses. Nim can also be played as a normal play game, which means that the person who makes the last move (i.e., who takes the last object) wins. This is called normal play because most games follow this convention, even though Nim usually does not.

Alice and Bob is tired of playing Nim under the standard rule, so they make a difference by also allowing the player to separate one of the heaps into two smaller ones. That is, each turn the player may either remove any number of objects from a heap or separate a heap into two smaller ones, and the one who takes the last object wins.

 

Input

Input contains multiple test cases. The first line is an integer 1 ≤ T ≤ 100, the number of test cases. Each case begins with an integer N, indicating the number of the heaps, the next line contains N integers s[0], s[1], ...., s[N-1], representing heaps with s[0], s[1], ..., s[N-1] objects respectively.(1 ≤ N ≤ 10^6, 1 ≤ S[i] ≤ 2^31 - 1)
 

Output

For each test case, output a line which contains either "Alice" or "Bob", which is the winner of this game. Alice will play first. You may asume they never make mistakes.
 

Sample Input

2
3
2 2 3
2
3 3
 

Sample Output

Alice
Bob
 
 

分析

打表找规律。

$$
sg(x)=
\begin{cases}
x-1,&x\ mod\ 4=0\\
x+1,&x\ mod\ 4=3\\
x,&else \\
\end{cases}
$$

code

  1. #include<cstdio>
  2. #include<algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int get_SG(int x) {
  7. if (x%==) return x-;
  8. else if (x%==) return x+;
  9. return x;
  10. }
  11. int main () {
  12. int T,n,ans;
  13. scanf("%d",&T);
  14. for (int Case=; Case<=T; ++Case) {
  15. scanf("%d",&n);
  16. ans = ;
  17. for (int t,i=; i<=n; ++i) {
  18. scanf("%d",&t);
  19. ans ^= get_SG(t);
  20. }
  21. if (ans==) puts("Bob");
  22. else puts("Alice");
  23.  
  24. }
  25. return ;
  26. }

打表代码

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6.  
  7. int sg[];
  8. bool Hash[];
  9. const int N = ;
  10.  
  11. void get_SG() {
  12. for (int i=; i<=N; ++i) {
  13. memset(Hash,false,sizeof(Hash));
  14. for (int j=; j<=i; ++j) {
  15. Hash[sg[j]] = true; // 取石子
  16. if (j!= && j!=N) Hash[sg[j]^sg[i-j]] = true; //拆分
  17. }
  18. for (int j=; ; ++j)
  19. if (!Hash[j]) {sg[i] = j;break;};
  20. }
  21. }
  22.  
  23. int main () {
  24. get_SG();
  25. /*for (int i=1; i<=10; ++i) {
  26. for (int j=1; j<=10; ++j) {
  27. printf("%d ",sg[(i-1)*10+j]);
  28. }
  29. puts("");
  30. }*/
  31. for (int i=; i<=; ++i) {
  32. printf("%d ",sg[i]);
  33. if (i % == ) puts("");
  34. }
  35. return ;
  36. }

HDU 3032 Nim or not Nim?(Multi_SG,打表找规律)的更多相关文章

  1. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  2. 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)T ...

  3. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  4. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  5. HDU 4588 Count The Carries 数位DP || 打表找规律

    2013年南京邀请赛的铜牌题...做的非常是伤心.另外有两个不太好想到的地方.. ..a 能够等于零,另外a到b的累加和比較大.大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数, ...

  6. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  7. HDU 5795 A Simple Nim (博弈 打表找规律)

    A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...

  8. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  9. hdu_5795_A Simple Nim(打表找规律的博弈)

    题目链接:hdu_5795_A Simple Nim 题意: 有N堆石子,你可以取每堆的1-m个,也可以将这堆石子分成3堆,问你先手输还是赢 题解: 打表找规律可得: sg[0]=0 当x=8k+7时 ...

随机推荐

  1. python小游戏之贪吃蛇

    本程序需要安装pygame,请自行百度安装...... 废话不多说,直接上代码 import pygame,sys,time,random from pygame.locals import * # ...

  2. vue-cli建立的项目如何在手机端运行以及如何用charles来抓包

    刚开始自己在config文件夹下的index.js中的dev下的host写成的是localhost,但是发现自己不能在手机端访问,并且也不可以在charles进行抓包处理,后来把localhost改成 ...

  3. git 初始化仓库与远程clone

    使用命令 git –bare init /home/git/myRep.git,初始化化仓库 在gitClient_01上,通过git clone命令进行克隆远程仓库,并在各自的电脑上运行开发. Gi ...

  4. 微信企业号升级企业微信后zabbix告警发不出去

    首先看下微信的脚本 #!/bin/bash ###SCRIPT_NAME:weixin.sh### ###send message from weixin for zabbix monitor### ...

  5. C++unsigned char和char区别

    char和unsigned charchar与unsigned char都是一个字节8bit,唯一的区别是,char的最高位为符号位,因此char能表示-128~127, unsigned char( ...

  6. Javascript笔记部分

    写入HTML输出 document.write(“<h1>”); 改变HTML内容 x = document.getElementById(“demo”) //查找元素 后面可以.valu ...

  7. Java第六次作业:RuPengGame setGameSize setGameTitle alert loadBgView playSound pause closeSound confirm input createText setTextPosition setTextColor setTextFontSize hideText showText CreateImage(number)

    package com.swift; import java.awt.Color; import com.rupeng.game.GameCore;//导入游戏引擎包 //实现Runnable接口 p ...

  8. [BZOJ] 2044: 三维导弹拦截

    排序去掉一维,剩下两维可以直接\(O(n^2)\)做,也可以用二维树状数组(但是不方便建边),解决第一问 第二问,按转移顺序连边,建出DAG,求最小不可重链覆盖即可 #include<algor ...

  9. Linux NFS服务器的安装与配置详解

    一.NFS服务简介 NFS是Network File System(网络文件系统).主要功能是通过网络让不同的服务器之间可以共享文件或者目录.NFS客户端一般是应用服务器(比如web,负载均衡等),可 ...

  10. nginx修改nginx.conf配置可以https访问

    修改nginx.conf,参照如下更改配置server { listen 443; server_name abc.com; // 访问域名 ssl on; root /var/www/bjubi.c ...