結果

問題 No.1036 Make One With GCD 2
ユーザー nehan_der_thal
提出日時 2020-04-26 17:32:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 759 ms / 2,000 ms
コード長 768 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 209,768 KB
最終ジャッジ日時 2024-09-16 14:13:50
合計ジャッジ時間 18,121 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys;input=sys.stdin.readline
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    while a % b:
        a, b = b, a%b
    return b
n = int(input())
X = list(map(int, input().split()))
st_f = [(0, 0), (X[0], X[0])]
st_b = [(0, 0)]
r = 0
j = 1
for i in range(n):
    while j<=n:
        if gcd(st_f[-1][1], st_b[-1][1]) == 1:
            break
        if j == n:
            j += 1
            break
#        print(j)
        st_f.append(
                (X[j], gcd(st_f[-1][1], X[j]))
                )
        j += 1
    if len(st_b) == 1:
        while len(st_f) > 1:
            x, y = st_f.pop()
            st_b.append(
                    (x, gcd(st_b[-1][1], x))
                    )
    st_b.pop()
    r += n-j+1
print(r)
0