HDU 2848 Number Cutting Game(博弈思想 + dfs)题解

思路:dfs找先手必胜的情况是否存在

代码:

#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define ull unsigned long long
using namespace std;
const int maxn = 1000000+5;
const int seed = 131;
const int MOD = 100013;
const int INF = 0x3f3f3f3f;
ll k;
ll fac[20];
int dfs(ll n,ll times,ll sum){
    if(times < k - 1 && n == 0) return 0;
    if(times == k - 1){
        if(dfs(sum + n,0,0))
            return 0;
        return 1;
    }
    for(int i = 1;n >= fac[i];i++){ //cut the number
        if(dfs(n / fac[i],times + 1,sum + n % fac[i])){
            return 1;
        }
    }
    return 0;
}
int main(){
    ll n;
    fac[0] = 1;
    for(int i = 1;i <= 19;i++)
        fac[i] = fac[i - 1] * 10;
    while(~scanf("%lld%lld",&n,&k)){
        printf("%d\n",dfs(n,0,0));
    }
    return 0;
}

 

posted @ 2018-08-22 15:26  KirinSB  阅读(267)  评论(0编辑  收藏  举报