[luogu2602 ZJOI2010] 数字计数 (数位dp)

传送门

Description

给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次。

Input

输入文件中仅包含一行两个整数a、b,含义如上所述。

Output

输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。

Sample Input

1 99

Sample Output

9 20 20 20 20 20 20 20 20 20

HINT

30%的数据中,a<=b<=10^6;

100%的数据中,a<=b<=10^12。

Solution

首先考虑不算前导零那么
同一位上每种数字的情况数均相同为
\(f[i]=f[i-1]*10+10^{i-1}\)
然后具体算时考虑各种情况就行了(懒得打了qwq)

Code

//By Menteur_Hxy
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;

LL a,b;
LL f[20],cnt[2][20],num[20],ten[20];

void search(LL x,LL cnt[20]) {
	int len=0; memset(num,0,sizeof(num));
	while(x) num[++len]=x%10,x/=10;
	for(int i=len;i>=1;i--) {
		for(int j=0;j<=9;j++) cnt[j]+=f[i-1]*num[i];
		for(int j=0;j<num[i];j++) cnt[j]+=ten[i-1];
		LL num2=0;
		for(int j=i-1;j>=1;j--) num2=num2*10+num[j];
		cnt[num[i]]+=num2+1;
		cnt[0]-=ten[i-1];
	}
}

int main() {
	scanf("%lld%lld",&a,&b);
	ten[0]=1;
	for(int i=1;i<=15;i++) f[i]=f[i-1]*10+ten[i-1],ten[i]=10*ten[i-1];
	search(a-1,cnt[0]);
	search(b,cnt[1]);
	for(int i=0;i<=9;i++) printf("%lld ",cnt[1][i]-cnt[0][i]);
	return 0;
}
posted @ 2018-07-26 20:29  Menteur_hxy  阅读(285)  评论(0编辑  收藏  举报