Uva 10994 - Simple Addition
*** At first i have just found that what is the reminder sum result from 1 to the last range. And then, found the reminder sum result from 1 to the first-1 range. And finally, I have have got the AC result to do Subtraction operation between them :)
#include<bits/stdc++.h> using namespace std; typedef long long LL; // Digit_Sum (SPOJ) //int sum(int n) //{ // if(n<10)return ( (n* (n+1)) / 2 ); // int t=n,i=0,x; // while(n/10 !=0) // { // //x=n%10; // i++; // n/=10; // } // // int p = powl(10,i); // // return ( (n*i*45*p/10) + (n* (n-1) * p/2 ) + n * (t%p + 1) + sum(t%p)); //} // //int main() //{ // int x,y; // while(cin >> x >> y) // { // cout << sum(y) - sum(x-1) << endl; // } // // return 0; //} LL mod_sum(LL n) { LL ans=0; while(n) { LL x = n%10; n/=10; ans+=( (x*(x+1)) / 2 ) + (n * 45); } return ans; } int main() { LL x,y; while(cin >> x >> y) { if(x<0 and y<0)break; cout << mod_sum(y) - mod_sum(x-1) << endl; } return 0; }
Comments
Post a Comment