aacord’s memo

abcを中心にpythonで解いた問題のメモ、整理をしています。緑になった。

arc 034 C - 約数かつ倍数 (python)

b より大きく、a 以下の数字の積の約数の数の個数を求めればいいのだけれど、b,a が大きすぎてそのまま掛け算をするとオーバーフローする。
なので一つずつ素因数分解して、個数分だけ素数をリストに入れていって、あとで collections.Counter で数えた。

a,b = map(int,input().split())
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

ans = 1
mod = 10**9+7
if a == b:
  print(1)
  exit()

from collections import Counter
chk = 1
jj = []
for i in range(a,b,-1):
  c = factorization(i)
  for j,k in c:
    for _ in range(k):
      jj.append(j)

c = Counter(jj)
for k in c.values():
  ans *= k+1
  ans %= mod
    
print(ans)