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 ...
随机推荐
- 将/home目录从单独的分区迁移回/目录下
安装系统的时候, 将/, swap, /home这三个目录放在了三个不同的分区, 现在希望将/home目录移回/目录下. 1. umount /home, 然后在/目录下创建/home_new, 通过 ...
- 自己用node.js 搭建APP服务器,然后用AFNetworking 请求 报如下错误:App TransportSecurity has blocked a cleartext HTTP (http://) resource load since it isinsecure. Temporary exceptions can be configured via your app's Info.
"App TransportSecurity has blocked a cleartext HTTP (http://) resource load since it isinsecure ...
- getServletPath getRequestURI getRequestURL区别
getContextPath:/test //上下文,类似工程名 getServletPath:/test.jsp getRequestURI:/test/test.jsp ...
- Tomcat负载均衡配置-未完成
集群技术是目前非常流行的提高系统服务能力与高可靠性( HA- High Availability )的手段,通过把多个独立的服务器组成一个集群可以实现失效无缝转移.也就是说当有某一台集群中的服务器当机 ...
- Masonry学习分享
不完整目录 •UIScrollView 应用Masonry的正确用法 •tableHeaderView使用Masonry •同向文字显示优先级 1.基础篇 1.1基础使用 1.1.1运行效果 1.1. ...
- Splay树-Codevs 1296 营业额统计
Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...
- Markdown语法速查
Markdown教程:http://wowubuntu.com/markdown/ h1 # h1 h2 ## h2 h3 ### h3 h4 #### h4 h5 ##### h5 h6 ##### ...
- UE4 减少APK包的大小
本文依据官方文档 Reducing APK Package Size整理而来,不过我会陆续添加自己减少包大小的心得. ETC1 纹理 当使用ETC1打Android包时,注意ETC1是不会压缩带Alp ...
- 1.2 如何在visual studio 中建立C#程序
这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大 ...
- bitcask
Bitcask模型是一种日志型kv模型.所谓日志型,是指它不直接支持随机写入,而是像日志一样支持追加操作.Bitcask模型将随机写入转化为顺序写入. 任意时刻,系统中只有一个数据文件支持写入,称为a ...