How many 0's?
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1744 | Accepted: 816 |
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
#include "iostream"
#include "cstring"
#include "cstdio"
#include "cmath"
using namespace std;
__int64 n,m;
struct node
{
__int64 num[12];
}cnt,res;
void prntf(node nn)
{
printf("%I64d/n",nn.num[0]);
}
node countnum(__int64 t)//过程(1-t) 中(0-9)的总个数
{
if(t==-1) {node tt;memset(tt.num,0,sizeof(tt.num));tt.num[0]=-1;return tt;}
__int64 times=1,temp,td;
__int64 i,j,start;
node tmp;
memset(tmp.num,0,sizeof(tmp.num));
while (t>=times)
{
td=t/(times*10);
for (i=0;i<=9;i++)
tmp.num[i]+=td*times;
if (td) tmp.num[0]-=times;
temp=(t/times)%10;//当前尾数
if (t>=times*10)//若只剩一位数的时候0是不要算的 因为我们是从1开始的
j=0;
else j=1;
for (i=j;i<temp;i++)
tmp.num[i]+=times;
tmp.num[temp]+=t%times+1;
times*=10;
}
return tmp;
}
void solve()
{
__int64 i,j;
node a,b;
a=countnum(n-1);a.num[0]++;
b=countnum(m);b.num[0]++;
node c;
for (i=0;i<10;i++)
c.num[i]=b.num[i]-a.num[i];
prntf(c);
}
int main()
{
//freopen("R:/in.txt","r",stdin);
while (scanf("%I64d%I64d",&n,&m)!=EOF)
{
if (n==-1&&m==-1) break;
solve();
}
}