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. 使用UTL_SMTP发送中文电子邮件

    就是在原有TOM源码的基础上修改utl_smtp.write_data中,将输出内容进行一下数据转换,这样可以保证中文输出不会出现乱码 ----------------------------- cr ...

  2. iOS开发之git学习

    本人是参考廖雪峰的git学习的.他写的非常详细,我在这里就是把我学习中的总结以及碰到的坑写出来. /* 初始化git仓库:git init */ /* 添加文件到git仓库 */ 分两步: 第一步:追 ...

  3. Ubuntu package managerment tools

    Visual demostration References Understanding differences between dpkg and apt-get/aptitude tools. A ...

  4. mysql优化(3) 集群配置

    两台服务器 192.168.187.131 192.168.187.132 1.主从配置 131为主 132为从 在131下 vim /etc/my.cnf [mysqld] datadir=/var ...

  5. 【转】commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar实现文件上传

    总共:一个upload.jsp,一个FileUploadServlet.java,两个文件:ImagesUploaded,ImagesUploadTemp, 一个web.xml,两个架包:common ...

  6. sublime前端编辑器入门与个人使用经验分享

    Sublime Text(以下简称sublime)是一款很好用的代码编辑器,小巧且很灵敏,几乎可以编写大部分主流的计算机语言代码,更是堪称前端代码编辑神器. 你百度一下会发现许多sublime的安装和 ...

  7. JQUERY选择和操作DOM元素(利用正则表达式的方法匹配字符串中的一部分)

    JQUERY选择和操作DOM元素(利用正则表达式的方法匹配字符串中的一部分) 1.匹配属性的开头 $("[attributeName^='value']"); 2.匹配属性的结尾 ...

  8. Python中字符串切片操作

    一:取字符串中第几个字符 print "Hello"[0] 表示输出字符串中第一个字符print "Hello"[-1] 表示输出字符串中最后一个字符   二: ...

  9. linux如何ARP嗅探 Linux下嗅探工具Dsniff安装记录

      先来下载依赖包 和一些必须要用到的工具 我这里用的是 dsniff-2.3 的版本 wget http://www.monkey.org/~dugsong/dsniff/dsniff-2.3.ta ...

  10. Struts2技术详解(转)

    1, 当Action设置了某个属性后,Struts将这些属性封装一个叫做Struts.valueStack的属性里.获取valueStack对象: ValueStack vs = (ValueStac ...