UVA10994 Simple Addition【前缀和】

Lets define a simple recursive function F(n), where
在这里插入图片描述
    Lets define another function S(p, q),
在这里插入图片描述
    In this problem you have to Calculate S(p, q) on given value of p and q.
Input
The input file contains several lines of inputs. Each line contains two non negative integers p and q (p ≤ q) separated by a single space. p and q will fit in 32 bit signed integer. In put is terminated by a line which contains two negative integers. This line should not be processed.
Output
For each set of input print a single line of the value of S(p, q).
Sample Input
1 10
10 20
30 40
-1 -1
Sample Output
46
48
52

问题链接UVA10994 Simple Addition
问题简述:(略)
问题分析
    数学计算问题,构造一个计算前缀和的函数实现。需要根据组合数学知识来推导公式。函数F(n)计算n的最右边非0数字。S(0,9)=45。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA10994 Simple Addition */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

LL sum(LL n)
{
    LL ans = 0, x;
    while(n) {
        x = n % 10;
        n /= 10;
        ans += ((1 + x) * x) / 2 + n * 45;
    }
    return ans;
}

int main()
{
    LL a, b;
    while(~scanf("%lld%lld", &a, &b) && a >= 0)
        printf("%lld\n", sum(b) - sum(a - 1));

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10469915.html