Codeforces Beta Round #91 (Div. 2 Only) C. Lucky Sum

来源:互联网 发布:英国工资 知乎 编辑:程序博客网 时间:2024/04/26 15:22

原题链接

C. Lucky Sum
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 477444 are lucky and 517467 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 expressionnext(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 cincout 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


#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>#include <map>#include <cmath>#include <queue>#include <ctime> #define maxn 1000005#define INF 1e18using namespace std;typedef long long ll;ll solve(ll m){\\找到大于等于m的最小幸运数char ch[20];int cnt = 0;while(m){ch[cnt++] = m % 10;m /= 10;}ch[cnt] = 0;    for(int i = cnt-1; i >= 0; i--){    if(ch[i] == 4 || ch[i] == 7)      continue;    if(ch[i] < 4){    ch[i] = 4;    for(int j = i-1; j >= 0; j--)     ch[j] = 4;    break;    }    if(ch[i] < 7){    ch[i] = 7;    for(int j = i-1; j >= 0; j--)     ch[j] = 4;    break;    }    if(ch[i] > 7){    ch[i] = 4;    for(int j = i-1; j >= 0; j--)      ch[j] = 4;    ch[i+1]++;    if(i+1 == cnt){    ch[i+1] = 4;    cnt++;    break;    }    for(int j = i+1; j < cnt; j++){    if(ch[j] > 7){    ch[j] = 4;    ch[j+1]++;    if(j+1 == cnt){     ch[j+1] = 4;     cnt++;     break;        }    }    if(ch[j] > 4){    ch[j] = 7;    break;    }    }    break;    }    }ll ans = 0;for(int i = cnt-1; i >= 0; i--)  ans = ans * 10 + ch[i];return ans;}int main(){//freopen("in.txt", "r", stdin);ll l, r;scanf("%I64d%I64d", &l, &r);ll ans = 0;while(1){ll d = solve(l);if(d >= r){ans += (r - l + 1) * d;break;}ans += (d - l + 1) * d;l = d + 1;}printf("%I64d\n", ans);return 0;}


0 0
原创粉丝点击