码迷,mamicode.com
首页 > 其他好文 > 详细

Light oj 1140 How Many Zeroes?

时间:2018-08-10 11:01:02      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:ons   map   integer   ack   ima   any   not   numbers   temp   

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?

标签:ons   map   integer   ack   ima   any   not   numbers   temp   

原文地址:https://www.cnblogs.com/songorz/p/9452832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!