幸运数字Ⅱ

幸运数字Ⅱ

https://ac.nowcoder.com/acm/problem/15291

题目链接:https://ac.nowcoder.com/acm/problem/15291
分析:由于这道题的l 和 r的上线都很接近于int上限,觉得类型转换比较烦,所以索性把所有数据都声明为long long了
在1e10里面的幸运数字个数为 2+2^2+2^3+2^4+...+2^10 数量级大概2的11次左右,很小。所以我们是可以大表存下所有1e10以内的幸运数的,当然最后还要加一个4444444444,否则r=1e10,就找不到大于1e10的幸运数了。
之后对于这个幸运数数组从小到大排序,用二分去查找l和r在其中的相对位置(当然用lower_bound和upper_bound)
然后对于范围中的每一个幸运数字:
ans += ( min ( a[i] , r) - l + 1) * a[i];
注意的是 这个l要实时更新。
具体看了代码都能明白的:

#include<iostream>
#include<algorithm>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<map>
#define ll long long
using namespace std;
#define close_stdin  ios::sync_with_stdio(false)
ll l, r,ans=0;
ll a[100005];
int cnt = 2;
void dfs(ll x) {
    if (x > 10000000000) { return; }
    if (x > 10) { a[cnt++] = x; }
    dfs(x * 10 + 4);
    dfs(x * 10 + 7);
}
void solve() {
    a[0] = 4;
    a[1] = 7;
    dfs(4);
    dfs(7);
    a[cnt] = 4444444444;
    sort(a, a + cnt + 1);
    cin >> l >> r;
    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];
        l = a[i] + 1;
    }
    cout << ans<<"\n";
}
int main() {
    close_stdin;//只是为了让cin和printf一样快
    solve();
}

谢谢浏览哈!

全部评论
R不应该是lower_bound吗?如果r等于某个幸运数,用upper_bound不就多加了一次吗
1 回复 分享
发布于 2023-07-11 10:27 河南

相关推荐

06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
第一份工作能做外包吗?
点赞 评论 收藏
分享
评论
14
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务