Lucky Numbers

本文介绍了一种算法,用于计算特定区间内所有大于等于该区间的最小幸运数之和。幸运数是指仅由数字4和7组成的正整数。算法首先生成所有可能的幸运数,然后针对每个输入区间计算所需的总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值