当前位置:   article > 正文

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

codeforces 122c

题干:

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代码:

  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. ll b[100005];
  5. int tot;
  6. void bfs() {
  7. priority_queue<ll,vector<ll>,greater<ll> > pq;
  8. tot = 0;
  9. b[++tot] = 4;pq.push(4);
  10. b[++tot] = 7;pq.push(7);
  11. while(!pq.empty()) {
  12. ll cur = pq.top();pq.pop();
  13. if(cur > (ll)1e12+7) return ;
  14. b[++tot] = cur*10+4;pq.push(b[tot]);
  15. b[++tot] = cur*10+7;pq.push(b[tot]);
  16. }
  17. }
  18. int main()
  19. {
  20. bfs();
  21. ll l,r;
  22. cin>>l>>r;
  23. // cout << b[tot] << endl;
  24. ll ans = 0;
  25. int st = lower_bound(b+1,b+tot+1,l) - b;
  26. int ed = lower_bound(b+1,b+tot+1,r) - b;
  27. if(ed > st) {
  28. ans += (b[st] - l + 1) * b[st];
  29. for(int i = st+1; i<ed; i++) {
  30. ans += (b[i] - b[i-1]) * b[i];
  31. }
  32. ans += (r-b[ed-1]) * b[ed];
  33. }
  34. else ans += (r-l+1) * b[st];
  35. cout << ans << endl;
  36. return 0 ;
  37. }

法2:分块:(还未看)

  1. #include<bits/stdc++.h>
  2. #define IO ios::sync_with_stdio(false);\
  3. cin.tie(0);\
  4. cout.tie(0);
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 1e5+10;
  8. ll last(ll x) {
  9. int len = int(log10(x)) + 1; //数字位数
  10. ll ans = 0,cnt = 0;
  11. for(int i=0; i<len; i++) //同等位数最大最小幸运数
  12. ans = ans*10+4,cnt = cnt*10+7;
  13. if(x>cnt) //位数+1
  14. return ans*10+4;
  15. while(ans<x) {
  16. ll res = cnt;
  17. for(int i=0; i<1<<len; i++) {
  18. ll tmp = 0;
  19. for(int j=0; j<len; j++) {
  20. if(i&(1<<j))
  21. tmp = tmp*10+7;
  22. else
  23. tmp = tmp*10+4;
  24. }
  25. if(tmp>=x)
  26. res = min(res,tmp);
  27. }
  28. ans = res;
  29. }
  30. return ans;
  31. }
  32. int main()
  33. {
  34. ll l,r;
  35. cin>>l>>r;
  36. ll now = l,ans = 0;
  37. while(true) {
  38. ll la = last(now);
  39. if(la>r) {
  40. ans+= la * (r-now+1);
  41. break;
  42. } else
  43. ans += la * (la-now+1);
  44. now = la+1;
  45. }
  46. cout<<ans<<endl;
  47. return 0;
  48. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/75793
推荐阅读
相关标签
  

闽ICP备14008679号