-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjosephus_problem_2.py
More file actions
74 lines (57 loc) · 1.4 KB
/
josephus_problem_2.py
File metadata and controls
74 lines (57 loc) · 1.4 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
# Fast I/O
input = sys.stdin.readline
# Common imports
from collections import defaultdict, deque, Counter
from itertools import combinations, permutations, product, accumulate
from functools import lru_cache, reduce
import heapq
import bisect
# Constants
MAX = float('inf')
MIN = float('-inf')
MOD = 10**9 + 7
import sys
input = sys.stdin.readline
class Fenwick:
def __init__(self, n):
self.n = n
self.bit = [0] * (n + 1)
def add(self, i, v):
while i <= self.n:
self.bit[i] += v
i += i & -i
def sum(self, i):
s = 0
while i > 0:
s += self.bit[i]
i -= i & -i
return s
def kth(self, k):
""" smallest index i such that sum(i) >= k """
idx = 0
bit_mask = 1 << (self.n.bit_length())
while bit_mask:
nxt = idx + bit_mask
if nxt <= self.n and self.bit[nxt] < k:
k -= self.bit[nxt]
idx = nxt
bit_mask >>= 1
return idx + 1
def solve():
n, k = map(int, input().split())
fw = Fenwick(n)
for i in range(1, n + 1):
fw.add(i, 1)
pos = 0
alive = n
ans = []
for _ in range(n):
pos = (pos + k) % alive
idx = fw.kth(pos + 1)
ans.append(idx)
fw.add(idx, -1)
alive -= 1
print(*ans)
if __name__ == "__main__":
solve()