結果
| 問題 |
No.917 Make One With GCD
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:45:37 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 88 ms / 2,000 ms |
| コード長 | 2,397 bytes |
| コンパイル時間 | 462 ms |
| コンパイル使用メモリ | 82,172 KB |
| 実行使用メモリ | 77,380 KB |
| 最終ジャッジ日時 | 2025-03-20 20:45:41 |
| 合計ジャッジ時間 | 3,374 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
ソースコード
def factorize(n):
factors = {}
while n % 2 == 0:
factors[2] = factors.get(2, 0) + 1
n = n // 2
i = 3
while i * i <= n:
while n % i == 0:
factors[i] = factors.get(i, 0) + 1
n = n // i
i += 2
if n > 2:
factors[n] = 1
return factors
def generate_divisors_with_mu(factors):
primes = list(factors.keys())
divisors = [(1, {}, False)] # (value, exponents dict, has_squared)
result = []
for p in primes:
max_e = factors[p]
temp = []
for d in divisors:
current_value, current_exponents, current_has_squared = d
# Append e=0 (same as current divisor)
temp.append((current_value, current_exponents.copy(), current_has_squared))
for e in range(1, max_e + 1):
new_value = current_value * (p ** e)
new_exponents = current_exponents.copy()
new_exponents[p] = e
new_has_squared = current_has_squared or (e >= 2)
temp.append((new_value, new_exponents, new_has_squared))
divisors = temp
for d in divisors:
value, exponents, has_squared = d
if has_squared:
mu = 0
else:
k = len(exponents)
mu = (-1) ** k
result.append((value, mu))
# Remove duplicate divisors, keeping the first occurrence (mu should be consistent)
unique = {}
for val, mu in result:
if val not in unique:
unique[val] = mu
return list(unique.items())
def main():
import sys
input = sys.stdin.read().split()
N = int(input[0])
A = list(map(int, input[1:N+1]))
D = {}
for a in A:
if a == 0:
continue # handle zero if needed, but problem says 1 <= A_i
factors = factorize(a)
if not factors and a == 1:
factors = {} # 1's factors are none
div_mu = generate_divisors_with_mu(factors)
for d, mu in div_mu:
if d not in D:
D[d] = mu
sum_total = 0
for d, mu in D.items():
count = 0
for x in A:
if d == 0:
continue
if x % d == 0:
count += 1
if count > 0:
sum_total += mu * ((1 << count) - 1)
print(sum_total)
if __name__ == '__main__':
main()
lam6er