HDU 1517 A Multiplication Game (博弈-求sg)

A Multiplication Game

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.

题目大意:

Stan 和 Ollie 两个人玩游戏, 一开始数字是 1,两个人轮流,Stan 先手,每个人选择 2~9 里面的一个一直乘以起初的数字,直到某个人乘到 这个数大于等于 n 就算赢,n给定,问你谁必然赢。

解题思路:

这题我没找出SG函数(必胜必输)的规律,只能用DP的方法求出每种状态的必胜必输状态。

2~9 ,依次其实可以用 2 3  5  7 这几个数得到,因此每个状态只需要记录2 3  5  7 这几个数的数目即可,接下来就是2~9的偏移量,可以 这么表示

const int off2[]={1,0,2,0,1,0,3,0};

const int off3[]={0,1,0,0,1,0,0,2};

const int off5[]={0,0,0,1,0,0,0,0};

const int off7[]={0,0,0,0,0,1,0,0};

解题代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;

const int off2[]={1,0,2,0,1,0,3,0};
const int off3[]={0,1,0,0,1,0,0,2};
const int off5[]={0,0,0,1,0,0,0,0};
const int off7[]={0,0,0,0,0,1,0,0};

int dp[40][40][40][40][2];
ll n;

ll pow_mod(ll a,int b){
    ll ans=1,tmp=a;
    while( b>0 ){
        if( b%2!=0 ) ans*=tmp;
        b/=2;
        tmp*=tmp;
    }
    return ans;
}

int DP(int c2,int c3,int c5,int c7,int f){
    ll tmp=pow_mod(2,c2)*pow_mod(3,c3)*pow_mod(5,c5)*pow_mod(7,c7);
    if(tmp>=n) return 1-f;
    if(dp[c2][c3][c5][c7][f]!=-1) return dp[c2][c3][c5][c7][f];
    int ans;
    if(f==0){
        ans=1;
        for(int i=0;i<8;i++){
            int cc2=c2+off2[i],cc3=c3+off3[i],cc5=c5+off5[i],cc7=c7+off7[i];
            if(DP(cc2,cc3,cc5,cc7,1-f)<ans){
                ans=0;
                break;
            }
        }
    }else{
        ans=0;
        for(int i=0;i<8;i++){
            int cc2=c2+off2[i],cc3=c3+off3[i],cc5=c5+off5[i],cc7=c7+off7[i];
            if(DP(cc2,cc3,cc5,cc7,1-f)>ans){
                ans=1;
                break;
            }
        }
    }
    return dp[c2][c3][c5][c7][f]=ans;
}

int main(){
    while(cin>>n){
        memset(dp,-1,sizeof(dp));
        if(DP(0,0,0,0,0)==0) printf("Stan wins.\n");
        else printf("Ollie wins.\n");
    }
    return 0;
}

HDU 1517 A Multiplication Game (博弈-求sg)

时间: 2024-12-24 21:49:13

HDU 1517 A Multiplication Game (博弈-求sg)的相关文章

HDU 1517 A Multiplication Game (博弈&amp;&amp;找规律)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3691    Accepted Submission(s): 2097 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

HDU 1517 A Multiplication Game 博弈

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6202    Accepted Submission(s): 3532 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

HDU 1517 A Multiplication Game(博弈论)

题目地址:HDU 1517 NP状态转换. 可以把题目的向上乘变成向下除.这样1是终结状态,设1的时候为必败点. 根据所有能一步到达必败点的点是必胜点,所以[x,x*9]必胜点:根据只能到达必胜点的点是必败点,所以[x*9+1,x*9*2]是必败点. 然后求解. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdli

hdu 1517 A Multiplication Game 段sg

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3832    Accepted Submission(s): 2183 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

hdu 1517 A Multiplication Game(必胜态,必败态)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5833    Accepted Submission(s): 3303 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

hdu 1517 (类巴神博弈)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3977    Accepted Submission(s): 2264 Problem Description Stan and Ollie play the game of multiplication by multiplying an i

【UVA1378】A Funny Stone Game (博弈-求SG值-输出方案)

[题目] Description The funny stone game is coming. There are n piles of stones, numbered with 0, 1, 2, ..., n − 1. Twopersons pick stones in turn. In every turn, each person selects three piles of stones numbered i, j, k(i < j, j ≤ k and at least one s

HDU 3032 Nim or not Nim? (博弈之求SG函数)

题意:经典Nim博弈游戏变换,给你n堆石子pi,每堆有pi个石子, Alice和Bob轮流取石子,每次可以从任意一堆中拿走任意个石子,也可以将某一堆石子分成两个小堆 (每堆石子个数必须不能为0),先拿完者获胜 思路:求SG函数后找规律: SG函数定义及求法:点击打开链接 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #in

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now