定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
定义next(x)为大于等于x的第一个幸运数字。给定l,r,请求出next(l) + next(l + 1) + … + next(r - 1) + next(r)。
输入描述:
两个整数l和r (1 <= l <= r <= 1000,000,000)。
输出描述:
一个数字表示答案。
示例1
输入
2 7
输出
33
示例2
输入
7 7
输出
7
#include<iostream>
#include<algorithm>
typedef long long LL;
LL a[100010];
LL cnt, l, r, res;
inline void dfs(LL x) {
if (x >= 10000000000) return;
if (x != 0) a[cnt++] = x;
dfs(x * 10 + 4);
dfs(x * 10 + 7);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
dfs(0);
a[cnt] = 4444444444;
std::sort(a, a + cnt + 1);
std::cin >> l >> r;
int L = std::lower_bound(a, a + cnt + 1, l) - a;
int R = std::upper_bound(a, a + cnt + 1, r) - a;
for (int i = L; i <= R; i++) {
res += (std::min(a[i], r) - l + 1) * a[i];
l = a[i] + 1;
}
std::cout << res <<'\n';
return 0;
}