Lucky Sum
Description
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Let next(x) be the minimum lucky number which is larger than or equals x. Petya is interested what is the value of the expressionnext(l) + next(l + 1) + ... + next(r - 1) + next(r). Help him solve this problem.
The single line contains two integers l and r (1 ≤ l ≤ r ≤ 109) — the left and right interval limits.
In the single line print the only number — the sum next(l) + next(l + 1) + ... + next(r - 1) + next(r).
Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the%I64d specificator.
Sample test(s)
| input |
| 2 7 |
| output |
| 33 |
| input |
| 7 7 |
| output |
| 7 |
Note
In the first sample: next(2) + next(3) + next(4) + next(5) + next(6) + next(7) = 4 + 4 + 4 + 7 + 7 + 7 = 33
In the second sample: next(7) = 7
题解:题意很好理解,重点难点是把lucky numbers存到一个数组里(dfs),再根据题意求和。
其中sum(x) = next(1) + next(2) + next(3) + ... + next(x), 所以next(l) + next(l+1) + ... + next(r) = sum(r) - sum(l-1)。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
typedef long long LL; const int maxLength = ;
LL luck[maxLength];
int index = ; void dfs(LL x, int cursor) {
if(cursor > ) {
return;
}
luck[index++] = x;
dfs(x*+, cursor+);
dfs(x*+, cursor+);
}
//sum(x) = next(1)+next(2)+...next(x)
LL sum(LL x) {
LL sum = ;
if(x == ) {
return ;
}
for(int i=; i<index; i++) {
if(x >= luck[i]) {
sum += luck[i]*(luck[i] - luck[i-]);
}else {
sum += luck[i]*(x-luck[i-]);
break;
}
}
return sum;
}
int main()
{
LL l, r;
dfs(, );
dfs(, );
sort(luck, luck+index);
cin >> l >> r;
cout << sum(r)-sum(l-) << endl;
return ;
}
转载请注明出处:http://www.cnblogs.com/michaelwong/p/4117182.html
Lucky Sum的更多相关文章
- Codeforces 121A Lucky Sum
Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...
- 『题解』Codeforces121A Lucky Sum
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Petya loves lucky numbers. Everybody k ...
- Codeforces Beta Round 84 (Div. 2 Only)
layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...
- ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)
ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...
- hdu 5676 ztr loves lucky numbers
题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- HDU 5213 Lucky 莫队+容斥
Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...
- CodeForces 146A Lucky Ticket
Lucky Ticket Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submi ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
随机推荐
- 不用css样式表和背景图片实现圆角矩形,超简洁!
当网站页面的整体布局设计好后,接下来有很多细节的实现是很让人头疼的.其中之一就是圆角矩形的实现. 在网上看了很多圆角矩形的实现方法,基本有两种,一种是用纯css实现,不需要背景图片:另一种是用背景图像 ...
- UILabel头文件常见属性
text : default is nil 文本属性,默认值是 nil @property(nullable, nonatomic,copy) NSString *text; font : defau ...
- js实现超过长度的字符截取指定长度(中文字符算2个字符),超出部分以...显示
//超过长度的字符截取指定长度,超出部分以...显示 function subString(str, len) { var newLength = 0; var newStr = "&quo ...
- DW 图片不显示的情况 ———网页只显示字不显示图片的情况 目录下的图片名被改动不显示图片的情况
例子1-- 酒瓶子 alt 在不现实图片的情况下显示汉字 图文效果展示 alt 1************************* 语句---- <body> < ...
- MapReduce详解
1.mapreduce之shuffle http://blog.csdn.net/thomas0yang/article/details/8562910 2.彻底了解mapreduce核心Shuffl ...
- C语言中的数据类型
基本数据类型: int float double char void 派生数据类型: 数据类型修饰符 + 基本数据类型 = 派生数据类型 signed 和 unsigned 类型 unsigned ...
- JavaScript 对象扩展代码
JavaScript 扩展代码 更具需要写的几个扩展. 扩展核心自执行函数 Object.extend /** * 对象扩展体 参数是 {属性|方法:属性值|方法体} * 只执行实现 * * 实例对基 ...
- dot.js教程文档api
dot.js是一个短小精悍的js模板引擎,压缩版仅有4K大小,最近使用dot的时候整理出这个dot.js教程文档,其实称不上什么教程,只是对dot.js的介绍和实例,希望能帮助到一部分需要的人. 使用 ...
- Flink Program Guide (4) -- 时间戳和Watermark生成(DataStream API编程指导 -- For Java)
时间戳和Watermark生成 本文翻译自Generating Timestamp / Watermarks --------------------------------------------- ...
- 【Chromium中文文档】OS X 沙箱设计
OS X 沙箱设计 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_Architecture/OSX ...