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 ...
随机推荐
- SSAS中CUBE行权限数据级权限控制
去年做了一个数据仓库的项目,其中涉及到了CUBE数据级权限的控制.在网上找这方面的资料,找到一个[BI] 通用数据级权限控制解决方案的实现(二):Cube中的角色设置与数据级权限控制.根据这个大牛的思 ...
- C# 5 break continue 球员成绩 彩票 选班长
二.新课: 1.break与continue. 这两个关键字一般放在循环的花括号里面使用. break--结束整个循环. continue--结束本次循环,进入下次循环. break的案例: ...
- [Linked List]Reverse Nodes in k-Group
Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard Given a linked list, reverse the no ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- c++11: <thread>学习
<thread>头文件中包含thread类与this_thread命名空间,下面逐一介绍. thread类 1. 构造函数 (1)默认构造函数 thread() noexcept; 默认构 ...
- spring sts 从数据库中反向生成实体类
首先我们要在sts中建立mysql的数据库连接 1. 当点击ok之后,如果没有报错的话就应该是建立好了,我们可以点击查看这个数据库中所有的表 我们就可以再sts进行数据库操作了,具体如下: 点击如下按 ...
- 带参数的URLconf
我们在Django建立helloworld自定义页面创建的页面,只能算是一个静态页,发起一个请求,返回一个固定的值,并不能满足我们动态的需求.今天我们创建一个带参数的URLconf,根据参数展示不同的 ...
- python基础教程第3章——字符串
1.字符串格式化 字符串格式化操作符%+转换标志+最小字段宽度+点后跟精度值+转换类型 String模块提供另外一种格式化方式 from string import Template s=Templa ...
- 点击超链接执行js代码实现确认操作
如题,本次是要实现点击超链接实现执行js代码,并确认是否删除数据库数据,采用php. 首先链接数据库,查询数据库数据: 1: <?php 2: $dbms='mysql'; //数据库类型 ,对 ...
- Oracle EBS-SQL (WIP-3):检查非标任务子件没选MRP净值.sql
SELECT WE.WIP_ENTITY_NAME, MSI.SEGMENT1, MSI.DESCRIPTION, WDJ.CLASS_CO ...