Lucky Sum
64-bit integer IO format: %I64d Java class name: (Any)
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 %I64dspecificator.
Sample Input
2 7
33
7 7
7
Hint
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 20000; 18 LL d[maxn]; 19 int tot = 1; 20 void dfs(LL num,int cur) { 21 d[tot++] = num; 22 if(cur > 11) return; 23 dfs(num*10+4,cur+1); 24 dfs(num*10+7,cur+1); 25 } 26 LL sum(LL x) { 27 if(x == 0) return 0; 28 LL temp = 0; 29 for(int i = 1; i < tot; i++) { 30 if(x >= d[i]) 31 temp += d[i]*(d[i]-d[i-1]); 32 else { 33 temp += d[i]*(x-d[i-1]); 34 break; 35 } 36 } 37 return temp; 38 } 39 int main() { 40 dfs(4,0); 41 dfs(7,0); 42 sort(d+1,d+tot); 43 LL lt,rt; 44 while(~scanf("%I64d %I64d",<,&rt)) 45 printf("%I64d ",sum(rt)-sum(lt-1)); 46 return 0; 47 }