首页 > 代码库 > HDU 1517 A Multiplication Game (博弈-求sg)
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.
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。