-
Notifications
You must be signed in to change notification settings - Fork 185
/
1086.cc
50 lines (46 loc) · 786 Bytes
/
1086.cc
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
44
45
46
47
48
49
50
// https://cses.fi/problemset/task/1086/
#include <iostream>
using namespace std;
typedef long long ll;
ll s(ll n, int k) {
if (n < k) return -1;
if (n < 2 * k + 1) return 0;
if (n <= k * 10) {
ll a = 2 * k + 1;
ll r = 1;
while (a + k <= n) {
a += k;
r += 1;
}
return r;
}
ll a = k * 10 + 1;
ll r = 1;
ll next = (a + r) * 10;
while (next + k <= n) {
a = (a + r) * 10;
r *= 10;
next = (a + r) * 10;
}
r *= 10;
ll d1 = next - a * 9;
ll d = a;
ll i = 1;
ll r2 = r;
if (a + d1 + k <= n) {
i++;
a += d1;
r2 += r;
while (a + d + k <= n) {
i++;
a += d;
r2 += r;
}
}
return r2 + s(n - a, k + (i == 1));
}
int main () {
ll n;
cin >> n;
cout << s(n, 0) << endl;
}