1. 程式人生 > >51Nod 1042 數字0-9的數量 數位DP

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

數位 數量 int efi define pre iostream with n)

 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 }

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