Description
Figo 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
There are multiple cases.
Every following line contains two integers l and r (1 ≤ l ≤ r ≤ 1000000000) — the left and
right interval limits. Output
For every case print a single number — the sum next(l) + next(l + 1) + ... + next(r - 1) + next(r). Sample Input
1 2
2 4 Sample Output
8
Figo 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
There are multiple cases.
Every following line contains two integers l and r (1 ≤ l ≤ r ≤ 1000000000) — the left and
right interval limits. Output
For every case print a single number — the sum next(l) + next(l + 1) + ... + next(r - 1) + next(r). Sample Input
1 2
2 4 Sample Output
8
12
#include <iostream>
using
namespace
std;
typedef
long
long
LL;
const
int
maxn = 55000;
const
LL
pow
= 10000000005;
LL ans[maxn];
void
solve()
{
ans[0]=4;
ans[1]=7;
LL pre,rear,temp;
pre=0; rear=2;
while
(pre<rear)
{
temp=ans[pre]*10+ans[0];
if
(temp>
pow
)
break
;
else
ans[rear++]=temp;
temp=ans[pre]*10+ans[1];
if
(temp>
pow
)
break
;
else
ans[rear++]=temp;
pre++;
}
}
int
main()
{
LL l,r,i,j,pre,rear,sum;
solve();
while
(cin>>l>>r)
{
sum=0;
if
(r<=4)
{
for
(i=l;i<=r;i++)
sum+=4;
cout<<sum<<endl;
continue
;
}
for
(i=0;i<maxn;i++)
if
(ans[i]>=l)
break
;
pre=i;
for
(;i<maxn;i++)
if
(ans[i]>=r)
break
;
rear=i;
if
(rear==pre)
{
sum=(r-l+1)*ans[pre];
}
else
{
sum+=(ans[pre]-l+1)*ans[pre];
sum+=(r-ans[rear-1])*ans[rear];
for
(i=pre+1;i<rear;i++)
sum+=(ans[i]-ans[i-1])*ans[i];
}
cout<<sum<<endl;
}
return
0;
}