結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-05-02 05:02:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 693 ms / 2,000 ms
コード長 1,564 bytes
コンパイル時間 1,862 ms
コンパイル使用メモリ 86,576 KB
実行使用メモリ 262,312 KB
最終ジャッジ日時 2023-10-14 20:28:06
合計ジャッジ時間 21,443 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 693 ms
259,560 KB
testcase_01 AC 404 ms
227,856 KB
testcase_02 AC 504 ms
225,452 KB
testcase_03 AC 162 ms
128,504 KB
testcase_04 AC 234 ms
162,888 KB
testcase_05 AC 67 ms
70,896 KB
testcase_06 AC 67 ms
70,752 KB
testcase_07 AC 258 ms
136,792 KB
testcase_08 AC 231 ms
122,816 KB
testcase_09 AC 499 ms
249,824 KB
testcase_10 AC 462 ms
234,752 KB
testcase_11 AC 485 ms
219,648 KB
testcase_12 AC 474 ms
235,808 KB
testcase_13 AC 653 ms
246,848 KB
testcase_14 AC 631 ms
257,116 KB
testcase_15 AC 598 ms
238,828 KB
testcase_16 AC 594 ms
240,904 KB
testcase_17 AC 624 ms
245,020 KB
testcase_18 AC 85 ms
76,688 KB
testcase_19 AC 89 ms
76,540 KB
testcase_20 AC 86 ms
76,844 KB
testcase_21 AC 89 ms
77,012 KB
testcase_22 AC 576 ms
237,196 KB
testcase_23 AC 448 ms
182,104 KB
testcase_24 AC 605 ms
242,488 KB
testcase_25 AC 541 ms
212,976 KB
testcase_26 AC 563 ms
235,072 KB
testcase_27 AC 70 ms
70,868 KB
testcase_28 AC 66 ms
71,144 KB
testcase_29 AC 68 ms
71,032 KB
testcase_30 AC 68 ms
71,040 KB
testcase_31 AC 75 ms
75,524 KB
testcase_32 AC 75 ms
75,604 KB
testcase_33 AC 70 ms
71,016 KB
testcase_34 AC 74 ms
75,904 KB
testcase_35 AC 66 ms
70,668 KB
testcase_36 AC 69 ms
71,016 KB
testcase_37 AC 69 ms
70,652 KB
testcase_38 AC 666 ms
262,140 KB
testcase_39 AC 491 ms
262,220 KB
testcase_40 AC 433 ms
182,064 KB
testcase_41 AC 489 ms
262,060 KB
testcase_42 AC 516 ms
262,072 KB
testcase_43 AC 472 ms
262,312 KB
testcase_44 AC 484 ms
262,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd


class SparseTable:
    """区間取得クエリをO(1)で答えるデータ構造をO(NlogN)で構築する
    query(l, r): 区間[l, r)に対するクエリに答える
    """
    def __init__(self, array):
        n = len(array)
        self.row_size = n.bit_length()

        # log_tableを構築する
        # log_table = [0, 0, 1, 1, 2, 2, 2, 2, ...]
        self.log_tbl = [0] * (n + 1)
        for i in range(2, n + 1):
            self.log_tbl[i] = self.log_tbl[i // 2] + 1

        # sparse_tableを構築する
        self.sp_tbl = [0] * (n * self.row_size)
        for i in range(n):
            self.sp_tbl[i] = array[i]
        for row in range(1, self.row_size):
            for i in range(n - (1 << row) + 1):
                self.sp_tbl[row * n + i] = \
                self._merge(self.sp_tbl[(row - 1) * n + i],
                            self.sp_tbl[(row - 1) * n + i + (1 << row - 1)])

    def _merge(self, num1, num2):
        """冪等律と結合律を満たす演算を行う"""
        return gcd(num1, num2)

    def query(self, l, r):
        """区間[l, r)に対するクエリに答える"""
        row = self.log_tbl[r - l]
        return self._merge(self.sp_tbl[row * n + l],
                           self.sp_tbl[row * n + r - (1 << row)])


import sys
input = sys.stdin.readline


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

sp = SparseTable(a)
r = 1
ans = 0
for l in range(n):
    r = max(l + 1, r)
    while r <= n and sp.query(l, r) != 1:
        r += 1
    ans += n - r + 1
print(ans)
0