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

hdu3734(数位dp)

时间:2018-08-15 16:57:32      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:turn   ++   show   std   ref   ons   c代码   for   php   

src:http://acm.hdu.edu.cn/showproblem.php?pid=4734

ac代码:

#include<bits/stdc++.h>
using namespace std;
#define per(i,a,b) for(int i=a;i <= b;i++)
#define max(a,b) a=max(a,b)
#define min(a,b) a=min(a,b)
#define sz(x) (int)x.size()
typedef long long ll;
ll gcd(ll a,ll b){while(b){ll t=b;b=a%b;a=t;}return a;}
const int inf=0x3f3f3f3f;
const int mod=1000000007;
#define siz 40005
int T,A,B,pos,a[15],all,dp[15][4600];
int f(int u)
{
    if(u==0)return 0;
    return u%10+2*f(u/10);
}
int dfs(int pos,int sum,bool limit)
{
    if(sum>all)return 0;
    if(pos==-1)return 1;
    if(!limit&&dp[pos][all-sum]!=-1)return dp[pos][all-sum];
    int up=limit?a[pos]:9;
    int ans=0;
    for(int i=0;i<=up;i++){
        ans+=dfs(pos-1,sum+i*(1<<pos),limit&&i==a[pos]);
    }
    if(!limit)dp[pos][all-sum]=ans;
    return ans;
}
int solve(int u)
{
    pos=0;
    while(u){
        a[pos++]=u%10;
        u/=10;
    }
    return dfs(pos-1,0,true);
}

int main()
{
    memset(dp,-1,sizeof(dp));
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%d %d",&A,&B);
        all=f(A);
        printf("Case #%d: ",cas);
        printf("%d\n",solve(B));
    }
    return 0;
}

 

hdu3734(数位dp)

标签:turn   ++   show   std   ref   ons   c代码   for   php   

原文地址:https://www.cnblogs.com/WindFreedom/p/9482092.html

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