約數

gebeng發表於2024-04-02
from math import sqrt
from collections import Counter
# ①試除法求約數

x = int(input())

ans = []

for i in range(1,int(sqrt(x)) + 1):
    if x % i == 0:
        ans.append(i)

print(ans)


# ②求多個數乘積的約數個數

n = int(input())
a = [*map(int,input().split())]
cnt = Counter()
MOD = pow(10,9) + 7
# 分解質因數,並得到每個質數的個數!
# n = x1 ^ p1 + x2 ^ p2 + ... + xn ^ pn
# cnt = (1 + p1) * (1 + p2) * ... * (1 + pn)
# 相當於排列組合,且由於是質數,每一個組合得到的約束唯一!
for x in a:
    i = 2
    while i * i <= x:
        while x % i == 0:
            x //= i
            cnt[i] += 1
        i += 1
    if x > 1:
        cnt[x] += 1

ans = 1
for p in cnt.values():
    ans = (ans * (1 + p)) % MOD
print(ans)


# ③求多個數乘積的約束之和

n = int(input())
a = [*map(int,input().split())]
cnt = Counter()
MOD = pow(10,9) + 7
# 分解質因數,並得到每個質數的個數!
# n = x1 ^ p1 + x2 ^ p2 + ... + xn ^ pn
# cnt = (1 + x1 + x1 ^ 2 + ... + x1 ^ p1) * (1 + x2 + x2 ^ 2 + ... + x2 ^ p2) * ... * ...

for x in a:
    i = 2
    while i * i <= x:
        while x % i == 0:
            x //= i
            cnt[i] += 1
        i += 1
    if x > 1:
        cnt[x] += 1

ans = 1
for x,cnt in cnt.items():
    tmp = 1
    # 秦九韶演算法
    for _ in range(cnt):
        tmp = (tmp * x + 1) % MOD
    ans = ans * tmp % MOD
print(ans)

相關文章