CodeForces - 122C Lucky Sum

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

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

next數組可以打表(打大一點),稍微優化一下就能過。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 2222
#define ll long long
ll 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章