結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-04-25 16:18:25
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,141 bytes
コンパイル時間 883 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 190,400 KB
最終ジャッジ日時 2024-04-24 21:46:46
合計ジャッジ時間 34,427 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,451 ms
186,552 KB
testcase_01 AC 516 ms
182,380 KB
testcase_02 AC 854 ms
157,708 KB
testcase_03 AC 169 ms
104,064 KB
testcase_04 AC 232 ms
129,152 KB
testcase_05 AC 41 ms
51,968 KB
testcase_06 AC 42 ms
52,096 KB
testcase_07 AC 232 ms
105,760 KB
testcase_08 AC 205 ms
106,112 KB
testcase_09 AC 713 ms
181,532 KB
testcase_10 AC 667 ms
169,364 KB
testcase_11 AC 727 ms
182,072 KB
testcase_12 AC 672 ms
169,636 KB
testcase_13 AC 880 ms
176,680 KB
testcase_14 AC 885 ms
186,888 KB
testcase_15 AC 857 ms
173,792 KB
testcase_16 AC 843 ms
174,388 KB
testcase_17 AC 856 ms
176,512 KB
testcase_18 AC 79 ms
72,832 KB
testcase_19 AC 83 ms
76,544 KB
testcase_20 AC 85 ms
76,928 KB
testcase_21 AC 83 ms
76,544 KB
testcase_22 AC 828 ms
173,028 KB
testcase_23 AC 638 ms
140,544 KB
testcase_24 AC 886 ms
176,348 KB
testcase_25 AC 789 ms
161,464 KB
testcase_26 AC 815 ms
169,884 KB
testcase_27 AC 42 ms
52,224 KB
testcase_28 AC 41 ms
52,224 KB
testcase_29 AC 41 ms
52,096 KB
testcase_30 AC 42 ms
52,352 KB
testcase_31 AC 47 ms
58,368 KB
testcase_32 AC 50 ms
58,624 KB
testcase_33 AC 42 ms
52,096 KB
testcase_34 AC 47 ms
58,112 KB
testcase_35 AC 43 ms
52,096 KB
testcase_36 AC 42 ms
51,968 KB
testcase_37 AC 41 ms
51,968 KB
testcase_38 AC 1,672 ms
190,136 KB
testcase_39 AC 1,755 ms
189,496 KB
testcase_40 AC 618 ms
140,544 KB
testcase_41 TLE -
testcase_42 AC 1,971 ms
189,380 KB
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


class SegmentTree:
    """一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する
    build(array)   := arrayを初期値とするセグメント木を構築する O(N)
    update(i, val) := i番目の要素をvalに変更する
    get_val(l, r)  := 区間[l, r)に対する二項演算の畳み込みの結果を返す
    """
    def __init__(self, n, op, e):
        """要素数、二項演算、単位元を引数として渡す
        例) 区間最小値 SegmentTree(n, min, 10 ** 18)
            区間和     SegmentTree(n, lambda a, b : a + b, 0)
        """
        self.n = n
        self.op = op
        self.e = e
        self.size = 2 ** ((n - 1).bit_length())
        self.node = [self.e] * (2 * self.size)

    def build(self, array):
        """arrayを初期値とするセグメント木を構築する"""
        for i, val in enumerate(array):
            self.node[self.size + i] = val
        for i in range(self.size - 1, 0, -1):
            self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])

    def update(self, i, val):
        """i番目の要素をvalに変更する"""
        i += self.size
        self.node[i] = val
        while i > 1:
            i >>= 1
            self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])

    def get_val(self, l, r):
        """[l, r)の畳み込みの結果を返す"""
        l, r = l + self.size, r + self.size
        res_l, res_r = self.e, self.e
        while l < r:
            if l & 1:
                res_l = self.op(res_l, self.node[l])
                l += 1
            if r & 1:
                r -= 1
                res_r = self.op(self.node[r], res_r)
            l, r = l >> 1, r >> 1
        return self.op(res_l, res_r)


import sys
input = sys.stdin.readline


n = int(input())
a = list(map(int, input().split()))

st = SegmentTree(n, gcd, 0)
st.build(a)
r = 1
ans = 0
for l in range(n):
    r = max(l + 1, r)
    while r <= n and st.get_val(l, r) != 1:
        r += 1
    ans += n - r + 1
    
print(ans)
0