bzoj1833 [ZJOI2010]count 数字计数

【bzoj1833】[ZJOI2010]count 数字计数

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。

这道题51nod 2级算法题(数据比这题要大),但是居然是省选题,无语了。这个很好推的。
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<cstring>
 6 using namespace std;
 7 
 8 typedef long long LL;
 9 
10 LL st,ed,ans[10]={0};
11 
12 LL make(LL num,int now)
13 {
14     LL res=0,tail=0,mi=1;
15     if (num<10&&now==0) return 0;
16     while (num!=0)
17     {
18         if (now==0&&num<10) break;
19         int x=num%10;
20         num/=10;
21         res+=(num-(now==0))*mi;
22         if (x>now) res+=mi;
23         if (x==now) res+=tail+1;
24         tail=(LL)x*mi+tail,mi*=10;        
25     }
26     return res;
27 }
28 int main()
29 {
30     scanf("%lld%lld",&st,&ed);
31     for (int i=0;i<=9;i++)
32     {
33         ans[i]=make(ed,i)-make(st-1,i);
34     }
35     for(int i=0;i<=8;i++)
36         cout<<ans[i]<<" ";
37     cout<<ans[9];     
38 }

 

posted @ 2017-08-28 16:47  Kaiser-  阅读(209)  评论(0编辑  收藏  举报