fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define ld long double
  5. #define vi vector<int>
  6. #define vl vector<ll>
  7. #define vld vector<ld>
  8. #define all(a) a.begin(), a.end()
  9. #define ull unsigned long long
  10. #define endl '\n'
  11. const ll MOD = 1e9 + 7;
  12.  
  13. #pragma GCC optimize("Ofast")
  14. #pragma GCC optimize("unroll-loops")
  15. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  16.  
  17. void fileio() {
  18. freopen("input.txt", "r", stdin);
  19. freopen("output.txt", "w", stdout);
  20. }
  21.  
  22. void read2d(vector<vector<int>>&arr,int n,int m) {
  23. for (int i =0;i<n;i++) {
  24. for (int j =0;j<m;j++) {
  25. cin>>arr[i][j];
  26. }
  27. }
  28. }
  29.  
  30. void print2d(vector<vector<int>>&arr,int n,int m) {
  31. for (int i =0;i<n;i++) {
  32. for (int j =0;j<m;j++) {
  33. cout<<arr[i][j]<<" ";
  34. }
  35. cout<<'\n';
  36. }
  37. }
  38.  
  39. void read1d(vector<int>&arr) {
  40. for (int i =0;i<arr.size();i++) {
  41. cin>>arr[i];
  42. }
  43. }
  44. void read1d(vector<ll>&arr) {
  45. for (int i =0;i<arr.size();i++) {
  46. cin>>arr[i];
  47. }
  48. }
  49.  
  50. void print1d(vector<int>&arr) {
  51. for (int i =0;i<arr.size();i++) {
  52. cout<<arr[i]<<" ";
  53. }
  54. }
  55. string s ;vector<ll>lucky;
  56. ll cnt;ll ans=LLONG_MAX;
  57. bool isLucky(ll n ) {
  58. string s = to_string(n);
  59. for (auto c : s ) {
  60. if (c!='4'&&c!='7') {
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. bool isSuperLucky(ll n ) {
  67. int cnt4=0,cnt7=0;
  68. string s = to_string(n);
  69. for (auto c : s ) {
  70. if (c!='4'&&c!='7') {
  71. return false;
  72. }
  73. if (c=='4') {
  74. cnt4++;
  75. }
  76. if (c=='7') {
  77. cnt7++;
  78. }
  79. }
  80. return cnt4==cnt7;
  81. }
  82. ll howmany(ll n ) {
  83. int cnt=0;
  84. while (n>0) {
  85. n/=10;
  86. cnt++;
  87. }
  88. return cnt;
  89. }
  90. ll sum=0;ll l , r,digits;ll last;
  91. void genhelper(int n ,int cr,ll new_number) {
  92. if (cr==11) {
  93. return;
  94. }
  95. if (isLucky(new_number)&&new_number>=r) {
  96. ans=min(ans,new_number);
  97. return;
  98. }
  99.  
  100.  
  101. genhelper(n,cr+1,new_number*10 + 4 );
  102. genhelper(n,cr+1,new_number*10 + 7 );
  103. }
  104.  
  105. void genhelper2(int cr, ll new_number) {
  106. if (cr == 11) {
  107. return;
  108. }
  109.  
  110. if (new_number > r) {
  111. return;
  112. }
  113.  
  114. if (new_number > 0) {
  115. ans = max(ans, new_number);
  116. }
  117.  
  118. genhelper2(cr + 1, new_number * 10 + 4);
  119. genhelper2(cr + 1, new_number * 10 + 7);
  120. }
  121. void gen(int n ,int len,int cr,ll new_number) {
  122. if (cr==len) {
  123. bool found=0;
  124. if (new_number>=l&&new_number<=r) {
  125. found=1;
  126. sum+=(new_number-l)*new_number+new_number;
  127. l=new_number+1;
  128. }
  129.  
  130. return;
  131. }
  132. gen(n,len,cr+1,new_number*10 + 4 );
  133. gen(n,len,cr+1,new_number*10 + 7 );
  134. }
  135.  
  136. void solve() {
  137. ans=0,sum=0;
  138. cin>>l>>r;
  139. genhelper2(0,0);
  140. last=ans;
  141. ans=LLONG_MAX;
  142. genhelper(0,0,0);
  143. for (int i =1;i<=10;i++) {
  144. gen(0,i,0,0);
  145. }
  146. if (l>last && !isLucky(r)) {
  147. sum+=(r-l+1)*ans;
  148. }
  149. cout<<sum<<endl;
  150.  
  151. }
  152. int main()
  153. {
  154. ios::sync_with_stdio(false);
  155. cin.tie(nullptr);
  156.  
  157. // fileio();
  158.  
  159. int t = 1;
  160. //cin >> t;
  161. while (t--)
  162. {
  163. solve();
  164. }
  165. }
  166.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
0