码迷,mamicode.com
首页 > 其他好文 > 详细

UVA10994 Simple Addition【前缀和】

时间:2019-03-04 12:56:30      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:rate   scan   tput   type   out   sig   参考   not   long   

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;
}

UVA10994 Simple Addition【前缀和】

标签:rate   scan   tput   type   out   sig   参考   not   long   

原文地址:https://www.cnblogs.com/tigerisland45/p/10469915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!