結果

問題 No.1036 Make One With GCD 2
ユーザー gew1fw
提出日時 2025-06-12 16:43:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,950 ms / 2,000 ms
コード長 971 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 181,748 KB
最終ジャッジ日時 2025-06-12 16:44:04
合計ジャッジ時間 17,662 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

def main():
    n = int(sys.stdin.readline())
    a = list(map(int, sys.stdin.readline().split()))
    total = 0
    prev_gcds = []
    for num in a:
        curr_gcds = []
        # Process previous GCDs
        for g, cnt in prev_gcds:
            new_g = math.gcd(g, num)
            if curr_gcds and curr_gcds[-1][0] == new_g:
                curr_gcds[-1] = (new_g, curr_gcds[-1][1] + cnt)
            else:
                curr_gcds.append((new_g, cnt))
        # Add the current number as a new subarray
        new_g = num
        if curr_gcds and curr_gcds[-1][0] == new_g:
            curr_gcds[-1] = (new_g, curr_gcds[-1][1] + 1)
        else:
            curr_gcds.append((new_g, 1))
        # Update total with counts of GCD 1
        for g, cnt in curr_gcds:
            if g == 1:
                total += cnt
        # Prepare for next iteration
        prev_gcds = curr_gcds
    print(total)

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