hdu4734 F(x)

F(x)

 HDU - 4734 

题目大意:

给出f(x)的定义:F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1,Ai是十进制数位,然后给出a,b求区间[0,b]内满足f(i)<=f(a)的i的个数。

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 10010
using namespace std;
int all,a,b,dp[20][maxn],len,bit[20],Case;
int f(int x){
    int res=0,w=1;
    while(x){
        int now=x%10;
        x/=10;
        res+=now*w;
        w*=2;
    }
    return res;
}
int dfs(int pos,int sum,int limit){
    if(pos==0)return sum<=all;
    if(sum>all)return 0;
    if(!limit&&dp[pos][all-sum]!=-1)return dp[pos][all-sum];
    int end=limit?bit[pos]:9;
    int ans=0;
    for(int i=0;i<=end;i++){
        ans+=dfs(pos-1,sum+i*(1<<(pos-1)),limit&&i==end);
    }
    if(!limit)dp[pos][all-sum]=ans;
    return ans;
}
int solve(int x){
    len=0;
    while(x){
        bit[++len]=x%10;
        x/=10;
    }
    return dfs(len,0,1);
}
int main(){
    //freopen("Cola.txt","r",stdin);
    memset(dp,-1,sizeof(dp));
    int n;
    scanf("%d",&n);
    for(Case=1;Case<=n;Case++){
        scanf("%d%d",&a,&b);
        all=f(a);
        printf("Case #%d: %d\n",Case,solve(b));
    }
}

 

posted @ 2017-08-18 20:20  Echo宝贝儿  阅读(274)  评论(0编辑  收藏  举报