Description

Lucky Sum
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

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.

Input

The single line contains two integers l and r (1 ≤ l ≤ r ≤ 109) — the left and right interval limits.

Output

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

Problem - 124A -Codeforces

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4117182.html

Lucky Sum的更多相关文章

  1. Codeforces 121A Lucky Sum

    Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origi ...

  2. 『题解』Codeforces121A Lucky Sum

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Petya loves lucky numbers. Everybody k ...

  3. Codeforces Beta Round 84 (Div. 2 Only)

    layout: post title: Codeforces Beta Round 84 (Div. 2 Only) author: "luowentaoaa" catalog: ...

  4. ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)

    ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...

  5. hdu 5676 ztr loves lucky numbers

    题目链接:hdu 5676 一开始看题还以为和数位dp相关的,后来才发现是搜索题,我手算了下,所有的super lucky number(也就是只含数字4, 7且4, 7的数量相等的数)加起来也不过几 ...

  6. 枚举 + 进制转换 --- hdu 4937 Lucky Number

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  7. HDU 5213 Lucky 莫队+容斥

    Lucky Problem Description WLD is always very lucky.His secret is a lucky number K.k is a fixed odd n ...

  8. CodeForces 146A Lucky Ticket

    Lucky Ticket Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  9. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

随机推荐

  1. 生成HFile文件后倒入数据出现Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.filter.Filter

    数据导入的时候出现: at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclar ...

  2. PS切图保存后的背景图为透明

    1.若想PS切图保存后的背景图为透明,那么则需要在如下图中所示的修改即可,切图后[文件]——>[存储为web格式]——>[PNG-24]: 2.要想在css中的背景图片为相通,则先剪切一个 ...

  3. UIALertView与UIAlertViewDelegate的基本用法

    首先,视图控制器必须得实现协议UIAlertViewDelegate中的方法,并指定delegate为self,才能使弹出的Alert窗口响应点击事件. 具体代码如下: ViewController. ...

  4. bootstrap简章

    系统整理一遍bootstrap 的东西 1/  设置页面为H5文档类型 <!DOCTYPE html> <html lang="zh-CN"> ... &l ...

  5. 新浪IP归属地API

    之前用过腾讯的AIP,但是官方暂停这个服务了,新浪的API时间很久了,稳定性也很好,但愿能一劳永逸. ''' '''

  6. linux学习之八-Linux本机性能监控

    Linux 优化 1.看看磁盘有没有满(根目录有没有满).内存有没有满.CPU有没有满 查看磁盘有没有满,使用df -h  看看磁盘使用情况 查看内存使用  free -m 特别关注swap用了多少 ...

  7. Debug, Release区别

    Debug, Release区别 Debug附加了很多调试信息,主要用于调试,所以文件大 Release就是经过优化的版本,去除了调试信息,进行了代码优化,所以文件较小,同时速度要快于Debug De ...

  8. uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)

    Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...

  9. 【翻译自mos文章】改变数据库用户sysman(该用户是DB Control Repository 的schema)password的方法

    改变数据库用户sysman(该用户是DB Control Repository 的schema)password的方法 參考原文: How To Change the Password of the ...

  10. oracle实例名,数据库名,服务名等概念差别与联系

    数据库名.实例名.数据库域名.全局数据库名.服务名 这是几个令非常多刚開始学习的人easy混淆的概念.相信非常多刚開始学习的人都与我一样被标题上这些个概念搞得一头雾水.我们如今就来把它们弄个明确. 一 ...