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的表达式数目. 题 ...
随机推荐
- What I Have Lived For(我为什么而活着-罗素)
What I Have Lived For by Bertrand Russell Three passions, simple but overwhelmingly strong, have gov ...
- 【Android】手机号码获取问题
手机号码不是所有的都能获取.只是有一部分可以拿到.这个是由于移动运营商没有把手机号码的数据写入到sim卡中.SIM卡只有唯一的编号,供网络与设备识别那就是IMSI号码,手机的信号也可以说是通过这个号码 ...
- django分页linaro-django-pagination
1.安装linaro-django-pagination settings INSTALLED_APPS = ( # ... 'linaro_django_pagination', ) MIDDLEW ...
- RatProxy
http://book.51cto.com/art/201212/374023.htm http://www.oschina.net/p/ratproxy/similar_projects
- VC编程之设置客户区背景图片
在很多系统中出于美观的需要常常要设置背景图片.下面我介绍一种在客户区设置背景图片的简单方法. 1 .将背景bmp 图片导入到工程,资源ID 这里假设为 IDB_BITMAP1 2 .在视图类添加如下代 ...
- JAVA GUI学习 - JTabbedPane选项卡组件学习
public class JTabbedPaneKnow extends JFrame { JTabbedPane jTabbedPane; JPanel jPanelRed; JPanel jPan ...
- MySQL几种方法的数据库备份
MySQL有几个方法来备份 最近博客一直想写点.可是不知道写什么,感觉自己近期的知识没有什么添加,今天想到了一篇能够写的博客.曾经试过依据data目录备份MySQL.可是从来没有成功过.前几天帮助朋友 ...
- 引用类型List<T>的比较
一:重新Equals和GetHashCode方法 /// <summary> /// 描 述:弹出模型对象列表比较器(根据ID比较) /// </summary&g ...
- IOC容器初始化——BeanDefinition的Resource定位
以编程的方式使用DefaultListableBeanFactory时,首先定义一个Resource来定位容器使用的BeanDefinition.这是使用的是ClassPathResource,意味着 ...
- LNMP一键安装包sh脚本
Xshell 5 (Build 0719) Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `he ...