HDU_1517_博弈(巧妙规律)

A Multiplication Game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5325    Accepted Submission(s): 3034


Problem 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.
 
解题思路:
由于每次都是从p=1开始的,所以只要判断每个游戏中1为必败点还是必胜点即可。(以下各式 / 均为取上整)
依照上面所提到的算法,将终结位置,即[n,无穷]标记为必败点;
然后将所有一步能到达此必败段的点标记为必胜点,即[n/9,n-1]为必胜点;
然后将只能到达必胜点的点标记为必败点,即[n/9/2,n/9-1]为必败点;
重复上面2个步骤,直至可以确定1是必胜点还是必败点。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<cmath>
using namespace std;

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int x;
        for(x=0;n>1;x++)
        {
            if(x&1)
                n=ceil(n*1.0/2);
            else
                n=ceil(n*1.0/9);
        }
        if(x&1)
            printf("Stan wins.
");
        else
            printf("Ollie wins.
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jasonlixuetao/p/5900115.html