码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu4734]F(x)数位dp

时间:2017-11-04 15:10:51      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:ace   数位dp   span   int   ret   while   code   printf   limit   

题意:求0~f(b)中,有几个小于等于 f(a)的。

解题关键:数位dp

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50002;
int dp[12][maxn],a[12];
int get(int x){
    int pos=0;
    while(x){
        a[pos++]=x%10;
        x/=10;
    }
    return pos;
}
int f(int x){
    if(x==0) return 0;
    return f(x/10)*2+x%10;
}
int dfs(int pos,int sta,bool limit){
    if(pos==-1) return sta>=0;
    if(sta<0) return 0;
    if(!limit&&dp[pos][sta]) return dp[pos][sta];
    int ans=0,up=limit?a[pos]:9;        //这里注意一下 
    for(int i=0;i<=up;i++){
        ans+=dfs(pos-1,sta-i*(1<<pos),limit&&i==a[pos]);
    }
    if(!limit) dp[pos][sta]=ans;
    return ans;
}
int main(){
    int T;
    scanf("%d",&T);
    for(int kase=1;kase<=T;kase++){
        int n,m;
        scanf("%d%d",&n,&m);
        int pos=get(m);
        int ans=dfs(pos-1,f(n),true);
        printf("Case #%d: %d\n",kase,ans);
    } 
    return 0;
}

 

[hdu4734]F(x)数位dp

标签:ace   数位dp   span   int   ret   while   code   printf   limit   

原文地址:http://www.cnblogs.com/elpsycongroo/p/7783175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!