牛客算法周周练1 E:幸运数字Ⅱ

E:幸运数字Ⅱ

直接dfs把这些数找出来,最多2^11个数。
然后二分找一下l和r的位置,乘起来就好了,注意4444444444要放进去。

code

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int man = 2e5+10;
#define IOS ios::sync_with_stdio(0)
template <typename T>
inline T read(){T sum=0,fl=1;int ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')fl=-1;
for(;isdigit(ch);ch=getchar())sum=sum*10+ch-'0';
return sum*fl;}
template <typename T>
inline void write(T x) {static int sta[35];int top=0;
do{sta[top++]= x % 10, x /= 10;}while(x);
while (top) putchar(sta[--top] + 48);}
template<typename T>T gcd(T a, T b) {return b==0?a:gcd(b, a%b);}
template<typename T>T exgcd(T a,T b,T &g,T &x,T &y){if(!b){g = a,x = 1,y = 0;}
else {exgcd(b,a%b,g,y,x);y -= x*(a/b);}}
#ifndef ONLINE_JUDGE
#define debug(fmt, ...) {printf("debug ");printf(fmt,##__VA_ARGS__);puts("");}
#else
#define debug(fmt, ...)
#endif
typedef long long ll;
const ll mod = 1e9+7;
vector<ll>sp;
ll l,r;

void dfs(ll sum){
    if(sum>1e11)return;
    if(sum!=0)sp.push_back(sum);
    dfs(sum*10+4);
    dfs(sum*10+7);
}

int main() {
    #ifndef ONLINE_JUDGE
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt","w",stdout);
    #endif
    cin >> l >> r;
    dfs(0);
    sort(sp.begin(),sp.end());
    int id1 = lower_bound(sp.begin(),sp.end(),l) - sp.begin();
    int id2 = lower_bound(sp.begin(),sp.end(),r) - sp.begin();
    ll sum = 0;
    int pre = l;
    //cout << id1 << " " << id2 << endl;
    for(int i = id1;i <= id2;i++){
        int cnt;
        if(sp[i]<=r)cnt = sp[i] - pre + 1;
        else cnt = r - pre + 1;
        sum += 1ll*sp[i]*cnt;
        pre = sp[i] + 1;
    }
    cout << sum << endl;
    return 0;
}
原创文章 93 获赞 12 访问量 8995

猜你喜欢

转载自blog.csdn.net/weixin_43571920/article/details/105385385
今日推荐