P2602 [ZJOI2010]数字计数
https://www.luogu.org/problemnew/show/P2602
数位dp
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 const double minv=1e-10; 5 6 ll g[10],shi[15]; 7 8 void cal(ll a,ll c,int ori) 9 { 10 int i,w,s; 11 w=(log(a+minv)/log(10)); 12 i=w; 13 while (i>=0) 14 g[0]-=shi[i--]*c; 15 while (w>=0) 16 { 17 s=a/shi[w]; 18 if (w!=0) 19 for (i=0;i<10;i++) 20 g[i]+=s*shi[w-1]*w*c; 21 for (i=0;i<s;i++) 22 g[i]+=shi[w]*c; 23 g[s]+=(a%shi[w]+1)*c; 24 a=a%shi[w]; 25 w--; 26 } 27 } 28 29 int main() 30 { 31 ll a,b; 32 int i; 33 scanf("%lld%lld",&a,&b); 34 shi[0]=1; 35 for (i=1;i<=12;i++) 36 shi[i]=shi[i-1]*10; 37 cal(b,1,0); 38 cal(a-1,-1,0); 39 for (i=0;i<10;i++) 40 { 41 printf("%lld",g[i]); 42 if (i!=10) 43 printf(" "); 44 } 45 return 0; 46 } 47 /* 48 1 999 49 5 10 50 10 20 51 10 19 52 1 1000000000000 53 99 1000000000000 54 */ 55 /* 56 13 99 57 8 15 18 19 19 19 19 19 19 19 58 13 550 59 104 210 213 214 214 156 104 104 104 104 60 2 110 61 21 32 21 21 21 21 21 21 21 21 62 1 100000000 63 68888897 80000001 80000000 80000000 80000000 80000000 80000000 80000000 80000000 80000000 64 */
验证
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define minv 1e-6 5 #define inf 1e9 6 #define pi 3.1415926536 7 #define nl 2.7182818284 8 const ll mod=1e9+7;//998244353 9 const int maxn=1e5+10; 10 11 ll g[10]; 12 13 int main() 14 { 15 int i,j; 16 ll a,b; 17 scanf("%lld%lld",&a,&b); 18 for (i=a;i<=b;i++) 19 { 20 j=i; 21 while (j) 22 { 23 g[j%10]++; 24 j/=10; 25 } 26 } 27 for (i=0;i<10;i++) 28 printf("%lld ",g[i]); 29 return 0; 30 } 31 /* 32 13 99 33 8 15 18 19 19 19 19 19 19 19 34 13 550 35 104 210 213 214 214 156 104 104 104 104 36 2 110 37 21 32 21 21 21 21 21 21 21 21 38 1 100000000 39 68888897 80000001 80000000 80000000 80000000 80000000 80000000 80000000 80000000 80000000 40 */