NBUT 1578-The smart Big Pang Pang【博弈论】 难度:**

题意:

Today,Big Pang Pang and Captain want to plan a game,the rule is simple.Now,they starting with two natural numbers.Captain,the first player, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be nonnegative. Then Big Pang Pang, the second player, does the same with the two resulting numbers, then Captain, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):

25 7
11 7
4 7
4 3
1 3
1 0

an Captain wins.

输入
The input consists of a number of lines. Each line contains two positive integers giving the starting two numbers of the game. Captain always starts.
输出
For each line of input, output one line saying either Captain or Big Pang Pang assuming that both of them play perfectly. The last line of input contains two zeroes and should not be processed.
样例输入
34 12
15 24
0 0
样例输出
Captain
Big Pang Pang

题解:

题意就是给你两个数,你和对手轮流操作:将大数减去小数的n倍(不能减成负数),最先将一个数减为0的人获胜。

首先我们分类讨论一下,如果给的两个数min和max之间满足min×2>max,也就是说你没有任何选择,只能选择用max-min×1,而对方也只能选择新的max-新的min×1,这样这场比赛就不是博弈,而是纯模拟了。(因为你和对方的操作都是唯一确定的)这样辗转相减模拟出结果就好了。

如果min×2=max,先手直接选择max-2×min就赢了。

如果min×2<max,这是本题攻克的核心。举一个例子,比如题目中给的25 7,假如你选择25-7×1,这样就变成了18 7,轮到对方下棋;而如果你选择25-7×2,这样就变成了11 7,又是轮到对方下棋。此时你观察下这两种选择的结果,(18 7)和(11 7)刚好差了一步!(18 7再做一步就可以变成11 7)差了一步意味着什么呢?意味着如果其中一种情况必然会导致失败的结果,那么另一种情况必然能导致胜利的结果。(步骤差1决定了必胜与必败之间的转化)所以就是说:当min×2<max时,你选择max-min×1和max-min×2,其中必然有一种结果可以让你胜利。简而言之,min×2<max时先手必胜。

代码:

#include<stdio.h>
int dfs(int a,int b)
{
	if(a>b)
	{
		int temp=a;
		a=b;
		b=temp;
	}
	if(b%a==0)return 1;
	else
	{
		if(dfs(b%a,a)==1&&b-a<a)return 0;
		else return 1;
	}
}
int main()
{
	int a,b;
	while(1)
	{
		scanf("%d%d",&a,&b);
		if(a==0&&b==0)break;
		if(dfs(a,b)==1)printf("Captain\n");
		else printf("Big Pang Pang\n");
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42921101/article/details/104341206