HDU3709

来源:互联网 发布:java发送邮件工具类 编辑:程序博客网 时间:2024/06/05 13:28
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef __int64 ll;ll len,wei[22];ll dp[22][22][2005];//hdu3709平衡数ll dfs(ll pos,ll pre,ll o,ll limit){//limit代表当前数字有没有上限     if(pos==0){//已经遍历完每一位的时候                if(pre==0){return 1;}//修改部分        return 0;    }    if(pre<0)return 0;    if(limit==0&&dp[pos][o][pre]!=-1){return dp[pos][o][pre];}    ll ans=0;    ll ed=limit?wei[pos]:9;    for(ll i=0;i<=ed;i++){//修改部分        ll nextpre=pre+(pos-o)*i;        ans+=dfs(pos-1,nextpre,o,limit&&i==ed);    }    if(!limit)dp[pos][o][pre]=ans;//如果当前没有上限,保存dp值     return ans;}ll solve(ll x){    if(x==-1)return 0;    if(x==0)return 1;    ll t=x;    len=0;    while(t){        wei[++len]=t%10;        t/=10;    }    ll ans=0,i;     for(i=len;i>0;i--) //修改    ans+=dfs(len,0,i,1);//修改    ans-=(len-1);     return ans;}int main(){    ll t,l,r;    memset(dp,-1,sizeof(dp));    scanf("%I64d",&t);    while(t--){        scanf("%I64d%I64d",&l,&r);        printf("%I64d\n",solve(r)-solve(l-1));    }    return 0;}