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

题意:只含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;
}

猜你喜欢

转载自blog.csdn.net/zhubozhen/article/details/79936372
今日推荐