插件下载失败,那么我们来做道题目吧!

题意:只含4和7的数字为幸运数字,令next(x)为大于等于x的第一个幸运数字,给出l,r,求出next(l)+next(l+1)…..+next(r)的和。
分析:首先,只含有4和7,那先dfs一遍,求出范围内的数,然后用lower_bound定位,累加。

#include<iostream>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<set>
#include<cmath>

using namespace std;
vector<long long> s;
void dfs(int now,int deep,long long cnt){
    if(now==deep){
        s.push_back(cnt);
        return ;
    }
    else{
        dfs(now+1,deep,cnt*10+4);
        dfs(now+1,deep,cnt*10+7);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    long long l,r;
    cin>>l>>r;
    s.clear();
    for(int i=1;i<=10;i++){
        dfs(0,i,0);
    }
    sort(s.begin(),s.end());
    long long ans=0;
    while(l<=r){
        int pos=lower_bound(s.begin(),s.end(),l)-s.begin();
        long long tot=min(s[pos],r)-l+1;
        ans=ans+tot*s[pos];
        l=s[pos]+1;
    }
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值