-
Notifications
You must be signed in to change notification settings - Fork 222
/
waiter.py
41 lines (34 loc) · 1.02 KB
/
waiter.py
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
#!/usr/bin/env python3
import sys
def gen_primes(n):
nroot = int(n**(0.5))
sieve = list(range(n+1))
sieve[1] = 0
for i in range(2, nroot+1):
if sieve[i] != 0:
m = n//i - i
sieve[i*i: n+1:i] = [0] * (m+1)
return [x for x in sieve if x !=0]
if __name__ == "__main__":
n, q = input().strip().split(' ')
n, q = [int(n), int(q)]
plates = list(map(int, input().strip().split(' ')))
primes = gen_primes(10000)
b_stacks = []
plate_stacks = [plates, []]
for ind in range(q):
a_ind = int(ind % 2)
b = []
while len(plate_stacks[a_ind]) > 0:
pl = plate_stacks[a_ind].pop()
if pl % primes[ind] == 0:
b.append(pl)
else:
plate_stacks[1 - a_ind].append(pl)
b_stacks.append(b)
for b in b_stacks:
if len(b) > 0:
print(*b[::-1], sep='\n')
for pl in plate_stacks:
if len(pl) > 0:
print(*pl[::-1], sep='\n')