POJ 2368-Buttons 解题报告 【博弈】

版权声明:© By www.mrchen.love https://blog.csdn.net/weixin_43206704/article/details/82821376

POJ 2368-Buttons 解题报告 【博弈】

Description

  As you surely already know, Yekaterinburg has gotten its right to hold The Summer Olympic Games of the 2032. It is planned that it will be allowed to Russia as a country-organizer to emend a program of the games a bit. So, in order to improve the command result it has been decided to replace the competition in gymnastics by the competition in the new game “Buttons”.
  The rules of the game are very simple. There’s a small heap of K buttons before two players. The players in turns take buttons from the heap, moreover, at a time one can take a number of buttons from 1 up to L. The one who takes the last button is the winner.
  The rules of the Olympic Games will be a bit harder then usual. The one, who is to make a first step according to a lot, has an opportunity to fix a number K with the following restriction to it: 3 <= K <= 100 000 000 (that is the exact number of buttons that has been prepared for the Olympic tournament). The player who is to make the second step fixes a number L that satisfies the following conditions 2 <= L < K.
  A very crucial task is given to your team: you are to write a program that should help the second player to make his choice. In other words, given a number K your program is to find a number L that guaranties a victory to the second player with a proper game of both sides.
  So, for instance, there are only three buttons in the heap, the choice L = 2 provides for the victory of the second player. Really, if the first player takes only one button at his turn, the second one wins, taking the two last buttons. On the contrary, if the first one takes two buttons, the second one wins, taking the last button.

Input

  The standard input consists of one line, which contains an only integer number K — a number of buttons in the heap, that has fixed the first player at his turn.

Output

  To the standard output you are to write the only number L — the maximal number of buttons that can be taken at a time which provides for the victory of the second player. If there are several those numbers L, you should write the least. If there are no such numbers, you are to write 0 to the standard output.

Sample Input

3

Sample Output

2


巴什博弈

  这题的大意就是巴什博弈: n n 个物品组成一堆,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取 m m 个,最后取尽物体的一方获胜。
  分析:如果 n &lt; m + 1 n&lt;m+1 ,则先手方一次就能取尽,先手方获胜;如果 n = m + 1 n=m+1 ,则无论先手方取走多少,后手方下一次总能一次取尽,后手方获胜。
  以下分两种情况:
  1. n n m + 1 m+1 的倍数, n = ( m + 1 ) s , ( s 1 ) n=(m+1) \cdot s ,(s\ge1) ,则后手可以每次使 n = ( m + 1 ) ( s 1 ) n=(m+1) \cdot (s-1) 。后手必胜。
  2. n n 不是 m + 1 m+1 的倍数, n = ( m + 1 ) s + r , ( s 0 , r &lt; m + 1 ) n=(m+1) \cdot s+r,(s \ge0,r&lt;m+1) ,则先手可以先取 r r 个,之后就变成和情况1一样的情形。先手必胜。

思路和注意点

  这道题要求后手必胜: K = ( L + 1 ) s K=(L+1)*s ;题目即在满足 2 L &lt; K 2\le L&lt;K 的情况下,求 K K 的最小约数,即求 K K 大于等于3的最小约数。题目不会无解,因为 K K 本身也是自己的约数。
  可以按照判断一个数是否为质数的方法从 3 循环到 K \sqrt K ,如果是质数则大于等于3的最小约数就是其本身。由于没从 2 开始循环,这里不要忘了 K K 能被 2 整除的情况,大于等于3的最小约数也可能是 K 2 \frac{K}{2} (比如K=2乘一个大于2的素数的情况)。


  代码如下:

#include <stdio.h>
#include <math.h>

int main()
{
	int i;
	int K = 0;

	scanf("%d", &K);
	int T = (int)sqrt((double)K)+1;
	for (i = 3; i <= T; i++)
	{
		if (K % i == 0)
			break;
	}

	if (i > T)
	{
		if (K % 2 || K == 4)
			printf("%d\n", K - 1);
		else
			printf("%d\n", K / 2 - 1);
	}
	else
		printf("%d\n", i - 1);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43206704/article/details/82821376