51Nod 1042 数字0-9的数量 数位DP

 1 #include <iostream>
 2 #include <algorithm>
 3 #define ll long long  
 4 using namespace std;
 5 
 6 void dfs(ll a, ll b, ll c[])
 7 {
 8     ll n = a / 10, m = a % 10, t = n;
 9     for (int i = 0; i <= m; i++)
10         c[i] += b;                //当前位对低位的影响  
11     for (int i = 0; i < 10; i++)
12         c[i] += b*n;            //高位对低位的影响
13     c[0] -= b;                    //0特殊处理,将多算的0减去  
14     while (t){                    //当前位对高位的影响  
15         c[t % 10] += b*(m + 1);    //加上0  
16         t /= 10;
17     }
18     if (n)  
19         dfs(n - 1, b * 10, c);    //n已经处理过,所以要处理n-1  
20 }
21 
22 ll x[20], y[20];
23 
24 int main()
25 {
26     std::ios::sync_with_stdio(false);
27     ll a, b;
28     cin >> a >> b;
29     dfs(a - 1, 1, x);
30     dfs(b, 1, y);
31     for (int i = 0; i<10; i++) 
32         cout << y[i] - x[i] << endl;
33     return 0;
34 }
原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8074785.html