-
Notifications
You must be signed in to change notification settings - Fork 1
/
uva_516.py
61 lines (44 loc) · 883 Bytes
/
uva_516.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import math
def pow (a, n):
ret = 1
while n:
if n&1:
ret *= a
a *= a
n >>= 1
return ret
#get prime numbers less than 32768
M = 32768
isPrime = [1]*M
for i in range(2, int(math.sqrt(M))):
if isPrime[i]:
for j in range(i*i, M, i):
isPrime[j] = 0
primes = []
for x in range(2, M):
if isPrime[x]:
primes.append(x)
#process cases
while 1:
a = [int(x) for x in input().split()]
if len(a) == 1 :
break
n = 1
for i in range(0,len(a)-1, 2):
n *= pow(a[i], a[i+1])
n -= 1
ans = []
for prime in primes:
if prime > n: break
if n%prime == 0:
cnt = 0
while n%prime == 0:
n //= prime
cnt += 1
ans.append([prime, cnt])
if n == 1: break
ans.sort(reverse=True)
print("{} {}".format(ans[0][0], ans[0][1]), end='')
for i in range(1, len(ans)):
print(" {} {}".format(ans[i][0], ans[i][1]), end='')
print()