Skip to content

Commit 6e6058b

Browse files
authored
Merge pull request #1739 from AlgorithmWithGod/khj20006
[20251225] BOJ / D5 / Divine Divisor / 권혁준
2 parents 1818329 + d05f5ac commit 6e6058b

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
```python
2+
import random
3+
4+
prime_for_miller_rabin = [2,3,5,7,11,13,17,19,23,29,31,37]
5+
prime_factors_count = dict()
6+
7+
def power(a,b,c):
8+
if b == 0: return 1
9+
if b == 1: return a % c
10+
h = power(a, b>>1, c) % c
11+
h = (h * h) % c
12+
if b%2 == 1: return (h*a)%c
13+
return h
14+
15+
def miller_rabin(x):
16+
if x <= 1: return False
17+
if x <= 3: return True
18+
if x%2 == 0: return False
19+
20+
for prime in prime_for_miller_rabin:
21+
if x == prime: return True
22+
if x % prime == 0: return False
23+
24+
t = (x-1) >> 1
25+
may_prime = False
26+
while True:
27+
v = power(prime, t, x)
28+
if v == x-1:
29+
may_prime = True
30+
break
31+
if t%2 == 1:
32+
if v == x-1 or v == 1:
33+
may_prime = True
34+
break
35+
t = t>>1
36+
37+
if x > 40 and may_prime == False:
38+
return False
39+
40+
if x > 40: return True
41+
return False
42+
43+
def gcd(a,b):
44+
if a == 0: return b
45+
if b == 0: return a
46+
if a < b:
47+
t = a
48+
a = b
49+
b = t
50+
if a%b == 0: return b
51+
return gcd(b, a%b)
52+
53+
def pollard_rho(x):
54+
if x == 1: return
55+
if x%2 == 0:
56+
try:
57+
prime_factors_count[2] = prime_factors_count[2]+1
58+
except:
59+
prime_factors_count[2] = 1
60+
pollard_rho(x>>1)
61+
return
62+
if miller_rabin(x):
63+
try:
64+
prime_factors_count[x] = prime_factors_count[x]+1
65+
except:
66+
prime_factors_count[x] = 1
67+
return
68+
69+
c = random.randint(1,20)
70+
a = random.randint(2,x-1)
71+
b = a
72+
v = 1
73+
74+
def f(g):
75+
return (g*g + c)%x
76+
77+
while v == 1:
78+
a = f(a)
79+
b = f(f(b))
80+
v = gcd(abs(a-b), x)
81+
82+
pollard_rho(v)
83+
pollard_rho(x//v)
84+
85+
86+
N = int(input())
87+
a = map(int, input().split())
88+
for i in a:
89+
pollard_rho(i)
90+
91+
count_max, count = 0, 0
92+
for prime in prime_factors_count:
93+
cnt = prime_factors_count[prime]
94+
if cnt > count_max:
95+
count_max = cnt
96+
count = 1
97+
elif cnt == count_max:
98+
count += 1
99+
100+
ans = 1
101+
for _ in range(count):
102+
ans = ans << 1
103+
104+
print(count_max)
105+
print(ans-1)
106+
```

0 commit comments

Comments
 (0)