Light oj 1140 - How Many Zeroes? 数位dp

1140 - How Many Zeroes?
    PDF (English) Statistics Forum
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)

求任意区间的数中0的出现次数

注意判断是否是前导0   //用lld

ACcdoe

#include <cstdio>
#include <cstring>
#define ll long long
ll dp[22][22];
int data[22];
int cnt=1;
ll dfs(int len,int sum,int is_0,int limit){
    if(!len)return is_0?1:sum;
    if(!limit&&dp[len][sum]!=-1&&!is_0)return dp[len][sum];
    int ed=limit?data[len]:9;
    ll ans=0;
    for(int i=0;i<=ed;++i)
        if(is_0)ans+=dfs(len-1,0,i==0,limit&&i==ed);
        else ans+=dfs(len-1,sum+(i==0),0,limit&&i==ed);
    return   (is_0||limit)?ans:dp[len][sum]=ans;
}
ll fun(ll n){
    int len=0;
    while(n){
        data[++len]=n%10;
        n/=10;
    }
    return dfs(len,0,1,1);
}
void doit(){
    ll x,y;
    scanf("%lld%lld",&x,&y);
    printf("Case %d: %lld\n",cnt++,fun(y)-fun(x-1));
}
int main(){
    int loop;
    scanf("%d",&loop);
    memset(dp,-1,sizeof(dp));
    while(loop--)doit();
}


你可能感兴趣的:(C++)