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 |
写错一个小地方。。。。然后调了很久。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
ll n,m;
vector<int> digit;
ll pow10[25];
ll dp[25];
ll getSum(int pos){
ll sum = 0;
for(int i = pos; i >= 0; i--){
sum *= 10;
sum += digit[i];
}
return sum+1;
}
ll dfs(int pos,int zero,int done){
if(pos==-1) return 0;
if(!done &&!zero && ~dp[pos]) return dp[pos];
ll res = 0;
int end = done?digit[pos]:9;
for(int i = 0; i <= end; i++){
if(i==0){
if(done&&i==end){
if(pos==0) res++;
else if(!zero)res += getSum(pos);
}else{
if(pos==0) res++;
else if(!zero) res += pow10[pos];
}
}
res += dfs(pos-1,zero&&i==0,done&&i==end);
}
if(!done && !zero) dp[pos] = res;
return res;
}
ll solve(ll x){
digit.clear();
if(x==0) digit.push_back(0);
while(x){
digit.push_back(x%10);
x /= 10;
}
return dfs(digit.size()-1,1,1);
}
void init(){
memset(dp,-1,sizeof dp);
pow10[0] = 1;
for(int i = 1; i <= 24; i++){
pow10[i] = pow10[i-1]*10;
}
}
int main(){
int ncase,T=1;
cin >> ncase;
init();
// cout<<pow10[14]<<endl;
while(ncase--){
cin >> n >> m;
printf("Case %d: ",T++);
if(n==0){
cout<<solve(m)<<endl;
}
else{
cout<<solve(m)-solve(n-1)<<endl;
}
}
return 0;
}