标签:
f[pos][top]
统计数字个数
g[pos][top][digit]
统计数位(对于f[pos][top]中的所有数)
1 /************************************************************** 2 Problem: 1833 3 User: idy002 4 Language: C++ 5 Result: Accepted 6 Time:0 ms 7 Memory:808 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <cstring> 12 13 typedef long long poi; 14 15 poi lf, rg; 16 int aa[20], tot; 17 poi f[20][2], g[20][2][10]; 18 poi ans[2][10]; 19 20 void calc( poi v, poi ans[10] ) { 21 for( tot=0; v; v/=10 ) 22 aa[++tot] = v%10; 23 memset( f, 0, sizeof(f) ); 24 memset( g, 0, sizeof(g) ); 25 f[tot][1] = 1; 26 f[tot][0] = aa[tot]-1; 27 for( int i=tot; i>=2; i-- ) { 28 f[i-1][1] = 1; 29 f[i-1][0] = f[i][0]*10+9+f[i][1]*aa[i-1]; 30 } 31 g[tot][1][aa[tot]] = 1; 32 for( int i=1; i<aa[tot]; i++ ) 33 g[tot][0][i] = 1; 34 for( int i=tot; i>=2; i-- ) { 35 // for( int s=0; s<=9; s++ ) 36 // g[i-1][0][s] = g[i][0][s]*10+f[i][0]+(s!=0); 37 for( int s=0; s<=9; s++ ) { 38 g[i-1][0][s] += f[i][0]; 39 for( int ss=0; ss<=9; ss++ ) { 40 g[i-1][0][ss] += g[i][0][ss]; 41 } 42 } 43 for( int s=1; s<=9; s++ ) 44 g[i-1][0][s]++; 45 for( int s=0; s<aa[i-1]; s++ ) { 46 g[i-1][0][s] += f[i][1]; 47 for( int ss=0; ss<=9; ss++ ) { 48 g[i-1][0][ss] += g[i][1][ss]; 49 } 50 } 51 for( int s=0; s<=9; s++ ) 52 g[i-1][1][s] = g[i][1][s]+(aa[i-1]==s); 53 } 54 55 for( int i=0; i<=9; i++ ) 56 ans[i] = g[1][0][i]+g[1][1][i]; 57 } 58 59 int main() { 60 scanf( "%lld%lld", &lf, &rg ); 61 calc(rg,ans[1]); 62 if( lf>1 ) calc(lf-1,ans[0]); 63 for( int i=0; i<=9; i++ ) 64 printf( "%lld%s", ans[1][i]-ans[0][i], i==9 ? "" : " " ); 65 }
标签:
原文地址:http://www.cnblogs.com/idy002/p/4354833.html