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;
}