A Multiplication Game (博弈,規律)

feng_zhiyu發表於2017-08-08

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.

題意:給一個正整數n(1 < n < 4294967295),p=1,S和O遊戲,兩人輪流對p乘以2-9之間的一個數,首先使p>=n的人獲勝,每次遊戲都從S開始

分析:與其說博弈,不如說是規律。。 這個題看著有點像Nim博弈,想著和9的倍數? 還是別的?
寫了一個,發現不對
然後根據題意列幾個出來
S勝:2-9 (0+2)——9
O勝:10-18 (9+1)——9*2
S勝:19-162 (2*9+1)——9*2*9
O勝:163-324 (9*2*9+1)——9*2*9*2
S勝:324-2916 (9*2*9*2+1)——9*2*9*2*9
……

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(a,n) memset(a,n,sizeof(a))
const double INF=0x3f3f3f3f+1.0;
const double eps=1e-6;
const double PI=2.0*acos(0.0);
typedef long long LL;
const int N=305;
int main()
{
    LL n,tmp;
    while(~scanf("%lld",&n))
    {
        if(n>1&&n<10) 
        {
            puts("Stan wins.");
            continue;
        }
        LL ans=0,p=1;
        while(p<n)
        {
 //           printf("p=%lld ans=%lld\n",p,ans);
            if(ans&1) p*=2;
            else p*=9;
            ans++;
        }
        printf("%s wins.\n",ans%2?"Stan":"Ollie");
    }
    return 0;
}

相關文章