-
Notifications
You must be signed in to change notification settings - Fork 0
/
095.py
executable file
·49 lines (43 loc) · 945 Bytes
/
095.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
#!/usr/bin/python2
import math
def cnt(x):
s = 0
for d in xrange(2, int(round(math.sqrt(x))) + 1):
if x % d == 0:
s += d
if x // d != d: s += x // d
return s + 1
#a = map(cnt, range(10**6 + 1))
a = []
with open('095.dat') as f:
a = map(int, f.read().split(','))
assert a[220] == 284
assert a[284] == 220
def chain(i):
s = set()
n = i
cnt = 1
curMin = 10**9
while not n in s:
s.add(n)
cnt += 1
if n < curMin:
curMin = n
n = a[n]
if n > 10**6:
return (0, 0)
if n == i:
return (cnt, curMin)
else:
return (0, 0)
assert chain(12496) == (6, 12496)
longest = 0
longestFor = 0
minInLongest = 0
for i in range(2, 10**6 + 1):
(cnt, curMin) = chain(i)
if cnt > longest:
longestFor = i
longest = cnt
minInLongest = curMin
print(longestFor, longest, minInLongest)