HDU 1517 a multiplication game (game-SG)

Source: Internet
Author: User

A multiplication game
Problem descriptionstan 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.
 
Inputeach line of input contains one integer number n.
 
Outputfor each line of input output one line either

Stan wins.

Or

Ollie wins.

Assuming that both of them play perfectly.
 
Sample Input
1621734012226
 
Sample output
Stan wins.Ollie wins.Stan wins.
 

Question:

Stan and Ollie play the game. The first digit is 1. The two take turns. Stan starts first, and each person chooses 2 ~ 9. One of them is always multiplied by the initial number until a person reaches the number greater than or equal to n. If n is given, he will ask you who will win.


Solution:

I did not find out the rule of SG function (mandatory). I can only use the DP method to find the mandatory and mandatory States for each State.

2 ~ 9. You can use the numbers 2, 3, 5, and 7 in sequence. Therefore, you only need to record the numbers 2, 3, 5, and 7 in each status. The next step is 2 ~ The offset of 9, which can be expressed in this way.

Const int off2 [] = };
Const int off3 [] = };
Const int off5 [] = {0, 0, 1, 0, 0 };
Const int off7 [] = {0, 0, 0, 0 };


Solution code:

#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;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.