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

题干:

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441560.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

模拟量使用计算机电缆,DJYPVRP计算机电缆型号规格

计算机电缆用途&#xff1a;本产品适用于额定电压30/500v及以下防干扰性能要求较高的电子计算机、检测仪器、仪表的连接。使用条件&#xff1a; 计算机电缆(电压等级&#xff1a;0.45/0.75KV&#xff0c;企标)本产品使用于交流额定电压为300/500及以下&#xff0c;对于防干扰性…

【CodeForces - 155C】Hometask (字符串,思维,贪心,熟悉句式)(总结)

题干&#xff1a; Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercas…

html 报表插件,轻量级图形报表插件JSCharts

插件描述&#xff1a;JScharts是一个用于在浏览器直接绘制图表的JavaScript工具包。JScharts支持柱状图、圆饼图以及线性图&#xff0c;可以直接将这个图插入网页&#xff0c;JScharts图的数据可以来源于XML文件、JSON文件或JavaScript数组变量。2017-05-09更新&#xff1a;改为…

算法讲解 -- 二分图之 匈牙利算法

匈牙利算法是由匈牙利数学家Edmonds于1965年提出&#xff0c;因而得名。匈牙利算法是基于Hall定理中充分性证明的思想&#xff0c;它是部图匹配最常见的算法&#xff0c;该算法的核心就是寻找增广路径&#xff0c;它是一种用增广路径求二分图最大匹配的算法。 -------等等&…

html 表格文字颜色 css,CSS 表格-JavaScript中文网-JavaScript教程资源分享门户

使用 CSS 可以使 HTML 表格更美观。CompanyContactCountryAlfreds FutterkisteMaria AndersGermanyBerglunds snabbkpChristina BerglundSwedenCentro comercial MoctezumaFrancisco ChangMexicoErnst HandelRoland MendelAustriaIsland TradingHelen BennettUKKniglich EssenP…

计算机专业表白文案,深情表白文案40句

深情表白文案40句时间&#xff1a;2020-05-17 22:41:51 分类&#xff1a;经典语句 | 适合表白的句子 | Word文档下载深情表白文案40句导语&#xff1a;你是我生活的全部&#xff0c;是我一生拼搏奋斗的赌注。为了你披荆斩棘&#xff0c;为了你艰辛受苦&#xff0c;为了你众生…

【HRBUST - 1054 】Brackets! Brackets! (括号匹配,思维,STL栈)

题干&#xff1a; There are six kinds of brackets: ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{’, ‘}’. dccmx’s girl friend is now learning java programming language, and got mad with brackets! Now give you a string of brackets. Is it valid? For example: “(([{}]))…

计算机中有关数及编码的知识,计算机中有关数及编码的知识

计算机中有关数及编码的知识1.计算机是智能化的电器设备计算机就其本身来说是一个电器设备&#xff0c;为了能够快速存储、处理、传递信息&#xff0c;其内部采用了大量的电子元件&#xff0c;在这些电子元件中&#xff0c;电路的通和断、电压高低&#xff0c;这两种状态最容易…

ACM技巧 - O(1)快速乘(玄学) 总结

ll fast_mult(ll a,ll b,ll mod) {return (a*b - (ll)((long double)a/mod*b)*modmod)%mod; } 参考&#xff1a;https://blog.csdn.net/m0_38013346/article/details/81435369 注意事项&#xff1a; double可能会挂&#xff0c;最好long double。u,v>p可能会挂&#xff0…

html盒子阴影只设置左右,只在容器一边或两边显示盒子阴影

实现方案首先定义一个有具体宽高的盒子&#xff0c;然后正确定位:after伪类。效果[图片上传中...(image.png-8e8e17-1593570486291-0)]底部阴影代码:Document.box-shadow {background-color: #FF8020;width: 160px;height: 90px;position: relative;}.box-shadow:after {conten…

【 HRBUST - 1055】Single(模拟,dp,打表)(总结)

题干&#xff1a; There are many handsome single boys in our team, for example, me. Some times, we like count singles. For example, in the famous “November 11th” (11.11), there are four singles ,so, single is actually 1. For another example, there are 2 …

一年级下册数学计算机应用题,【小学一年级数学练习题】小学生和机器人

【www.jmzhongda.cn--学生个人工作总结】一个小学生在认真地做作业。这是一系列加、减、乘、除的四则应用计算题&#xff0c;难度相当大&#xff0c;特别那几个繁分数题&#xff0c;计算起来太繁杂。他额头上不知不觉地渗出细雨般的汗珠来了。正在这个时候&#xff0c;不知从什…

【EOJ Monthly 2018.10 - A】oxx 的小姐姐们(模拟,水题,填充矩阵,输出格式有坑)

题干&#xff1a; Time limit per test: 1.0 seconds Memory limit: 512 megabytes oxx 和他的小姐姐&#xff08;们&#xff09;躺在图书馆前的大草坪上看星星。 有强迫症的 oxx 想要使得他的小姐姐们正好躺成一块 nm 的长方形。 已知小姐姐的形状是 1p 的长方形&#xf…

数据结构数组计算机中的应用,2018考研计算机:数据结构数组和广义表复习重点...

2018考研计算机&#xff1a;数据结构数组和广义表复习重点2017-08-17 16:00|考研集训营《数据结构(C语言版)》复习重点在二、三、六、七、九、十章&#xff0c;考试内容两大类&#xff1a;概念&#xff0c;算法&#xff0c;自从计算机专业课统考以后&#xff0c;专业课考试题型…

【HDU - 5672】String(尺取法)

题干&#xff1a; There is a string SS.SS only contain lower case English character.(10≤length(S)≤1,000,000)(10≤length(S)≤1,000,000) How many substrings there are that contain at least k(1≤k≤26)k(1≤k≤26) distinct characters? Input There are mul…

【HDU - 2030 】汉字统计 (C语言汉字编码,长知识)

题干&#xff1a; 统计给定文本文件中汉字的个数。 Input 输入文件首先包含一个整数n&#xff0c;表示测试实例的个数&#xff0c;然后是n段文本。 Output 对于每一段文本&#xff0c;输出其中的汉字的个数&#xff0c;每个测试实例的输出占一行。 [Hint:]从汉字机内码的…

【HDU - 1257】最少拦截系统 (标解dp,贪心可过,最长上升子序列类问题)

题干&#xff1a; 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能…

ae toolbarcontrol运行时没有_想办法让AE跑起来

好像大家都已经习惯了ADOBE系列软件的高能量吸收&#xff0c;谁让人家功能强大呢&#xff0c;尤其是AE&#xff0c;发展势头越来越猛&#xff0c;目前可以直接生成JSON文件&#xff0c;解析为代码文件后跨平台使用于安卓和苹果设备上&#xff0c;算是直接搭上了火热的移动互联快…

C++中两个常用的控制语句格式的函数(width和precision函数)

width 语法: int width();int width( int w );函数 width()返回当前的宽度。可选择参数w用于设定宽度大小。宽度是指每一次输出中显示的字符的最小数目。例如&#xff1a; cout.width( 5 );cout << "2";displays 2(在一个2的后面紧跟着四个空格) precision 语…

如何将风险应用加入白名单_理财要如何将资金分散呢?可以参考风险分散和分享经济红利的思路...

#理财大赛第三季#银行理财是值得投资者配置资金的&#xff0c;收益相对稳定&#xff0c;它的风险等级也很明显地标注在产品说明书和风险揭示书上&#xff0c;投资者可以根据自身投资偏好选择适合的理财。大部分买过银行理财的人都会选择将所有资金投入进去。银行理财确实是一个…