AcWing 3734. 求和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL l, r, ans;
vector<LL> s;
void dfs(int c, LL x) {
s.emplace_back(x);
if (c == 10) return;
dfs(c + 1, 10 * x + 4);
dfs(c + 1, 10 * x + 7);
}
int main() {
s.clear();
dfs(0, 0);
sort(s.begin(), s.end());
scanf("%lld%lld", &l, &r);
ans = 0;
for (int i = 1; i < s.size(); ++i) {
LL a = s[i - 1] + 1, b = s[i];
ans += s[i] * max(0ll, min(r, b) - max(l, a) + 1);
}
printf("%lld\n", ans);
return 0;
}