-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1086.cpp
43 lines (40 loc) · 878 Bytes
/
1086.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, pw[19], cnt[10], cnt2[10];
bool check(ll x) {
memset(cnt, 0, sizeof(cnt));
for (int k = 0; k <= 17; ++k) {
for (int i = 0; i <= 9; ++i) {
ll a = (i > 0) ? 0 : 1;
ll b = x / pw[k + 1];
while (b * pw[k + 1] + pw[k] * i > x) --b;
if (a > b) continue;
cnt[i] += (b - a + 1) * pw[k];
cnt[i] -= max(0LL, b * pw[k + 1] + pw[k] * (i + 1) - 1 - x);
}
}
for (int i = 0; i <= 9; ++i) {
if (cnt[i] > n) return false;
}
return true;
}
int main() {
cin >> n;
pw[0] = 1;
for (int i = 1; i <= 18; ++i) {
pw[i] = pw[i - 1] * 10;
}
ll lt = 1, rt = 9e17, ans = -1;
while (lt <= rt) {
ll mid = (lt + rt) >> 1;
if (check(mid)) {
ans = mid;
lt = mid + 1;
} else {
rt = mid - 1;
}
}
cout << ans;
return 0;
}