-
Notifications
You must be signed in to change notification settings - Fork 6
/
rasterized.py
38 lines (34 loc) · 920 Bytes
/
rasterized.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
LIMIT = 10**7
spf = list(range(LIMIT+1))
primes = [2]
p = 3
while p <= LIMIT:
if spf[p] == p:
primes.append(p)
for i in range(p*p, LIMIT+1, 2*p):
if spf[i] == i: spf[i] = p
p += 2
def tot(n):
res = n
for p, _ in pf(n): res //= p; res *= p-1
return res
def pf(n):
res = []
idx = k = 0
while n != 1 and idx < len(primes):
pp = primes[idx]
if pp*pp > n: break
if n % pp == 0:
while n % pp == 0: n //= pp; k += 1
if k: res.append((pp, k))
idx += 1; k = 0
if n != 1: res.append((n, 1))
return res
def solve(n):
divs = [1]
for p, k in pf(n):
l = len(divs)
for _ in range(k*l): divs.append(divs[len(divs)-l]*p)
print(sum(tot(i+1) for i in divs))
import sys; input = sys.stdin.readline
for _ in range(int(input())): solve(int(input()))