题干:

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

Input

The single line contains two integers l and r (1 ≤ l ≤ r ≤ 109) — 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.

Examples

Input

2 7

Output

33

Input

7 7

Output

7

Note

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

解题报告:

这题不难发现是状态的转移,每一个状态可以延伸到新的两个状态,所以考虑bfs搜索一下,然后二分查找位置就可以暴力了。这题的一个坑点在于,能否发现,当st和ed是相等的时候就不能分区间这么看了,而应该直接看这个区间。找个例子看啊:这种样例很好找啊,比如1e9 1e9这个样例。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll b[100005];
int tot;
void bfs() {priority_queue<ll,vector<ll>,greater<ll> > pq;tot = 0;b[++tot] = 4;pq.push(4);b[++tot] = 7;pq.push(7);while(!pq.empty()) {ll cur = pq.top();pq.pop();if(cur > (ll)1e12+7) return ;b[++tot] = cur*10+4;pq.push(b[tot]);b[++tot] = cur*10+7;pq.push(b[tot]);}
}
int main()
{bfs();ll l,r;cin>>l>>r;
//  cout << b[tot] << endl;ll ans = 0;int st = lower_bound(b+1,b+tot+1,l) - b;int ed = lower_bound(b+1,b+tot+1,r) - b;if(ed > st) {ans += (b[st] - l + 1) * b[st];for(int i = st+1; i<ed; i++) {ans += (b[i] - b[i-1]) * b[i];}ans += (r-b[ed-1]) * b[ed];}else ans += (r-l+1) * b[st];cout << ans << endl;return 0 ;
}

法2:分块:(还未看)

#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\cin.tie(0);\cout.tie(0);using namespace std;
typedef long long ll;
const int maxn = 1e5+10;ll last(ll x) {int len = int(log10(x)) + 1;    //数字位数ll ans = 0,cnt = 0;for(int i=0; i<len; i++)        //同等位数最大最小幸运数ans = ans*10+4,cnt = cnt*10+7;if(x>cnt)                       //位数+1return ans*10+4;while(ans<x) {ll res = cnt;for(int i=0; i<1<<len; i++) {ll tmp = 0;for(int j=0; j<len; j++) {if(i&(1<<j))tmp = tmp*10+7;elsetmp = tmp*10+4;}if(tmp>=x)res = min(res,tmp);}ans = res;}return ans;
}int main()
{ll l,r;cin>>l>>r;ll now = l,ans = 0;while(true) {ll la = last(now);if(la>r) {ans+= la * (r-now+1);break;} elseans += la * (la-now+1);now = la+1;}cout<<ans<<endl;return 0;
}

*【CodeForces - 122C 】Lucky Sum (bfs记录状态,二分查找,有坑)(或分块)相关推荐

  1. LeetCode 1102. 得分最高的路径(优先队列BFS/极大极小化 二分查找)

    文章目录 1. 题目 2. 解题 2.1 优先队列BFS 2.2 极大极小化 二分查找 1. 题目 给你一个 R 行 C 列的整数矩阵 A.矩阵上的路径从 [0,0] 开始,在 [R-1,C-1] 结 ...

  2. leetcode算法算题记录-数组--二分查找

    public class 二分查找 {//注: 数组为有序数组且数组中无重复元素是使用二分法的前提//704.二分查找 简单//给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 ta ...

  3. 【Uva - 10047 】The Monocycle(搜索,bfs记录状态)

    题干: Uva的题目就不粘贴题干了,,直接上题意吧. 有你一个独轮车,车轮有5种颜色,为了5中颜色的相对位置是确定的.有两种操作:1.滚动:轮子必须沿着顺时针方向滚动,每滚动一次会到达另一个格子,着地 ...

  4. 【ACM】Uva 1152 (4 Values whose Sum is 0) 二分查找lower_bound() 和upper_bound()的使用

    [问题描述] The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, ...

  5. LintCode 1816. 使结果不超过阈值的最小除数(二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 描述 给你一个整数数组 nums 和一个正整数 threshold ,你需要选择一个正整数作为除数,然后将数组里每个数都除以它,并对除法结果求和. 请你找 ...

  6. LeetCode 410. 分割数组的最大值(极小极大化 二分查找 / DP)

    文章目录 1. 题目 2. 解题 2.1 二分查找 2.2 DP 1. 题目 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组. 设计一个算法使得这 m 个子数组各自和 ...

  7. 用C++实现二分查找

    用C++实现二分查找 对于有序表而言,通常使用二分查找来寻找待查记录.二分查找,又名折半查找,具体查找过程为:先确定待查找记录的范围,然后逐步缩小范围知道找到或者找不到该记录为至.其C++实现代码如下 ...

  8. leetcode1011. 在 D 天内送达包裹的能力(二分查找)

    传送带上的包裹必须在 D 天内从一个港口运送到另一个港口. 传送带上的第 i 个包裹的重量为 weights[i].每一天,我们都会按给出重量的顺序往传送带上装载包裹.我们装载的重量不会超过船的最大运 ...

  9. LeetCode 1631. 最小体力消耗路径(DFS + 二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 你准备参加一场远足活动.给你一个二维 rows x columns 的地图 heights ,其中 heights[row][col] 表示格子 (row ...

最新文章

  1. 重磅直播|大规模点云可视化技术
  2. python填写excel-Python读写Excel表格(简单实用)
  3. python绘制球体_趣学Python之弹球游戏第一阶段--画个红球
  4. C#使用Xamarin开发可移植移动应用进阶篇(6.使用渲染器针对单个平台自定义控件),附源码
  5. Nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分...
  6. PicGo+码云Gitee建立国内高速图床
  7. 编程输出2的90次方的精确值
  8. linux socket的select函数例子
  9. python中大括号是什么_Python中模块(Module)和包(Package)到底是什么,有什么区别?...
  10. python快递费用计算_Python制作快递查询系统,来感受到了Python的强大!
  11. python 笔记 之 练习答案-ABCD乘以9=DCBA
  12. CentOS安装系统时硬盘分区建议
  13. CSS之固定定位,绝对定位+负边距,双飞翼布局,属性选择器,伪类选择器补,状态伪类选择器,相邻全部兄弟选择器,取非选择器,em与rem,变形效果...
  14. webmin的vsftp插件
  15. meshLab裁剪网格
  16. jsp里table边框线_JSP表格边框颜色
  17. 面试题19/leetcode10:正则表达式匹配 C++
  18. Boost出现error C2678
  19. 优秀的免费高清图片素材网站推荐
  20. 用计算机弹精灵宝可梦音乐,《精灵宝可梦》图鉴402:可以演奏出优美音乐的精灵——音箱蟀...

热门文章

  1. keeplive linux平台下,Linux下搭建keepalive+nginx
  2. Codeforces Round #719 (A-C)
  3. python命令行调试django代码_Django shell调试models输出的SQL语句方法
  4. 枚举命名规范_UE4 C++基础教程 - 编码规范
  5. activemq jdbc mysql_activeMQ JDBC Master Slave
  6. 冠榕智能灯光控制协议分析(node-controller)
  7. Asterisk NAT
  8. java 二进制as_Java中的二进制文本
  9. js如何获取计算机当前时间,js获取当前系统时间
  10. 教师资格证计算机考察知识点,教师资格证考试信息技术常考知识点同步练习题.docx...