『题解』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\),现给定起点和终点 ...
随机推荐
- spring boot通过Spring Data Redis集成redis
在spring boot中,默认集成的redis是Spring Data Redis,Spring Data Redis针对redis提供了非常方便的操作模版RedisTemplate idea中新建 ...
- 编译安装msyql
环境: ubuntu18.04.2 mysql5.7.21 #创建mysql属组 groupadd mysql useradd -g mysql mysql #查看属组 tail /etc/passw ...
- Spring Cloud之Zuul
API网关是一个更为智能的应用服务器,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤.它除了要实现请求路由.负载均衡.校验过滤等功能之外,还需要更多能力, ...
- golang 服务平滑重启小结
背景 golang 程序平滑重启框架 supervisor 出现 defunct 原因 使用 master/worker 模式 背景 在业务快速增长中,前期只是验证模式是否可行,初期忽略程序发布重启带 ...
- Vue的介绍及安装和导入
08.27自我总结 Vue的介绍及安装和导入 本质就是封装一些js 一Vue的介绍 进式 JavaScript 框架 通过对框架的了解与运用程度,来决定其在整个项目中的应用范围,最终可以独立以框架方式 ...
- django渲染高阶
08.16自我总结 django渲染高阶 一.利用母版渲染 1.创建母版文件 如:stamper.html <!DOCTYPE html> <html lang="en&q ...
- 公共DNS性能大比拼
今天中午,访问Gitee突然访问不进去,然后收到红薯通知:阿里云停止了 Gitee.com 的域名解析. 码云官方也随后给出解决办法 没有任何提示,没有任何提前通知,阿里云停止了 Gite ...
- Jenkins介绍
持续集成: 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成.每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证 ...
- 二、docker 镜像容器常用操作(让我们用docker 溜得飞起)
前言 上篇讲了我们如何安装docker,现在该我们一展拳脚的时候了.接下来让我们一起学习一下docker常见的操作,让我们能够会使用 docker. 基本概念 在讲使用之前,还是先将一下docker ...
- 关于seaJs合并压缩(gulp-seajs-combine )路径与文件ID匹配问题。
前段时间和有大家介绍过用 gulp-seajs-combine 来打包seaJs文件.大家会发现合并seaJs一个很奇怪的现象,那就是它的 ID和路径匹配原则.使得有些文件已经合并过去了,但还是会提示 ...