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 ...
随机推荐
- HDU1789(Doing Homework again)题解
HDU1789(Doing Homework again)题解 以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定任务分数和其截止日期,每日可完成一任务,输出当罚分尽可能小时 ...
- 一个很奇特的异常 tmpFile.renameTo(classFile) failed
刚换新电脑 win8+迈克菲 ,当tomcat把默认端口更改后(或者是启动了迈克菲的实时扫描),部署工程后,主页打不开了. 杀毒软件都会造成这个问题 百度杀毒全家桶 360全家桶 关掉就好了 .怀疑是 ...
- CSS 3 属性学习 —— 2. RGBA
RGBA 是 CSS3 中用来控制颜色的单位,分别是 Red Green Blue 三原色和 Alpha 透明度的缩写. 也就是说该属性可以帮助我们在设置颜色的同时,也设置了其透明度. 其实就是 RG ...
- 最长增长子序列 DP
#include<iostream> using namespace std; #define INF 0x7fffffff #define N 10000 // O(n^2) int l ...
- Balsamiq Mockups registration code
最近使用Mockups 进行页面原型设计,发现是未注册的,于是网上查询了下注册码,居然有效,在此记录下. 有需要的朋友也可以试试. Name:Rick Dong Key:eNrzzU/O ...
- Android 为应用添加数字角标
今天在论坛上看到了一个帖子,终于搞清了我很久以来的一个困惑,android到底能不能实现ios的角标效果,QQ是怎么实现的.看了这个帖子顿时终于解除了我的困惑. 先说一个下大概的思路: 大家都知道an ...
- 安装gstreamer
安装gstreamerglib2.44.0locate libffi.soexport LIBFFI_CFLAGS=-L/opt/vagrant/embedded/lib/./confiure./co ...
- 拓扑图弹力布局呈现Flickr图片搜索结果:智能创新
十年前有值得分享的图片我都存在Flickr上,可惜yahoo收购了Flickr之后堕落好多年,最近yahoo在梅姐带领下Flickr团队终于恢复了生机,个人免费存储空间扩充到了1T,界面用户体验也有 ...
- kubuntu/ubuntu下安装fcitx输入法
1.添加fcitx源(官方的源是旧版,不推荐使用) sudo gedit /etc/apt/sources.list 在sources.list文件中尾部添加: deb http://ppa.laun ...
- java中文乱码解决之道(三)—–编码详情:伟大的创想—Unicode编码
原文出处:http://cmsblogs.com/?p=1458 随着计算机的发展.普及,世界各国为了适应本国的语言和字符都会自己设计一套自己的编码风格,正是由于这种乱,导致存在很多种编码方式,以至于 ...