結果
問題 | No.917 Make One With GCD |
ユーザー |
![]() |
提出日時 | 2025-03-20 21:19:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 179 ms / 2,000 ms |
コード長 | 1,900 bytes |
コンパイル時間 | 305 ms |
コンパイル使用メモリ | 82,884 KB |
実行使用メモリ | 77,440 KB |
最終ジャッジ日時 | 2025-03-20 21:20:21 |
合計ジャッジ時間 | 3,519 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 32 |
ソースコード
def factorize(x):factors = {}if x == 1:return factorsi = 2while i * i <= x:while x % i == 0:factors[i] = factors.get(i, 0) + 1x = x // ii += 1if x > 1:factors[x] = 1return factorsdef generate_divisors(factors):if not factors:return [(1, 1)]primes = list(factors.keys())exponents = [factors[p] for p in primes]divisors = []current_exponents = [0] * len(primes)def backtrack(index):if index == len(primes):d = 1has_square = Falseprime_count = 0for i in range(len(primes)):p = primes[i]e = current_exponents[i]d *= p ** eif e >= 2:has_square = Trueif e >= 1:prime_count += 1mu = 0 if has_square else (-1) ** prime_countdivisors.append((d, mu))returnfor e in range(0, exponents[index] + 1):current_exponents[index] = ebacktrack(index + 1)backtrack(0)return divisorsdef main():import sysinput = sys.stdin.read().split()N = int(input[0])A = list(map(int, input[1:N+1]))divisors_dict = {}for x in A:factors = factorize(x)generated = generate_divisors(factors)for d, mu in generated:if d not in divisors_dict:divisors_dict[d] = musum_ans = 0for d in divisors_dict:mu = divisors_dict[d]if mu == 0:continuecnt = 0for a in A:if a % d == 0:cnt += 1if cnt == 0:continueterm = mu * ((1 << cnt) - 1)sum_ans += termprint(sum_ans)if __name__ == "__main__":main()