『题解』Codeforces121A Lucky Sum
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^{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的更多相关文章
- 『题解』洛谷P1063 能量项链
原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...
- 『题解』Codeforces9D How many trees?
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In one very old text file there was wr ...
- 『题解』Codeforces446C DZY Loves Fibonacci Numbers
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...
- 『题解』洛谷P4016 负载平衡问题
title: categories: tags: - mathjax: true --- Problem Portal Portal1:Luogu Portal2: LibreOJ Descripti ...
- 『题解』UVa11324 The Largest Clique
原文地址 Problem Portal Portal1:UVa Portal2:Luogu Portal3:Vjudge Description Given a directed graph \(\t ...
- 『题解』Codeforces1142A The Beatles
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...
- 『题解』Codeforces1142B Lynyrd Skynyrd
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...
- 『题解』洛谷P1993 小K的农场
更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...
- 『题解』洛谷P2296 寻找道路
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...
随机推荐
- python编程基础之十六
for in 循环,与其说是循环不如说精确点交遍历 for 变量名 in + 迭代对象 语句A else: 语句B 作用:一次访问迭代对象中的元素并赋值给变量 循环终止时,执行else语句块,如果br ...
- WampServer配置可局域网访问站点
一 WampServer3.1.7版本 二 需求:项目开发阶段,服务器还未购买,前端需要调用接口进行测试,于是想到了在本地搭设一个站点,可以局域网内访问 三 先为这个项目单独在本地设置一个端口,比如我 ...
- UVa12105 越大越好
题文:https://vjudge.net/problem/12364(或者见紫书) 题解: 因为题目中有两个限制条件,那么我们就顺着题目的意思来dp,设dp[i][j]表示目前还剩下的i个火柴,用这 ...
- Have a Good Attitude 良好的态度
Poor attitudes lead to poor communication. Poor communication leads to poor service. Poor service le ...
- Jenkins构建Jmeter项目之源代码管理(SVN)
1.查看项目创建中是否又svn插件,没有的话下载插件subversion 2.配置svn源代码管理,如下图(testcases目录下包含build.xml和脚本文件) 3.查看Jenkins本地工作空 ...
- Jmeter结构体系及运行顺序
一:jmeter运行原理: jmeter时以线程的方式来运行的(由于jmeter是java开发的所以是运行在JVM虚拟机上的,java也是支持多线程的) 二:jmeter结构体系 1.取样器smapl ...
- 实验吧之【你真的会PHP吗?】
你真的会PHP吗? 首先刚进网页就是一个have fun 看了源码没有什么提示,也没有输入框,那就打开F12看看 有提示 6c525af4059b4fe7d8c33a.txt 访问 http://c ...
- luoguP4779 【模板】单源最短路径
题目描述 单源最短路径模板. 使用 SPFA 肯定是不行的啦,网格图hack. 所以我们使用 Dijkstra 算法. 这里有一篇写的很好的 blog,无必要赘述.最后贴上代码. #include&l ...
- php反序列化漏洞复现
超适合小白的php反序列化漏洞复现 写在前头的话 在OWASP TOP10中,反序列化已经榜上有名,但是究竟什么是反序列化,我觉得应该进下心来好好思考下.我觉得学习的时候,所有的问题都应该问3个问题: ...
- pycharm(社区版2019.1版本)打开README.md文件卡死解决办法
现象:pycharm(社区版2019.1版本)打开README.md文件卡死 解决办法: 将插件Markdown support前的勾选√去掉,保存修改后重启pycharm即可