1833: [ZJOI2010]count 数字计数

1833: [ZJOI2010]count 数字计数

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 2951  Solved: 1307
[Submit][Status][Discuss]

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。

Source

//数位dp:[l,r]=[1,r+1)-[1,l) 
//论文:刘聪 《浅谈数位类统计问题》 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a,b,f[20],c[20];
void dp(ll x,ll flag){
    int i,j,k;ll pos,now;
    for(i=1,pos=10;pos<x;i++,pos*=10){
        for(j=0;j<=9;j++) f[j]+=c[i-1]*9*flag;
        for(j=1;j<=9;j++) f[j]+=pos/10*flag;
    }
    for(i--,now=(pos/=10);now<x;pos/=10,i--){
        for(;now+pos<=x;now+=pos){
            ll tmp=now/pos;
            for(;tmp;tmp/=10) f[tmp%10]+=pos*flag;
            for(j=0;j<=9;j++) f[j]+=c[i]*flag;
        }
    }
}
int main(){
    int i;ll pos;
    c[1]=1;
    for(i=2,pos=10;i<=12;i++,pos*=10)
        c[i]=c[i-1]*10+pos;
    cin>>a>>b;
    dp(b+1,1);
    dp(a,-1);
    for(i=0;i<=9;i++){
        cout<<f[i];
        if(i<9) cout<<' ';
    }
    return 0;
}
原文地址:https://www.cnblogs.com/shenben/p/6288039.html