10994 - Simple Addition(规律)
Problem E
Simple Addition
Input: Standard Input
Output: Standard Output
Let’s define a simple recursive function F (n), where

Let’s 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.
For each set of input print a single line of the value of S(p, q).
Sample Input Output for Sample Input
|
1 10 10 20 30 40 -1 -1 |
46 48 52 |
题意:求题目中那个公式从p到q F【i] 的和
思路:每个数肯定是1-9.这样的规律,个位1-9,十位1-9,百位1-9.如此一来,只要把各位加完,加十位,加百位,一个个加完,最后就是答案了。加的时候可以直接用等差数列和公式
代码:
#include <stdio.h>
int p, q;
long long Sum(long long s, long long e) {
if (s > e)
return 0;
return (s + e) * (e - s + 1) / 2;
}
long long cal(long long n) {
if (n <= 0) return 0;
long long ans = 0;
while (n) {
ans += n / 10 * 45;
ans += Sum(1, n % 10);
n /= 10;
}
return ans;
}
int main() {
while (~scanf("%d%d", &p, &q) && p != -1 && q != -1) {
printf("%lld\n", cal(q) - cal(p - 1));
}
return 0;
}
10994 - Simple Addition(规律)的更多相关文章
- uva 10994 - Simple Addition(规律)
题目链接:uva 10994 - Simple Addition 题目大意:给出l和r,求∑(l≤i≤r)F(i), F(i)函数题目中有. 解题思路:由两边向中间缩进,然后l和r之间的数可以按照1~ ...
- uva 10994 - Simple Addition
//组合数学 //计算sum{i从右往左数的第一个非0数字,p<=i<=q}. #include <cstdio> typedef long long ll; ll sum(l ...
- 组合数学第一发 hdu 2451 Simple Addition Expression
hdu 2451 Simple Addition Expression Problem Description A luxury yacht with 100 passengers on board ...
- HDU 2451 Simple Addition Expression(组合数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2451 Problem Description A luxury yacht with 100 pass ...
- HDU2451:Simple Addition Expression
Problem Description A luxury yacht with 100 passengers on board is sailing on the sea in the twiligh ...
- 【计数】Simple Addition Expression
[来源] 2008年哈尔滨区域赛 [题目链接]: http://acm.hdu.edu.cn/showproblem.php?pid=2451 [参考博客]: HDU 2451 Simple Addi ...
- HDU 2451 Simple Addition Expression(找规律,考验智商)
题目 最近比赛的题目好多签到题都是找规律的考验智商的题目啊,,,我怎么越来越笨了,,,, 通过列举,可以发现规律: 从左往右按位扫这个数: 当数的长度大于1时: 当首位大于3时,答案就是4*4*4*… ...
- 【HDOJ】2451 Simple Addition Expression
递推,但是要注意细节.题目的意思,就是求s(x) = i+(i+1)+(i+2),i<n.该表达中计算过程中CA恒为0(包括中间值)的情况.根据所求可推得.1-10: 31-100: 3*41- ...
- HDU 2451 Simple Addition Expression
题目大意:有一个关于 简单加法表达式 的定义告诉你,就是 选一个数字i 如果 i+(i+1)+(i+2) 它的和,没有任何一位进位的话,那就是 一个i的简单加法表达式,求小于n的表达式数目. 题 ...
随机推荐
- jquery获取多个checkbox的值异步提交给php
html代码: <tr> <td><input type="checkbox" name="uid" value="&l ...
- yii教程
http://www.yiichina.com/doc 官网是很好的参考文档
- ios如何实现推送通知
推送通知的步骤:1.询问是否允许推送通知.2.如果用户允许在APPDELEGATE 中实现 - (void)application:(UIApplication *)application didRe ...
- Sql:查看数据库表和表结构的语句
T-sql 显示表结构和字段信息的sql语句: exec sp_help tablename; ~~使用存储过程 sp_help 显示数据库包含哪些表的sql语句: use yourDBname;se ...
- Spring Boot使用Druid和监控配置
Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource 整体步骤: (1) -- Druid简单介绍,具体看官网: (2) ...
- html文件中文在浏览器中显示乱码问题解决
利用浏览器打开html文件时,中文显示乱码,如下是原文件的内容 1 <html> 2 <head> 3 <title> ...
- BZOJ 3038 上帝造题的七分钟2 (并查集+树状数组)
题解:同 BZOJ 3211 花神游历各国,需要注意的是需要开long long,还有左右节点需要注意一下. #include <cstdio> #include <cmath> ...
- 概率图模型(PGM)学习笔记(四)-贝叶斯网络-伯努利贝叶斯-多项式贝叶斯
之前忘记强调了一个重要差别:条件概率链式法则和贝叶斯网络链式法则的差别 条件概率链式法则 贝叶斯网络链式法则,如图1 图1 乍一看非常easy认为贝叶斯网络链式法则不就是大家曾经学的链式法则么,事实上 ...
- 各浏览器对 window.open() 的窗口特征 sFeatures 参数支持程度存在差异
标准参考 无. 问题描述 使用 window.open 方法可以弹出一个新窗口,其中 open 方法的 sFeatures 参数选项在各浏览器中支持程度不一,这有可能导致同样的代码使各浏览器中弹出窗口 ...
- js动画学习(四)
七.多属性封装函数 前面分别介绍了单独改变单一属性值得动画,从本节起开始介绍多属性封装函数,一个函数搞定多种属性值的改变. 首先介绍一个很重要的函数getStyle(),这个函数返回一个元素的当前属性 ...