标签:des style blog color os 2014
162 17 34012226
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),布布扣,bubuko.com
HDU 1517 A Multiplication Game (博弈-求sg)
标签:des style blog color os 2014
原文地址:http://blog.csdn.net/a1061747415/article/details/37042973