Title Link: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042
Title: Give an interval of a-B to count the number of occurrences within this interval of 0-9. For example, 10-19,1 appears 11 times (10,11,12,13,14,15,16,17,18,19, of which 11 includes 2 1), and the remaining numbers appear 1 times. Input
Two number A, a (1 <= a <= b <= 10^18)
Output
Output total 10 rows, respectively, 0-9 occurrences
This problem feels very troublesome, tangled for a long time.
We use recursion to consider each one. Into the already calculated low, current bit, not yet calculated high.
There are only a few cases when dealing with the current bit:
1, the current position and low-level relationship
2, the relationship between high and low
3, the current relationship with the high
Three cases can be processed. Note that 0 of special cases, such as 01 in fact, do not have 0.
#include <iostream>
#include <bits/stdc++.h>
#define LL Long long
using namespace std;
void Dfs (LL a,ll b,ll c[])
{
ll n=a/10,m=a%10,t=n;
for (int i=0;i<=m;i++) c[i]+=b;//Current bit-to-low effect for
(int i=0;i<10;i++) c[i]+=b*n;//High-to-low effect
c[0] -=b;//0 special treatment, the number of 0 minus while
(t)//The current bit of the effect of the high
(c[t%10]+=b* (m+1);//Plus 0
t/=10;
}
if (n) dfs (N-1,B*10,C);//n has been processed, so to process n-1
}
ll x[20],y[20];
int main ()
{
ll A, B;
cin>>a>>b;
DFS (a-1,1,x);
DFS (b,1,y);
for (int i=0;i<10;i++) cout<<y[i]-x[i]<<endl;
}