hdu 1846
```
今天,大家选择上机考试,就是一种勇敢(brave)的选择;这个短学期,我们讲的是博弈(game)专题;所以,大家现在玩的也是“勇敢者的游戏”,这也是我命名这个题目的原因。
当然,除了“勇敢”,我还希望看到“诚信”,无论考试成绩如何,希望看到的都是一个真实的结果,我也相信大家一定能做到的~
各位勇敢者要玩的第一个游戏是什么呢?很简单,它是这样定义的:
1、 本游戏是一个二人游戏;
2、 有一堆石子一共有n个;
3、 两人轮流进行;
4、 每走一步可以取走1…m个石子;
5、 最先取光石子的一方为胜;
如果游戏的双方使用的都是最优策略,请输出哪个人能赢。
每组测试数据占一行,包含两个整数n和m(1<=n,m<=1000),n和m的含义见题目描述。
2 23 2 4 3
first second
```
#include<stdio.h>
#include<string.h>
int T,n,m,t[1010];
int main() {
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
memset(t,0,sizeof t);
for(int i=1;i<=n;i++) {
for(int j=i-1;j>=i-m&&j>=0;j--) {
if(!t[j]) {
t[i]=1; break;
}
}
}
puts(t[n]?"first":"second");
}
}
hdu 1847
“升级”?“双扣”?“红五”?还是“斗地主”?
当然都不是!那多俗啊~
作为计算机学院的学生,Kiki和Cici打牌的时候可没忘记专业,她们打牌的规则是这样的:
1、 总共n张牌;
2、 双方轮流抓牌;
3、 每人每次抓牌的个数只能是2的幂次(即:1,2,4,8,16…)
4、 抓完牌,胜负结果也出来了:最后抓完牌的人为胜者;
假设Kiki和Cici都是足够聪明(其实不用假设,哪有不聪明的学生~),并且每次都是Kiki先抓牌,请问谁能赢呢?
当然,打牌无论谁赢都问题不大,重要的是马上到来的CET-4能有好的状态。
Good luck in CET-4 everybody!
1 3
Kiki Cici
#include<stdio.h>
int n,a[1010];
int main() {
for(int i=1;i<=1000;i++) {
for(int j=1;j<=i;j*=2) {
if(a[i-j]==0) {
a[i]=1; break;
}
}
}
while(scanf("%d",&n)!=EOF) {
puts(a[n]?"Kiki":"Cici");
}
}
poj 2975
Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player’s move consists of removing one or more stones from any single pile. Play ends when all the stones have been removed, at which point the last player to have moved is declared the winner. Given a position in Nim, your task is to determine how many winning moves there are in that position.
A position in Nim is called “losing” if the first player to move from that position would lose if both sides played perfectly. A “winning move,” then, is a move that leaves the game in a losing position. There is a famous theorem that classifies all losing positions. Suppose a Nim position contains n piles having k1, k2, …, kn stones respectively; in such a position, there are k1 + k2 + … + knpossible moves. We write each ki in binary (base 2). Then, the Nim position is losing if and only if, among all the ki’s, there are an even number of 1’s in each digit position. In other words, the Nim position is losing if and only if the xor of the ki’s is 0.
Consider the position with three piles given by k1 = 7, k2 = 11, and k3 = 13. In binary, these values are as follows:
111 1011 1101
There are an odd number of 1’s among the rightmost digits, so this position is not losing. However, suppose k3 were changed to be 12. Then, there would be exactly two 1’s in each digit position, and thus, the Nim position would become losing. Since a winning move is any move that leaves the game in a losing position, it follows that removing one stone from the third pile is a winning move when k1 = 7, k2 = 11, and k3 = 13. In fact, there are exactly three winning moves from this position: namely removing one stone from any of the three piles.
The input test file will contain multiple test cases, each of which begins with a line indicating the number of piles, 1 ≤ n ≤ 1000. On the next line, there are n positive integers, 1 ≤ ki ≤ 1, 000, 000, 000, indicating the number of stones in each pile. The end-of-file is marked by a test case with n = 0 and should not be processed.
For each test case, write a single line with an integer indicating the number of winning moves from the given Nim position.
3 7 11 13 2 1000000000 1000000000 0
3 0
#include<stdio.h>
int n,a[1001];
int main() {
while(scanf("%d",&n)&&n){
int x=0,r=0;
for(int i=1;i<=n;i++)scanf("%d",&a[i]),x^=a[i];
for(int i=1;i<=n;i++)if(a[i]>(a[i]^x))r++;
printf("%d\n",r);
}
}