2 0 9 7604 24324
10 897
数位DP,可以参考本博客另外两篇博文:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
//dp[i][j][k],i表示位数,j表示去掉的点的位置,k表示两遍数的计算结果的差
LL dp[20][20][2500];
int num[50];
LL dfs(int pos,int center,int cha,bool limit)
{
if(pos==0) return cha==0;//结果差为0则返回1
if(cha<0) return 0;
if(!limit && dp[pos][center][cha]!=-1) return dp[pos][center][cha];
int end = limit?num[pos]:9;
LL ans = 0;
for(int i = 0;i <= end;i++)
{
ans+=dfs(pos-1,center,cha+i*(pos-center),limit&&(i==end));
}
if(!limit)
dp[pos][center][cha] = ans;
return ans;
}
LL work(LL n)
{
int len = 0;
while(n)
{
num[++len] = n%10;
n/=10;
}
LL ans = 0;
for(int i = 1;i <= len;i++)
{
ans+=dfs(len,i,0,true);
}
return ans-len+1;
}
int main()
{
int T;
scanf("%d",&T);
LL a,b;
while(T--)
{
memset(dp,-1,sizeof(dp));
scanf("%I64d %I64d",&a,&b);
printf("%I64d\n",work(b)-work(a-1));
}
return 0;
}
HDU 3709 Balanced Number,布布扣,bubuko.com
原文地址:http://blog.csdn.net/demonstrate8/article/details/38659133