【题目描述】
原题来自:ZJOI 2010
给定两个正整数 a 和 b,求在 [a,b] 中的所有整数中,每个数码 (digit) 各出现了多少次。
【输入】
仅包含一行两个整数 a,b,含义如上所述。
【输出】
包含一行 10 个整数,分别表示 0∼9 在 [a,b] 中出现了多少次。
【输入样例】
1 99
【输出样例】
9 20 20 20 20 20 20 20 20 20
【提示】
数据范围与提示:
30% 的数据中,1≤a≤b≤106 ;
%100% 的数据中,1≤a≤b≤1012 。
#include<iostream>
#include<cstring>
using namespace std;
long long digit[25];
#define D digit[0]
#define ll long long
struct node
{
ll f[10],index;
node(){
for(ll i=0;i<10;i++)
f[i]=0;
index=0;
}
}dp[25][25];
node dfs(ll pos,bool done,ll res,bool p)
{
if(!pos){
node ps;
if(p)
ps.index=1;
return ps;
}
if(!done&&p&&dp[pos][res].index)
return dp[pos][res];
ll ends=done?digit[pos]:9;
node now,ans;
ans.index=0;
for(ll i=0;i<=ends;i++)
{
now=dfs(pos-1,done&&i==ends,i,p|i);
ans.index+=now.index;
if(p|i)
ans.f[i]+=now.index;
for(ll j=0;j<10;j++)
ans.f[j]+=now.f[j];
}
if(!done&&p)dp[pos][res]=ans;
return ans;
}
node get_digit(ll x)
{
D=0;
while(x)
{
digit[++D]=x%10;
x/=10;
}
return dfs(D,1,0,0);
}
int main()
{
ll a,b;
cin>>a>>b;
node now,ing;
now=get_digit(b);
ing=get_digit(a-1);
for(ll i=0;i<10;i++){
cout<<now.f[i]-ing.f[i]<<' ';
}
cout<<endl;
}