Description
Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?
Input
Input starts with an integer T (≤ 11000), denoting the number of test cases.Each case contains two unsigned 32-bit integers m and n, (m ≤ n).
Output
For each case, print the case number and the number of zeroes written down by Jimmy.
Sample Input
510 11100 2000 5001234567890 23456789010 4294967295
Sample Output
Case 1: 1Case 2: 22Case 3: 92Case 4: 987654304Case 5: 3825876150
给出区间[m,n],求区间内的所有数共有多少个0。
设dp[i][j]表示处理到第i位时,它前面共有j个0(除了前导零)。
1 // 110,101,100,011,010,001,000 2 #pragma comment(linker, "/STACK:1024000000,1024000000") 3 #include<iostream> 4 #include<cstdio> 5 #include<cstring> 6 #include<cmath> 7 #include<math.h> 8 #include<algorithm> 9 #include<queue> 10 #include<set> 11 #include<bitset> 12 #include<map> 13 #include<vector> 14 #include<stdlib.h> 15 using namespace std; 16 #define ll long long 17 #define eps 1e-10 18 #define MOD 1000000007 19 #define N 1000000 20 #define inf 1e12 21 ll a,b; 22 ll dp[26][26]; 23 int dig[26]; 24 ll dfs(int len,/*转化为二进制数的位数*/ int first,/*1表示目前前导都为0*/ int sta,/*sta表示前面有几个0*/ int up/*up来判断每一位的取值,1的时候只能取原定值,0的时候取0~9的任意值*/){ 25 if(len==0){//当算到最后一位的时候 26 if(first){ 27 return (ll)1; 28 }else{ 29 return (ll)sta; 30 } 31 } 32 if(!up && dp[len][sta]!=-1 && !first){//记忆化,之前算过的直接返回值,不再继续算 33 return dp[len][sta]; 34 } 35 36 int n=up?dig[len]:9;//如果up的值为1,只能取dig[len],比如值为123,up值为1即第一位为1了,那么n的值最大为2,而不能为9 37 ll res=0; 38 for(int i=0;i<=n;i++){ 39 if(first){//如果前导都为0时,sta=0,up值的确定取决于是否是n 40 res+=dfs(len-1,first&&i==0,0,up&&i==n); 41 }else{ 42 if(i==0){// 这里判断的是如果i=0,前面的0的个数+1 43 res+=dfs(len-1,0,sta+1,up&&i==n); 44 }else{ 45 res+=dfs(len-1,0,sta,up&&i==n); 46 } 47 48 } 49 } 50 if(!up && !first){ 51 dp[len][sta]=res; 52 } 53 return res; 54 } 55 ll cal(ll num){ 56 int len=0; 57 if(num == 0){//如果值为0,则只有一位数0 58 dig[++len] = 0; 59 } 60 while(num){ 61 dig[++len] = num % 10; 62 num/=10; 63 } 64 return dfs(len,1,0,1); 65 } 66 int main() 67 { 68 int t; 69 int ac=0; 70 scanf("%d",&t); 71 while(t--){ 72 scanf("%lld%lld",&a,&b); 73 memset(dp,-1,sizeof(dp)); 74 printf("Case %d: ",++ac); 75 printf("%lld\n",cal(b)-cal(a-1)); 76 77 } 78 return 0; 79 }