//To understand the proceedings please visit http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sumOfDigitsFrom1ToN(ll n)
{
if (n<10)
return n*(n+1)/2;
ll d=log10(n);
ll *a=new ll[d+1];
a[0]=0,a[1]=45;
for (ll i=2;i<=d;i++)
a[i]=a[i-1]*10+45*ceil(pow(10,i-1));
ll p=ceil(pow(10,d));
int msd = n/p;
return msd*a[d]+(msd*(msd-1)/2)*p+
msd*(1+n%p)+sumOfDigitsFrom1ToN(n%p);
}
int main()
{
ll i,j,k,l,m,n;
while(cin>>n>>m)
{
if(n==-1&&m==-1)
break;
else
{
ll ans=sumOfDigitsFrom1ToN(max(n,m))-sumOfDigitsFrom1ToN(min(n,m)-1);
cout<<ans<<endl;
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll sumOfDigitsFrom1ToN(ll n)
{
if (n<10)
return n*(n+1)/2;
ll d=log10(n);
ll *a=new ll[d+1];
a[0]=0,a[1]=45;
for (ll i=2;i<=d;i++)
a[i]=a[i-1]*10+45*ceil(pow(10,i-1));
ll p=ceil(pow(10,d));
int msd = n/p;
return msd*a[d]+(msd*(msd-1)/2)*p+
msd*(1+n%p)+sumOfDigitsFrom1ToN(n%p);
}
int main()
{
ll i,j,k,l,m,n;
while(cin>>n>>m)
{
if(n==-1&&m==-1)
break;
else
{
ll ans=sumOfDigitsFrom1ToN(max(n,m))-sumOfDigitsFrom1ToN(min(n,m)-1);
cout<<ans<<endl;
}
}
return 0;
}
No comments:
Post a Comment