1833: [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。
代码如下:(谁能告诉我为什么把主函数里的i定义到for里面就WA了)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
long long ans[10],f[20];
inline void Resolve(long long x,long long pos)
{
while(x)
ans[x%10]+=pos,x/=10;
}
void Digital_DP(long long x,int flag)
{
int i,j;
long long pos,now;
for(i=1,pos=10;pos<x;++i,pos*=10)
{
for(j=0;j<=9;j++)
ans[j]+=f[i-1]*9*flag;
for(j=1;j<=9;j++)
ans[j]+=pos/10*flag;
}
now=pos/=10;--i;
while(now<x)
{
while(now+pos<=x)
{
long long temp=now/pos;
Resolve(temp,pos*flag);
for(j=0;j<=9;j++)
ans[j]+=f[i]*flag;
now+=pos;
}
pos/=10;--i;
}
}
int main()
{
int i,j;
long long a,b,pos;
f[1]=1;
for(i=2,pos=10;i<=12;i++,pos*=10)
f[i]=f[i-1]*10+pos;
scanf("%lld%lld",&a,&b);
Digital_DP(b+1,1);
Digital_DP(a,-1);
for(j=0;j<=9;j++)
printf("%lld%c",ans[j],j==9?'\n':' ');
return 0;
}