LightOJ - 1140 How Many Zeroes?

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

5

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

 

Sample Output

Case 1: 1

Case 2: 22

Case 3: 92

Case 4: 987654304

Case 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 } 
View Code

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值