Python解法
2026-03-26 16:23:02
发布于:北京
3阅读
0回复
0点赞
Python的解法如下
import sys
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
m1 = int(input[ptr])
m2 = int(input[ptr+1])
ptr +=2
s = list(map(int, input[ptr:ptr+N]))
if m1 == 1:
print(0)
return
# 分解 m1
factors = []
tmp = m1
i = 2
while i*i <= tmp:
if tmp % i == 0:
c = 0
while tmp % i == 0:
c +=1
tmp //=i
factors.append((i, c*m2))
i +=1
if tmp>1:
factors.append((tmp, m2))
min_t = float('inf')
for num in s:
ok = True
mt = 0
for p, need in factors:
if num % p != 0:
ok = False
break
c = 0
x = num
while x % p ==0:
c +=1
x //=p
t = (need + c -1) // c
if t>mt:
mt = t
if ok:
if mt < min_t:
min_t = mt
print(min_t if min_t != float('inf') else -1)
if __name__ == '__main__':
main()
全部评论 1
强
3天前 来自 浙江
0







有帮助,赞一个