幸运数字(dfs+暴力)-牛客

时间:2020-08-08
本文章向大家介绍幸运数字(dfs+暴力)-牛客,主要包括幸运数字(dfs+暴力)-牛客使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

幸运数字(dfs+暴力)-牛客

题意:定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。 比如说,47、744、4都是幸运数字而5、17、467都不是。

定义next(x)为大于等于x的第一个幸运数字。给定l,r,请求出next(l) + next(l + 1) + ... + next(r - 1) + next(r)。

暴力找出所有的幸运数字,存进数组,二分查找,分块求和

代码:

#include<bits/stdc++.h>
/*#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>*/
using namespace std;
const int maxn=1e6+10;
const int mod=19260817;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> pii;
const int N=5e5+10;

ll l,r;
ll ans;
ll a[maxn];
int cnt=2;

void dfs(ll x)
{
    if(x>1e9) return ;
    if(x>10)
     a[cnt++]=x;
     dfs(x*10+4);
     dfs(x*10+7);
}
int main()
{
    cin>>l>>r;
    a[0]=4;
    a[1]=7;
    dfs(4);
    dfs(7);
    a[cnt]=44444444444;//界限
    sort(a,a+cnt+1);
    int L=lower_bound(a,a+cnt+1,l)-a;
    int R=upper_bound(a,a+cnt+1,r)-a;
    for(int i=L; i<=R; i++)
    {
        ans+=(min(a[i],r)-l+1)*a[i];//分块
        //cout<<ans<<" ";
        l=a[i]+1;
    }
    cout<<ans<<endl;
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/sweetlittlebaby/p/13460606.html