forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.py
More file actions
executable file
·33 lines (32 loc) · 730 Bytes
/
d.py
File metadata and controls
executable file
·33 lines (32 loc) · 730 Bytes
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
#!/usr/bin/env python3
# https://abc044.contest.atcoder.jp/tasks/arc060_b
import math
n = int(input())
s = int(input())
f = None
for i in range(2, math.ceil(math.sqrt(n))):
c = 0
k = n
while k > 0:
c += k % i
k //= i
if c == s:
f = i
break
if f is None:
for i in range(int(math.sqrt(n)), 1, -1):
b = n // i
k = n
c = 0
while k > 0:
c += k % b
k //= b
if s >= c and (s - c) % i == 0 and b - (s - c) // i > n // (i + 1):
f = b - (s - c) // i
break
if f is None:
if s == n: f = n + 1
elif s <= (n + 1) // 2: f = n - s + 1
elif s == 1: f = n
if f is None: print(-1)
else: print(f)