HDU5591(博弈)

***********************************************声明******************************************************

      原创作品,出自 “晓风残月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


Problem Description
ZYB  played a game named  NumberBomb  with his classmates in hiking:a host keeps a number in  [1,N]  in mind,then 
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] .
 

Input
In the first line there is the number of testcases  T .

For each teatcase:

the first line there is one number  N .

1T100000 , 1N10000000
 

Output
For each testcase,print the ans.
 

Sample Input
  
  
1 3
 

Sample Output
  
  
1
 

Source


题解:

      题意是说给定[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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值