【HDU 1517】A Multiplication Game

【题目】

传送门

Description

Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p >= n.

Input

Each line of input contains one integer number n.

Output

For each line of input output one line either “Stan wins.” or “Ollie wins.” assuming that both of them play perfectly.

Sample Input

162
17
34012226

Sample Output

Stan wins.
Ollie wins.
Stan wins.


【分析】

题目大意:StanOllie 玩游戏,Stan 先手。给出一个数 n n ,从 1 1 开始,轮流对这个数乘 2 2 ~ 9 9 中的一个数,直到这个数大于等于 n n 为止,最后乘的人胜利。假如 StanOllie 都采取最优策略,问谁会赢。

先引入两个概念吧。

  1. 必败点(P 点):前一个选手(Previous player)将取胜的位置称为必败点。
  2. 必胜点(N 点):下一个选手(Next player)将取胜的位置称为必胜点。

那么这道题,对于 Stan 而言, n n 这个位置是一个必败点,因为前一个人已经到 n n 了,那他显然就输了。

不难发现的是, [ &ThickSpace; n 9 &ThickSpace; , &ThickSpace; n 1 &ThickSpace; ] [\;\lceil\frac{n}{9}\rceil\;,\;n-1\;] 这里面的数都是必胜点,因为它们都可以通过乘 9 9 达到 n n

n = n 9 n&#x27;=\lceil\frac{n}{9}\rceil ,那么 [ &ThickSpace; n 2 &ThickSpace; , &ThickSpace; n 1 &ThickSpace; ] [\;\lceil\frac{n&#x27;}{2}\rceil\;,\;n&#x27;-1\;] 里的点都是必败点,因为无论乘多少,它们都只能达到必胜点。

按照上面的规律从 n n 往前递推,直到等于 1 1 ,然后判断此时是必胜点还是必败点就行了。


【代码】

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
	int winner;
	unsigned int n;
	while(~scanf("%ud",&n))
	{
		while(n!=1)
		{
			if(n!=1)  winner=0,n=ceil((double)n/9);
			if(n!=1)  winner=1,n=ceil((double)n/2);
		}
		puts(winner?"Ollie wins.":"Stan wins.");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/forever_dreams/article/details/87886207