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 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 interesting, players can separate one heap into three smaller heaps(no empty heaps)instead of the picking operation.Please find out which player will win the game if each of them never make mistakes.

两个玩家轮流从n堆糖中挑若干糖果,选到最后一颗糖的人赢得游戏。他们每回合他们可以在同一堆糖上取任意数量的糖果(不能不拿)。为了让游戏更有意思,玩家可以选择把某堆糖一分为三(没有空堆)而不去取糖果。在两人皆不犯错的情况下,找出游戏的赢家。

CN

Input - 输入

Intput 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≤106,1≤s[i]≤109)

多组测试用例。第一行是一个整数1≤T≤,表示测试用例的数量。每组测试用例以一个整数n打头,表示糖果的堆数,随有N个整数s[],s[],....,s[n−], 分别表示各堆糖果的数量。(≤n≤^,≤s[i]≤^)

CN

Output - 输出

For each test case,output a line whick contains either"First player wins."or"Second player wins".

对于每个测试用例,输出一行"First player wins."或"Second player wins"。

CN

Sample Input - 输入样例

2
2
4 4
3
1 2 4

Sample Output - 输出样例

Second player wins.
First player wins.

题解

  暴力出奇迹,搜索是真知,打表找规律,反正SG……

代码 C++

 #include <cstdio>
#include <cstring>
#define mx 100
int sg[mx], w[mx << ];
void rdy(){
int i1, i2, i3, i, j;
memset(sg, , sizeof(sg));
for (i = ; i < mx; ++i){
memset(w, , sizeof(w));
for (j = ; j < i; ++j) w[sg[j]] = ;
if (i >= ){
for (i1 = ; i1 * <= i; ++i1){
for (i2 = i1; i1 + i2 * <= i; ++i2){
i3 = i - i1 - i2;
if (i3 >= i2) w[sg[i1] ^ sg[i2] ^ sg[i3]] = ;
}
}
}
for (j = ; w[j]; ++j);
sg[i] = j;
}
}
int main(){
rdy();
int n, i;
for (i = ; i < mx; ++i){
if (sg[i] != i) printf("i=%d sg=%d\n", i, sg[i]);
}
return ;
}

打表围观

 #include <cstdio>
int sg(int i){
if (i % == || (i + ) % == ){
if (i & ) ++i;
else --i;
}
return i;
}
int main(){
int t, n, s, opt;
scanf("%d", &t);
while (t--){
scanf("%d", &n); opt = ;
for (opt = ; n--; opt ^= sg(s)) scanf("%d", &s);
if (opt) puts("First player wins.");
else puts("Second player wins.");
}
return ;
}

HDU 5795 A Simple Nim(简单Nim)的更多相关文章

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

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

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

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

  3. HDU 5795 A Simple Nim 打表求SG函数的规律

    A Simple Nim Problem Description   Two players take turns picking candies from n heaps,the player wh ...

  4. HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. hdu 5795 A Simple Nim 博弈sg函数

    A Simple Nim Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Pro ...

  6. HDU 5795 A Simple Nim ——(Nim博弈 + 打表)

    题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作. 分析:打表找sg值的规律即可. 感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~ 打 ...

  7. HDU 5795 A Simple Nim

    打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

  8. HDU 5795:A Simple Nim(博弈)

    http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description   Two players take t ...

  9. HDU 5795 博弈

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Time Limit: 2000/1000 MS (Java/Oth ...

随机推荐

  1. ArcGIS Server 增加缓存路径

    Server缓存服务,由于缓存文件经常比较大,默认放在C盘下容易导致磁盘空间不够,因此Server提供了增加缓存路径的方法来解决该问题. 增加的路径有两种,一种是Server所在服务器增加一个和原缓存 ...

  2. js串讲回顾

    注:1.xx.nextSibling.css.xxx->xx的下一个元素的css样式;2. window.opener.document.getElementById("cms&quo ...

  3. ant build utf-8

    使用Ant编译过程中,报error: unmappable character for encoding UTF8 最简单的方法是在Build.xml文件中,在所有出现Javac的地方,增加一个选项: ...

  4. java中PriorityQueue优先级队列使用方法

    优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...

  5. 【C++】类型转换(学习笔记)

    1. 隐式类型转换,相关联的类型(e.g.int vs double)之间可以发生隐式类型转换. 比如,在条件中,非布尔类型转为布尔类型: 初始化时,初始值变为变量类型: 赋值时,右值变成左侧的类型: ...

  6. Jmeter进行数据库压测

    一.配置并发用户 新建线程组,设置线程数,Ramp-up和循环次数 二.添加JDBC请求 先选中JDBC Users(线程组),右键选中ADD-Config Element--JDBC Connect ...

  7. Android内存泄露

    Android 内存泄漏是一个十分头疼的事情.LeakCanary是一款开源软件,主要作用是检测 Android APP 内存泄露.比起以前的 MAT 工具,LeakCanary 有着十分强大的功能, ...

  8. 20145320《Java程序设计》第9周学习总结

    20145320<Java程序设计>第9周学习总结 教材学习内容总结 16.整合数据库 JDBC(java DateBase Connectivity)是用于执行SQL的解决方案,开发人员 ...

  9. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  10. submit

    前台<body>中的代码: <body> <div id="top"> </div> <form id="login ...