***********************************************声明******************************************************
原创作品,出自 “晓风残月xj” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj)。
由于各种原因,可能存在诸多不足,欢迎斧正!
*********************************************************************************************************
好久没刷题了,最近写代码比较少,大部分时间都在查问题,修bug之类修修补补的,曰之为“运维”,着实学不到什么东西,所以决定晚上刷刷题,保留编程的感觉,脚本用多也不好。
ZYB's Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 456 Accepted Submission(s): 372
players guess a number in turns,the player who exactly guesses X loses,or the host will tell all the players that
the number now is bigger or smaller than X .After that,the range players can guess will decrease.The range is [1,N] at first,each player should guess in the legal range.
Now if only two players are play the game,and both of two players know the X ,if two persons all use the best strategy,and the first player guesses first.You are asked to find the number of X that the second player
will win when X is in [1,N] .
For each teatcase:
the first line there is one number N .
1≤T≤100000 , 1≤N≤10000000
1 3
1
题解:
题意是说给定[1,n]的区间,然后给定一个双方都知道的X,两个人依次说一个数,不断缩小范围,正好说中的人输。本题要求找到保证第二个人胜的策略。
好久没刷题,读上面的题目特别困难,同时在是否应该保留最后一个换行符的地方出现几次PE,最终AC了。本题是一个比较基础的博弈问题。正好可以联想到毕业找工作那会的智力题:给定一个圆,然后两人轮流扔硬币,最后无法完全贴在圆上的输。请问第二个人是否有必胜的把握。其实可以这么想,第一个人放在圆心处。由于圆的对称性,第一个人只要找第二个关于圆心对称的地方扔就行,最后无法完全覆盖的必然是第二个人。
回到本题,只要N是奇数且X=[n/2]取上整时,第二个人找第一个人关于X对称的点就行,这样就能保证X留给第一个人。如果是偶数,没有必胜的策略。进一步,可以证明,当N为奇数,X取其他数是不能保证必胜的,因为第一个人可以留下一个关于X对称的奇数列,保证用上面的策略必胜。
代码比较简单,直接贴上。
#include<cstdio>
int main()
{
int cnt=0;
scanf("%d",&cnt);
while(cnt--){
int n;
scanf("%d",&n);
if(n&1)printf("1\n");
else printf("0\n");
}
return 0;
}