Sicily 7969. Digit Sum
来源:互联网 发布:刺客信条起源优化 编辑:程序博客网 时间:2024/05/18 05:52
7969. Digit Sum
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
For a pair of integers a and b, the digit sum of the interval [a, b] is defined as the sum of all digits occurring in all numbers between (and including) a and b. For example, the digit sum of [28, 31] can be calculated as:
2+8 + 2+9 + 3+0 + 3+1 = 28
Given the numbers a and b, calculate the digit sum of [a, b].
Input
On the first line one positive number: the number of test cases, at most 100. After that per test
case:
- one line with two space-separated integers, a and b (0 <= a <= b <= 1015).
Output
Per test case:
- one line with an integer: the digit sum of [a, b].
Sample Input
30 1028 311234 56789
Sample Output
46281128600
Problem Source
2013年每周一赛第五场暨校赛模拟赛III/BAPC 2012
// Problem#: 7969// Submission#: 3593648// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University// Solution to Digit Sum// Author: Thomas Beuman// Time complexity: O(log b)// Memory: O(1)// @EXPECTED_RESULTS@: CORRECT/*Solution outline:Compute the digitsum from 0 to b and from 0 to a-1, and subtract the twoDetermine the digitsum from 0 to n-1 recursively:Example:digitsum(125) = digitsum(120) + sumofdigits(120) + sumofdigits(121) + ... + sumofdigits(124) digitsum(120) = 10 * digitsum(12) + (0+1+2+3+4+5+6+7+8+9) * 12 sumofdigits(120) + sumofdigits(121) + ... + sumofdigits(124) = 5 * sumofdigits(12) + (0+1+2+3+4)*/#include <cstdio>using namespace std;typedef long long i64;// Returns the sum of the digits of ni64 sumofdigits (i64 n){ return n ? n%10 + sumofdigits(n/10) : 0;}// Returns the sum of all digits from 0 to n-1i64 digitsum (i64 n){ return n < 10 ? n*(n-1)/2 : digitsum(n%10) + 45 * (n/10) // Last digit + (n%10) * sumofdigits(n/10) + 10 * digitsum(n/10); // Other digits}int main(){ int cases, casenr; i64 a, b; scanf("%d", &cases); for (casenr = 1; casenr <= cases; casenr++) { scanf("%lld %lld", &a, &b); printf("%lld\n", digitsum(b+1) - digitsum(a)); } return 0;}
0 0
- Sicily 7969. Digit Sum
- sicily--1325. Digit Generator
- sicily 1325. Digit Generator
- Sicily 1325. Digit Generator
- Sicily 4315. Digit Count
- [sicily]1325. Digit Generator
- sicily 1325. Digit Generator
- Sicily 1325. Digit Generator
- sicily 1268(Single Digit Adder)
- sicily 1608. Digit Counting || 4315
- Sicily 1268. Single Digit Adder
- [Sicily] sicily 1091. Maximum Sum
- lucky sum of digit
- [projecteuler]Factorial digit sum
- HDU 5710 Digit-Sum
- HDU 5710 Digit-Sum
- HDU5710-Digit-Sum
- Digit-Sum HDU
- 牛腩新闻发布系统--阶段总结(一)
- CocoaPods - 第三方类库管理工具
- DeepLearning tutorial(4)CNN卷积神经网络原理简介+代码详解
- java堆和栈
- Linux vmstat命令实战详解
- Sicily 7969. Digit Sum
- sqlserver缓存计划-清除只使用一次的缓存计划
- 用jQuery和PHP来实现微信转盘抽奖程序 附源代码
- Linux dd命令中dsync与fdatasync的区别
- 前端面试题--网易邮箱
- Windows 进程的句柄表
- pdb文件
- yii2.0 user功能解析
- SpringMVC运行机制