A Multiplication Game (hdu 1517) (博弈)

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.

双方的立场上希望自己能赢,要么取最大的要么取最小的,即取2或者9。那我们找到第一个大于等于n的只含1,2,9三个因子数且2,9的因子数相差小于等于1的数。如果2,9因子数相等则先手输,否则赢。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int m;
ll ans[1000];
void init()
{
    m=0;
    for(int i=1;i<=32;i++)
    {
        ll p=(ll)pow(2,i);
        for(int j=1;j<=12;j++)
        {
            ll k=(ll)pow(9,j);
            if(p*k<0||abs(i-j)>=2) continue;
            ans[m++]=p*k;
        }
    }
    sort(ans,ans+m);
}
int main()
{
    init();
    ll n;
    while(scanf("%lld",&n)==1)
    {
        if(n<=9)
        {
            printf("Stan wins.\n");
            continue;
        }
        int a=0,b=0;
        int d=lower_bound(ans,ans+m,n)-ans;
        ll t=ans[d];
        n=ans[d];
        while(t%2==0&&t) a++,t/=2;
        while(n%9==0&&n) b++,n/=9;
        if(abs(a-b)==1) printf("Stan wins.\n");
        else printf("Ollie wins.\n");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/81407684