标签:ret contest some sub sample 题解 rate cti tput
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 7994 Accepted Submission(s): 3816
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
ll n,m;
ll dp[20][20][2000];//多加一维表示力矩
int bit[20];
ll dfs(int pos,int center,int sum,bool limit)
{
if(pos==-1) return sum==0;
if(sum<0)return 0;//剪枝
if(!limit&&dp[pos][center][sum]!=-1) return dp[pos][center][sum];
int up=limit?bit[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
ans+=dfs(pos-1,center,sum+(pos-center)*i,limit&&i==up);
if(!limit) dp[pos][center][sum]=ans;
return ans;
}
ll calc(ll n)
{
int len=0;
while(n)
{
bit[len++]=n%10;
n/=10;
}
ll ans=0;
for(int i=len-1;i>=0;i--)
ans+=dfs(len-1,i,0,true);
return ans-(len-1);//排除0、00、000....的干扰
}
int main()
{
int T;
scanf("%d",&T);
memset(dp,-1,sizeof(dp));
while(T--)
{
scanf("%lld %lld",&n,&m);
printf("%lld\n",calc(m)-calc(n-1));
}
return 0;
}
hdu3709 Balanced Number (数位dp)
标签:ret contest some sub sample 题解 rate cti tput
原文地址:https://www.cnblogs.com/zhgyki/p/9757636.html