Light oj 1140 How Many Zeroes?

Posted songorz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Light oj 1140 How Many Zeroes?相关的知识,希望对你有一定的参考价值。

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

题解:数位DP入门;我们先处理处数字的每一位,然后对每一位考虑,同时记录是否

有前导零,以及前导零的个数;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<string>
using namespace std;
typedef long long LL;
LL dp[40][40],digit[40],n,m;
int T;

LL dfs(int pos,int pre,int cnt,bool Judge)
{
    if(pos==0) return cnt;
    int sz=Judge? digit[pos]:9;
    if(!Judge && dp[pos][cnt]!=-1 && pre) return dp[pos][cnt];
    LL ans=0;
    for(int i=0;i<=sz;i++)
    {
        if(i==0&&pre) ans+=dfs(pos-1,1,cnt+1,Judge&&i==sz);
        else ans+=dfs(pos-1,i!=0||pre,cnt,Judge&&i==sz);
    } 
    
    if(!Judge&&pre) dp[pos][cnt]=ans;
    return ans;
}

LL work(LL num)
{
    int temp=0;
    if(num==-1) return -1;
    while(num)
    {
        digit[++temp]=num%10;
        num/=10;
    }
    return dfs(temp,0,0,1);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>T;
    for(int i=1;i<=T;i++)
    {
        memset(dp,-1,sizeof dp);
        cin>>m>>n;
        cout<<"Case "<<i<<": "<<work(n)-work(m-1)<<endl;
    }
    return 0;
}

 

以上是关于Light oj 1140 How Many Zeroes?的主要内容,如果未能解决你的问题,请参考以下文章

[暑假集训--数位dp]LightOJ1140 How Many Zeroes?

How Many Zeroes? LightOJ - 1140

light oj 1058

(light OJ 1005) Rooks dp

Light oj 1422 - Halloween Costumes

(状压) Brush (IV) (Light OJ 1018)