How many 0's?
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3207 | Accepted: 1707 |
Description
A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he write down?
Input
Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, m ≤ n. The last line of input has the value of m negative and this line should not be processed.
Output
For each line of input print one line of output with one integer number giving the number of 0's written down by the monk.
Sample Input
10 11 100 200 0 500 1234567890 2345678901 0 4294967295 -1 -1
Sample Output
1 22 92 987654304 3825876150
Source
Waterloo Local Contest, 2006.5.27
#include<cstdio>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
ll b[12] = { 1, 10, 100, 1000, 10000, 100000, 1000000,10000000, 100000000, 1000000000, 10000000000, 100000000000 };
ll count_num ( ll n, ll id )
{
ll left, m, sum = 0;
for ( int i = 1; i < 12; i++ )
{
left = n / b[i] - (id==0);
sum += left * b[i-1];
m = (n % b[i] - n % b[i-1]) / b[i-1]; //求出从第到高的第i位上的具体数字
if ( m > id ) sum += b[i-1];
else if ( m == id ) sum += n % b[i-1] + 1;
if ( n < b[i] ) break;
}
return sum;
}
int main()
{
ll n,m;
while(cin>>n>>m&&m>=0)
{
cout<<count_num(m,0)-count_num(n-1,0)<<endl;
}
}