24h購物| | PChome| 登入
2012-11-11 11:26:10| 人氣2,095| 回應0 | 上一篇 | 下一篇

[UVA][math] 12517 - Digit Sum

推薦 0 收藏 0 轉貼0 訂閱站台


 Digit Sum 

The security system for Associated Computer Informatics Systems (ACIS) has aninteresting test to check the identity for authorized personal. These persons have got a piece ofsoftware that allowed them to calculate, given two integer positive numbers M and N, what is thesum of the decimal digits in the sequence M, M + 1, ..., N. Then, when somebody istrying to access ACIS system, he/she is asked to answer the question of the sum for some M and Nthat are provided at that moment, by means of the given software.


ACIS programmers have developed a rather naïve algorithm only to verify that themethod calculates the right answer. Now they are interested in developing a faster algorithm, inorder to stop unauthorized users (who may be detected because they do not answer the sum questionfast enough). And then you have been hired to help ACIS programmers to find such a method.

Input 

The problem input consists of several cases, each one defined by a line with two integernumbers, M and N, without leading blanks and separated by a blank. You may assume that1 $ leq$ M $ leq$ N $ leq$ 109. The end of the input is signaled by a line with two zerovalues.

Output 

For each case, output a line with the sum of the decimal digits for the sequenceM, M + 1, ..., N.

Sample Input 

3 8
5 18
1 50
0 0

Sample Output 

33
80
330

#include <stdio.h>
#define LL long long
LL calc(LL n, LL dep, LL lef) {
if(n == 0) return 0;
LL m = n%10;
return n/10*45*dep + m*lef + m*(m-1)/2*dep + calc(n/10, dep*10, lef+m*dep);
}
int main() {
int m, n;
while(scanf("%d %d", &m, &n) == 2 && n)
printf("%lld\n", calc(n,1,1)-calc(m-1,1,1));
return 0;
}

台長: Morris
人氣(2,095) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12527 - Different Digits
此分類上一篇:[ACM-ICPC][排容、逆元] 5701 - The Boss on Mars

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文