更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

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 expression \(next(l) + next(l + 1) + \cdots + next(r - 1) + next(r)\). Help him solve this problem.

Input

The single line contains two integers \(l\) and \(r (1 \le l \le r \le 10^9)\) - 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 Input1

2 7

Sample Output1

33

Sample Input2

7 7

Sample Output2

7

Sample Explain

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\)

Description in Chinese

Petya喜欢Lucky Number。仅含有数字\(4\)和\(7\)的数字是一个Lucky Number

规定\(next(x)\)等于最小的大于等于\(x\)的Lucky Number。现在Petya想知道\(next(l) + next(l + 1) + \cdots + next(r - 1) + next(r)\)

Solution

我们可以先预处理出所有的Lucky Number

在询问时,我们可以采用前缀和的思想,题目中的询问可转换为:

\[\sum^{r}_{i = l} \mathrm{next}(i) \\\\ = \sum^{r}_{i = 1}{\mathrm{next}(i)}- \sum^{l - 1}_{i = 1}{\mathrm{next}(i)}
\]

当我们在询问\(\sum^{x}_{i = 1}{\mathrm{next}(i)}\)是,如果一个个暴力枚举一定会超时。所以我们可以把连续的一段(第一个大于等于\(x\)的值相同的数)一起加起来。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; typedef long long LL;
const int MAXN = 2005;
int l, r;
LL f[MAXN];
inline void prepare() {//预处理Lucky Number的值
f[1] = 4; f[2] = 7;
int cnt = 2;
for (int i = 1; i <= 512; i++) {
f[++cnt] = f[i] * 10 + 4;
f[++cnt] = f[i] * 10 + 7;
}
}
inline LL calc(int x) {//计算前n个next(i)的值
LL ret = 0;
for (int i = 1; i <= 2000; i++) {
if (f[i] >= x) {//如果超出了x(也就是累加询问的上界),就加完退出
ret += f[i] * (x - f[i - 1]);//询问上界的值与最大小于询问上界的值的差,也就是有多少个next(i)要一起累加
return ret;
} else ret += f[i] * (f[i] - f[i - 1]);//否则就一段一段加,增加的就是两个相邻的Lucky Number的差
}
return ret;
}
int main() {
scanf("%d%d", &l, &r);
prepare();
printf("%lld\n", calc(r) - calc(l - 1));
return 0;
}

『题解』Codeforces121A Lucky Sum的更多相关文章

  1. 『题解』洛谷P1063 能量项链

    原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...

  2. 『题解』Codeforces9D How many trees?

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In one very old text file there was wr ...

  3. 『题解』Codeforces446C DZY Loves Fibonacci Numbers

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...

  4. 『题解』洛谷P4016 负载平衡问题

    title: categories: tags: - mathjax: true --- Problem Portal Portal1:Luogu Portal2: LibreOJ Descripti ...

  5. 『题解』UVa11324 The Largest Clique

    原文地址 Problem Portal Portal1:UVa Portal2:Luogu Portal3:Vjudge Description Given a directed graph \(\t ...

  6. 『题解』Codeforces1142A The Beatles

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...

  7. 『题解』Codeforces1142B Lynyrd Skynyrd

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...

  8. 『题解』洛谷P1993 小K的农场

    更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...

  9. 『题解』洛谷P2296 寻找道路

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...

随机推荐

  1. 区间 GCD

    区间 GCD题目描述最近 JC 同学刚学会 gcd,于是迷上了与 gcd 有关的问题.今天他又出了一道这样的题目,想要考考你,你能顺利完成吗?给定一个长度为 n 的字符串 s[1..n],串仅包含小写 ...

  2. redhat5配置网络源

    最近适配了一堆linux系统, Redhat4/5/6, ubuntu 12/14/16, Suse 10/11/12 其中适配到Red5 时候配置网络源 # The mirror system us ...

  3. 程序员需要了解的硬核知识之CPU

    大家都是程序员,大家都是和计算机打交道的程序员,大家都是和计算机中软件硬件打交道的程序员,大家都是和CPU打交道的程序员,所以,不管你是玩儿硬件的还是做软件的,你的世界都少不了计算机最核心的 - CP ...

  4. OpenCV支持Qt用户界面

    在运行opencv程序的时候报下面的错误: ... The library is compiled without QT support in function ... 原因是在使用cmake安装op ...

  5. Java 8 Optional:优雅地避免 NPE

    本篇文章将详细介绍 Optional 类,以及如何用它消除代码中的 null 检查.在开始之前首先来看下什么是 NPE,以及在 Java 8 之前是如何处理 NPE 问题的. 空指针异常(NullPo ...

  6. .NET实时2D渲染入门·动态时钟

    .NET实时2D渲染入门·动态时钟 从小以来"坦克大战"."魂斗罗"等游戏总令我魂牵梦绕.这些游戏的基础就是2D实时渲染,以前没意识,直到后来找到了Direct ...

  7. Unity的学习笔记(射线检测)

    首先,射线检测的API是这样的,网上找了一下,这个图片看得很清楚: 接下来是自己使用这个进行测试 using System.Collections; using System.Collections. ...

  8. 我要学并发-Java内存模型到底是什么

    内存模型 在计算机CPU,内存,IO三者之间速度差异,为了提高系统性能,对这三者速度进行平衡. CPU 增加了缓存,以均衡与内存的速度差异: 操作系统增加了进程.线程,以分时复用 CPU,进而均衡 C ...

  9. 一个简洁漂亮的jQuery拖放排序插件DDSort

    拖放排序是WEB应用中常见的功能.虽然网上有很多别人已经造好的轮子,但是就我个人而言,没事就喜欢研究原理,自己造轮子,不管强大与否,简洁够用就是我的目标,再一个就是自己写的东西,应用起来得心应手,修改 ...

  10. JAVA实现扫描线算法

    首先说一下,教科书上的扫描线算法确实是用c++很好实现,而且网上有很多源码,而java实现的基本没有(可能是我没看到),所以肖先生还是打算自己码(实验作业写这个而自己又个是写java的猿0.0). 对 ...