HDU 5795 A Simple Nim(简单Nim)
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)的更多相关文章
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- HDU 5795 A Simple Nim (博弈 打表找规律)
A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...
- HDU 5795 A Simple Nim 打表求SG函数的规律
A Simple Nim Problem Description Two players take turns picking candies from n heaps,the player wh ...
- HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 5795 A Simple Nim 博弈sg函数
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
- HDU 5795 A Simple Nim ——(Nim博弈 + 打表)
题意:在nim游戏的规则上再增加了一条,即可以将任意一堆分为三堆都不为0的子堆也视为一次操作. 分析:打表找sg值的规律即可. 感想:又学会了一种新的方法,以后看到sg值找不出规律的,就打表即可~ 打 ...
- HDU 5795 A Simple Nim
打表找SG函数规律. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...
- HDU 5795:A Simple Nim(博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Problem Description Two players take t ...
- HDU 5795 博弈
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 A Simple Nim Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- 利用Columnal网格系统快速搭建网站的基本布局结构
1.下面是一些对响应式设计提供了不同程度支持的CSS框架: (1)Semantic(http://semantic.gs); (2)Skeleton(http://getskeleton.com); ...
- Oracle 11g的Redo Log和Archive Log的分析方法
自Oracle 11g起,无需设置UTL_FILE_DIR就可以使用LOGMNR对本地数据库的日志进行分析,以下是使用LOGMNR的DICT_FROM_ONLINE_CATALOG分析REDO和归档日 ...
- 【转】发布一个基于NGUI编写的UI框架
发布一个基于NGUI编写的UI框架 1.加载,显示,隐藏,关闭页面,根据标示获得相应界面实例 2.提供界面显示隐藏动画接口 3.单独界面层级,Collider,背景管理 4.根据存储的导航信息完成界面 ...
- python学习之 字符串前'r'的用法
在打开文件的时候open(r'c:\....') 加r和不加''r是有区别的 'r'是防止字符转义的 如果路径中出现'\t'的话 不加r的话\t就会被转义 而加了'r'之后'\t'就能保留原有的样子 ...
- C#编程利器之二:结构与枚举(Structure and enumeration)【转】
C#编程利器之二:结构与枚举(Structure and enumeration) 在上一篇文章中,介绍了类如何封装程序中的对象.而实际中,出了类可以封装对象外,结构和枚举也可以封装一些对象,本文将着 ...
- 协程并发框架gevent及其用法
gevent是python的一个并发框架,采用协程实现并发目的,用起来也非常简单 gevent的docs:http://www.gevent.org/contents.html 一个最简单的例子: i ...
- Win10如何设置防火墙开放特定端口 windows10防火墙设置对特定端口开放的方法
Win10防火墙虽然能够很好地保护我们的系统,但同时也会因限制了某些端口,而给我们的操作带了一些不便.对于既想使用某些端口,又不愿关闭防火墙的用户而言,在Win10系统中设置防火墙开放特定端口就非常必 ...
- 事务复制-大批量DEL操作
同事讨论删除发布表历史记录,导致订阅端数据滞后N小时.后来询问得知,发布表T只保留最近31天的数据,每天由Job删除31天前的数据,每天的删除量约400-500万条.默认情况下,在发布端删除400万条 ...
- html中表table行循环滚动例子
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><meta h ...
- ios - 纯代码创建collectionView
开始考虑好一点点时间,因为一般的都是用xib,或者storyboard来写的.这次用纯代码...废话较多请看 首先把storyboard干掉,工程里面的main干掉 由于干掉了storyboard则启 ...