首页 > 代码库 > HDU2848 Number Cutting Game

HDU2848 Number Cutting Game

题解:

记忆化搜索

如何把数分割为k个部分并且求和。以及如何编程这个博弈过程。。挺考查代码能力的

代码:

#include<bits/stdc++.h>using namespace std;#define pb push_back#define mp make_pair#define se second#define fs first#define LL long long#define CLR(x) memset(x,0,sizeof x)#define MC(x,y) memcpy(x,y,sizeof(x))  #define SZ(x) ((int)(x).size())#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1 typedef pair<int,int> P;const double eps=1e-9;const int maxn=200100;const int mod=1e9+7;const int INF=1e9;LL Base[22];void makebase(){    Base[1]=10;    for(int i=2;i<22;i++) Base[i]=Base[i-1]*10;}bool dfs(LL,int);bool test(LL n,int d,int k,LL cur){    if(d==k) return !dfs(cur+n,k);    for(int i=1;i<22;i++)    {        LL a=n%Base[i];        LL b=n/Base[i];        if(!b) break;        if(test(b,d+1,k,cur+a)) return true;    }    return false;}bool dfs(LL n,int k){    if(n<Base[k-1]) return false;    return test(n,1,k,0);}int main(){    LL n;    int k;    makebase();    while(~scanf("%lld%d",&n,&k))    {        int flag=dfs(n,k);        printf("%d\n",flag);    }    return 0;}

 

HDU2848 Number Cutting Game