题目链接

题目

题目描述

定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。

比如说,47、744、4都是幸运数字而5、17、467都不是。

定义next(x)为大于等于x的第一个幸运数字。给定l,r,请求出next(l) + next(l + 1) + ... + next(r - 1) + next(r)。

输入描述

两个整数l和r (1 <= l <= r <= 1000,000,000)。

输出描述

一个数字表示答案。

示例1

输入

2 7

输出

33

示例2

输入

7 7

输出

7

题解

知识点:BFS,枚举。

显然对每个数进行枚举是不可行的。而因为一大块数对应一个幸运数字,所以考虑枚举幸运数字,再遍历快速遍历目标区间。

考虑用bfs打表,因为bfs生成的数字天然排好序了。

时间复杂度 \(O(r-l)\)

空间复杂度 \(O(?)\)

代码

#include <bits/stdc++.h>
#define ll long long using namespace std; vector<ll> a; void bfs() {
queue<ll> q;
q.push(0);
while (!q.empty()) {
ll x = q.front();
q.pop();
if (x / (1e9) >= 1)continue;
a.push_back(x * 10 + 4);
a.push_back(x * 10 + 7);
q.push(x * 10 + 4);
q.push(x * 10 + 7);
}
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
bfs();
int l, r;
cin >> l >> r;
ll ans = 0;
int i = 0, pos = l;
while (a[i] < pos) i++;
while (a[i] <= r) {
ans += (a[i] - pos + 1) * a[i];
pos = a[i] + 1;
i++;
}
ans += (r - pos + 1) * a[i];
cout << ans << '\n';
return 0;
}

NC15291 幸运数字Ⅱ的更多相关文章

  1. BZOJ 1853 【Scoi2010】 幸运数字

    Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认 为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,8 ...

  2. BZOJ 4568 幸运数字

    题目传送门 4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MB Description A 国共有 n 座城市,这些城市由 n-1 ...

  3. BZOJ 1853: [Scoi2010]幸运数字

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 2117  Solved: 779[Submit][Status] ...

  4. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并

    4568: [Scoi2016]幸运数字 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 238  Solved: 113[Submit][Status ...

  5. 【BZOJ-1853&2393】幸运数字&Cirno的完美算数教室 容斥原理 + 爆搜 + 剪枝

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1817  Solved: 665[Submit][Status] ...

  6. BZOJ1853 [Scoi2010]幸运数字

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  7. 【bzoj1853】 Scoi2010—幸运数字

    http://www.lydsy.com/JudgeOnline/problem.php?id=1853 (题目链接) 今天考试考了容斥,结果空知道结论却不会写→_→ 题意 求区间中不含6,8两个数字 ...

  8. 幸运数字(number)

    幸运数字(number) Time Limit:1000ms   Memory Limit:64MB [题目描述] LYK最近运气很差,例如在NOIP初赛中仅仅考了90分,刚刚卡进复赛,于是它决定使用 ...

  9. Bzoj 1853: [Scoi2010]幸运数字 容斥原理,深搜

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1774  Solved: 644[Submit][Status] ...

  10. bzoj 1853: [Scoi2010]幸运数字 容斥

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1170  Solved: 406[Submit][Status] ...

随机推荐

  1. spring--AOP通知类型有哪些

    Spring AOP(Aspect-Oriented Programming,面向切面编程)提供了五种类型的通知(advice),这些通知定义了切面(aspect)是在目标对象的方法执行的哪个点被应用 ...

  2. 使用命令行方式搭建uni-app + Vue3 + Typescript + Pinia + Vite + Tailwind CSS + uv-ui开发脚手架

    .markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

  3. SV概述

    System Verilog概述 路科验证视频,B站可看(补充一下知识) 学习SV之前,最好有Verilog基础 SV诞生 SV发展历史 Verilog - 偏向于设计 System Verilog ...

  4. 【C/C++】 变参函数

    #include <stdio.h> #include <stdbool.h> #include <stdarg.h> #define MLA_ASSERT(exp ...

  5. 【Mysql系列】(二)日志系统:一条更新语句是如何执行的

    有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步,认准https://blog.zysicyj.top 这篇文章是从Github ReadMe拷贝的,内容实践下载是没问题的,能够正 ...

  6. [转帖]【Redis】Redis中使用Lua脚本

    Lua是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. Lua具体语法参考:https://www.runoob. ...

  7. [转帖]深入理解Redis的scan命令

    熟悉Redis的人都知道,它是单线程的.因此在使用一些时间复杂度为O(N)的命令时要非常谨慎.可能一不小心就会阻塞进程,导致Redis出现卡顿. 有时,我们需要针对符合条件的一部分命令进行操作,比如删 ...

  8. Springboot tomcat.threads线程数学习

    Springboot tomcat.threads线程数学习 摘要 压测完nginx 突然想搞一下springboot内嵌的tomcat的 threads的参数 一不做二不休, 直接就着脚本进行学习和 ...

  9. OpenEuler2203使用rpm方式安装Oracle19c的过程

    OpenEuler2203使用rpm方式安装Oracle19c的过程 安装介质 oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm oracle-d ...

  10. js中toFixed 并不是你想的那样进行四舍五入

    toFixed 的简单介绍 toFixed() 方法可把 Number 类型的数字通过四舍五入为指定小数位的字符串.(将数字类型转化为字符串类型) 也就是说toFixed只能够处理数字类型的. 字符串 ...