hdu 4734 F(x) 数位DP

题目链接
简单的数位dp

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int mod = 1e9+7;
int dp[11][20000];
int a[110];
int len,maxx;
int dfs(int pos,int prev,bool limit){
    int i;
    if(pos==0) return 1;
    if(!limit&&dp[pos][maxx-prev]!=-1) return dp[pos][maxx-prev];
    int up;
    int ans=0;
    up = limit?a[pos]:9;
    for(i = 0;i <= up;i ++){
        int next = prev+i*(1<<pos-1);
        if(maxx-next<0) continue;
        ans += dfs(pos-1,next,limit&&i==up);
    }
    if(!limit) dp[pos][maxx-prev] = ans;
    return ans;
}
int solve(int n){
    len = 0;
    while(n!=0){
        int x = n%10;
        n /= 10;
        a[++len] = x;
    }
    int ans = dfs(len,0,1);
    return ans;
}
int find_maxx(int a){
    LL ans=0;
    int now=0;
    while(a!=0){
        LL x = a%10;
        now ++;
        ans += x*(1<<now-1);
        a/=10;
    }
    return ans;
}
int main(){
    //freopen("a.txt","r",stdin);
    ios::sync_with_stdio(0);
    int T;
    cin>>T;
    int tot=0;
    memset(dp,-1,sizeof(dp));
    while(T --){
        int a,x;
        tot ++;
       // memset(dp,-1,sizeof(dp));
        cout<<"Case #"<<tot<<": ";
        cin>>a>>x;
        maxx = find_maxx(a);
        cout<<solve(x)<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_30358129/article/details/80325450