結果

問題 No.2075 GCD Subsequence
ユーザー lam6er
提出日時 2025-04-16 16:24:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,709 bytes
コンパイル時間 521 ms
コンパイル使用メモリ 81,996 KB
実行使用メモリ 133,664 KB
最終ジャッジ日時 2025-04-16 16:25:16
合計ジャッジ時間 19,901 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 WA * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin
from collections import defaultdict

MOD = 998244353

def main():
    max_a = 10**6
    spf = list(range(max_a + 1))
    for i in range(2, int(max_a**0.5) + 1):
        if spf[i] == i:
            for j in range(i*i, max_a+1, i):
                if spf[j] == j:
                    spf[j] = i

    def get_primes(x):
        if x == 1:
            return []
        primes = set()
        while x != 1:
            p = spf[x]
            primes.add(p)
            while x % p == 0:
                x //= p
        return sorted(primes)

    input = sys.stdin.read().split()
    n = int(input[0])
    a_list = list(map(int, input[1:n+1]))

    count = defaultdict(int)
    ans = 0

    for a in a_list:
        if a == 1:
            ans = (ans + 1) % MOD
            continue
        primes = get_primes(a)
        m = len(primes)
        if m == 0:
            ans = (ans + 1) % MOD
            continue

        subsets = []
        for p in primes:
            new_subsets = []
            for (d, bits) in subsets:
                new_d = d * p
                new_bits = bits + 1
                new_subsets.append((new_d, new_bits))
            new_subsets.append((p, 1))
            subsets += new_subsets

        sum_val = 0
        for d, bits in subsets:
            sign = 1 if bits % 2 == 1 else -1
            sum_val = (sum_val + count.get(d, 0) * sign) % MOD

        current = (sum_val + 1) % MOD
        ans = (ans + current) % MOD

        for d, bits in subsets:
            sign = 1 if bits % 2 == 1 else -1
            add_val = current * sign
            count[d] = (count[d] + add_val) % MOD

    print(ans % MOD)

if __name__ == "__main__":
    main()
0