Lucky sumtime limit: 2000 msmemory limit: 262144 kbthis problem will be judged on codeforces. Original ID: 121a
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.
LetNext(X) Be the minimum lucky number which is larger than or equalsX. Petya is interested what is the value of the expressionNext(L) Accept + acceptNext(LPipeline + pipeline 1) pipeline + pipeline... pipeline + PipelineNext(RExecutor-cores 1) memory + CoresNext(R). Help him solve this problem.
Input
The single line contains two integersLAndR(1 digit ≤ DigitLLimit ≤ limitRLimit ≤limit 109)-the left and right interval limits.
Output
In the single line print the only number-the sumNext(L) Accept + acceptNext(LPipeline + pipeline 1) pipeline + pipeline... pipeline + PipelineNext(RExecutor-cores 1) memory + CoresNext(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 inputinput
2 7
Output
33
Input
7 7
Output
7
Hint
In the first sample:Next(2) accept + acceptNext(3) accept + acceptNext(4) accept + acceptNext(5) accept + acceptNext(6) accept + acceptNext(7) rows = listen 4 rows + Listen 4 rows + limit 4 rows + limit 7 rows + limit 7 rows + limit 7 rows = Limit 33
In the second sample:Next(7) Limit = Limit 7
Sourcecodeforces beta round #91 (Div. 1 only) Problem Solving: contains only a few numbers 4 or 7. Search again.
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 long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 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\n",sum(rt)-sum(lt-1));46 return 0;47 }View code
Codeforces 121a lucky sum