結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-04-25 00:00:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,830 ms / 2,000 ms
コード長 1,810 bytes
コンパイル時間 538 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 269,500 KB
最終ジャッジ日時 2024-04-24 18:25:14
合計ジャッジ時間 38,159 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 526 ms
264,644 KB
testcase_01 AC 454 ms
189,360 KB
testcase_02 AC 483 ms
233,848 KB
testcase_03 AC 165 ms
125,312 KB
testcase_04 AC 258 ms
172,160 KB
testcase_05 AC 36 ms
51,968 KB
testcase_06 AC 36 ms
51,968 KB
testcase_07 AC 240 ms
133,220 KB
testcase_08 AC 216 ms
129,792 KB
testcase_09 AC 1,693 ms
249,312 KB
testcase_10 AC 1,628 ms
232,020 KB
testcase_11 AC 1,668 ms
251,668 KB
testcase_12 AC 1,512 ms
233,180 KB
testcase_13 AC 1,772 ms
251,972 KB
testcase_14 AC 1,830 ms
262,908 KB
testcase_15 AC 1,698 ms
244,260 KB
testcase_16 AC 1,683 ms
246,080 KB
testcase_17 AC 1,752 ms
250,508 KB
testcase_18 AC 67 ms
69,504 KB
testcase_19 AC 69 ms
69,760 KB
testcase_20 AC 71 ms
70,400 KB
testcase_21 AC 68 ms
70,784 KB
testcase_22 AC 1,705 ms
242,872 KB
testcase_23 AC 1,232 ms
189,952 KB
testcase_24 AC 1,757 ms
249,356 KB
testcase_25 AC 1,573 ms
227,300 KB
testcase_26 AC 1,686 ms
238,828 KB
testcase_27 AC 37 ms
51,968 KB
testcase_28 AC 38 ms
52,352 KB
testcase_29 AC 37 ms
52,224 KB
testcase_30 AC 36 ms
51,840 KB
testcase_31 AC 44 ms
60,160 KB
testcase_32 AC 46 ms
60,544 KB
testcase_33 AC 36 ms
52,224 KB
testcase_34 AC 44 ms
60,032 KB
testcase_35 AC 37 ms
52,224 KB
testcase_36 AC 37 ms
51,840 KB
testcase_37 AC 36 ms
52,224 KB
testcase_38 AC 467 ms
268,596 KB
testcase_39 AC 545 ms
269,116 KB
testcase_40 AC 1,273 ms
189,952 KB
testcase_41 AC 1,509 ms
269,500 KB
testcase_42 AC 1,461 ms
268,980 KB
testcase_43 AC 1,284 ms
268,736 KB
testcase_44 AC 1,407 ms
268,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a: int, b: int) -> int:
    """a, bの最大公約数(greatest common divisor: GCD)を求める
    計算量: O(log(min(a, b)))
    """
    if b == 0:
        return a
    return gcd(b, a%b)


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_table = [0] * (n + 1)
        for i in range(2, n + 1):
            self.log_table[i] = self.log_table[i//2] + 1

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

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

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

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

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