Light oj 1140 - How Many Zeroes?(数位dp)

本文探讨了在指定数字区间内,从整数到十进制表示过程中,数字0出现的次数。通过输入测试案例数量及具体区间,输出对应区间内数字0的总数。示例包括边界条件和特殊感谢。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1140 - How Many Zeroes?
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for Sample Input

5

10 11

100 200

0 500

1234567890 2345678901

0 4294967295

Case 1: 1

Case 2: 22

Case 3: 92

Case 4: 987654304

Case 5: 3825876150

 


SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)

爆  int, 刚开始郁闷一会才发现


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;

using namespace std;

#define INF 0x3f3f3f3f
#define N 15

ll dp[N][N];

int bit[N];

ll dfs(int pos,int st,int num,bool bound)
{
     if(pos==-1) return st ? num:1;
     if(st&&!bound&&dp[pos][num]!=-1) return dp[pos][num];
     int up= bound ? bit[pos]:9;
     ll ans=0;
     for(int i=0;i<=up;i++)
     {
         if(i==0)
         {
             if(st)
                ans+=dfs(pos-1,st,num+1,bound&&i==up);
             else
                ans+=dfs(pos-1,0,0,bound&&i==up);
         }
         else
            ans+=dfs(pos-1,1,num,bound&&i==up);
     }

     if(st&&!bound)
          dp[pos][num]=ans;
     return ans;
}

ll solve(ll x)
{
    int len=0;
    if(x<0) return 0;
    if(x==0) return 1;
    while(x)
    {
        bit[len++]=x%10;
        x/=10;
    }
////    for(int i=len-1;i>=0;i--)
////        printf("%d",bit[i]);
////    printf("\n");
    return dfs(len-1,0,0,true);
}

int main()
{
    int i,j,t,ca=0;
    ll le,ri;
     memset(dp,-1,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&le,&ri);
        ll f=solve(ri);
        ll ff=solve(le-1);
       // printf("%lld %lld\n",f,ff);
        printf("Case %d: %lld\n",++ca,f-ff);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值