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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值