結果

問題 No.1036 Make One With GCD 2
ユーザー gew1fw
提出日時 2025-06-12 16:14:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 782 bytes
コンパイル時間 210 ms
コンパイル使用メモリ 82,796 KB
実行使用メモリ 181,824 KB
最終ジャッジ日時 2025-06-12 16:15:04
合計ジャッジ時間 18,680 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 34 TLE * 1 -- * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from math import gcd
from collections import defaultdict

def main():
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    
    total = 0
    prev_gcds = defaultdict(int)
    
    for num in a:
        curr_gcds = defaultdict(int)
        # Process each GCD from the previous step
        for g, cnt in prev_gcds.items():
            new_g = gcd(g, num)
            curr_gcds[new_g] += cnt
        # Add the subarray consisting of just the current number
        curr_gcds[num] += 1
        # Check if any GCD is 1 and add to total
        if 1 in curr_gcds:
            total += curr_gcds[1]
        # Update prev_gcds for the next iteration
        prev_gcds = curr_gcds
    
    print(total)

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