『题解』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\),现给定起点和终点 ...
随机推荐
- redis相关缓存知识
Redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- 浅谈分布式事务与TX-LCN
最近做项目使用到了分布式事务,下面这篇文章将给大家介绍一下对分布式事务的一些见解,并讲解分布式事务处理框架TX-LCN的执行原理,初学入门,错误之处望各位不吝指正. 什么情况下需要使用分布式事务? 使 ...
- Python之string模块(详细讲述string常见的所有方法)
相信不少学习python的程序员都接触过string模块 string模块主要包含关于字符串的处理函数 多说无益,初学python的小伙伴还不赶紧码起来 接下来将会讲到字符串的大小写.判断函数. 以及 ...
- 解决Maven依赖jar包冲突总结
maven导入jar包中的一些概念: 直接依赖:项目中直接导入的jar包,就是该项目的直接依赖包. 传递依赖:项目中没有直接导入的jar包,可以通过项目直接依赖jar包传递到项目中 ...
- 2.RF中scalar,list和dict变量的定义和取值
$:定义scalar变量:@定义list变量:&定义dict变量: $还用来取值,包含scalar, list和dict变量,如下example所示 1.定义scalar变量:set vari ...
- 使用java语言实现一个动态数组(详解)(数据结构)
废话不多说,上代码 1.从类名开始(我真是太贴心了) public class Array<E> 首先数组类需要带有泛型,这个不多说.需要注意的是在java中,数组只能存放同一个类型的. ...
- Windows API 编程入门
Windows 工作原理的中心思想就是“动态链接”概念.Windows 自身带有一大套函数,应用程序就是通过调用这些函数 来实现它的用户界面和在屏幕上显示文本和图形的.这些函数都是在动态链接库里实现的 ...
- [Luogu3787] 冰精冻西瓜
题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...
- asp.net core 设置默认文档index.html
参考:https://jingyan.baidu.com/article/6079ad0e3e212168fe86db75.html 在Startup.cs的Configure添加 app.UseFi ...
- CocosCreator中_worldMatrix到底是什么(下)
Cocos Creator 中 _worldMatrix 到底是什么(下) 1. 摘要 上篇介绍了矩阵的基本知识以及对应图形变换矩阵推倒.中篇具体介介绍了对应矩阵转换成cocos creator代码的 ...