CodeForces

来源:互联网 发布:云计算板块平均市盈率 编辑:程序博客网 时间:2024/06/05 18:01

Lucky Sum

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).

Example

Input2 7Output33Input7 7Output7

Note

In the first sample: next(2) + next(3) + next(4) + next(5) + next(6) + next(7) = 4 + 4 + 4 + 7 + 7 + 7 = 33In the second sample: next(7) = 7

next数组可以打表(打大一点),稍微优化一下就能过。


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 2222#define ll long longll a[N];int ans = 0;void init( ll num ){    if( num >= 1e10 ) return ;/// mx > 1e9    else a[ans++] = num;    init( num * 10 + 4 );    init( num * 10 + 7 );}int main(){    freopen( "in.txt", "r", stdin );    init( 0 );    sort( a, a + ans );//for( int i = 0 ; i < 1023 ; i ++ )cout<<a[i]<<" ";    int n, m, l, r, mid;    ll sum;    while( scanf( "%d%d", &n, &m ) != EOF ){    ///还想用二分的,但是好像不适用。//        l = 1, r = ans-1;//        while( l <= r ){//            mid = (l + r) / 2;//            if( a[mid] > n )//                r = mid - 1;//            else if( a[mid] < n )//                l = mid + 1;//            else break;//        }        if( n < 1 && m < 1 ) break;        l = 0, r = 0;        for( int i = 1; i < ans ; i ++ ){            if( a[i] >= n && l == 0 )                l = i;            if( a[i] >= m && r == 0 )                r = i;            if( l && r ) break;        }        sum = 0;        if( l == r ){            for( int i = n ; i <= m ; i ++ ){                sum += a[l];            }        }        else{            sum += ( a[l] - n + 1 ) * a[l];/// +1            for( int i = l+1  ; i <= ans ; i ++ ){                if( a[i] >= m ){                    sum += ( m - a[i-1] ) * a[i];                    break;                }                else{                    sum += ( a[i] - a[i-1] ) * a[i];                }            }        }        cout<<sum<<endl;    }    return 0;}
原创粉丝点击