結果

問題 No.1036 Make One With GCD 2
ユーザー terasaterasa
提出日時 2022-05-28 18:27:18
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,686 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 81,788 KB
実行使用メモリ 229,712 KB
最終ジャッジ日時 2023-10-20 23:30:57
合計ジャッジ時間 24,860 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 568 ms
191,164 KB
testcase_02 WA -
testcase_03 AC 76 ms
96,596 KB
testcase_04 AC 109 ms
112,156 KB
testcase_05 AC 44 ms
55,676 KB
testcase_06 AC 45 ms
55,676 KB
testcase_07 AC 227 ms
125,372 KB
testcase_08 AC 196 ms
110,960 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 58 ms
66,260 KB
testcase_19 AC 73 ms
72,960 KB
testcase_20 AC 80 ms
76,640 KB
testcase_21 AC 79 ms
76,608 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 45 ms
55,676 KB
testcase_28 AC 45 ms
55,612 KB
testcase_29 WA -
testcase_30 AC 44 ms
55,676 KB
testcase_31 AC 45 ms
55,676 KB
testcase_32 AC 47 ms
57,724 KB
testcase_33 AC 45 ms
55,676 KB
testcase_34 AC 46 ms
55,676 KB
testcase_35 WA -
testcase_36 AC 45 ms
55,676 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict, Counter
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


class SegTree:
    def __init__(self, N, func, e):
        self.N = N
        self.func = func
        self.X = [e] * (N << 1)
        self.e = e

    def build(self, seq):
        for i in range(self.N):
            self.X[self.N + i] = seq[i]
        for i in range(self.N)[::-1]:
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def add(self, i, x):
        i += self.N
        self.X[i] += x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def update(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def query(self, L, R):
        L += self.N
        R += self.N
        vL = self.X[L]
        vR = self.X[R - 1]
        while L < R:
            if L & 1:
                vL = self.func(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)


N = int(input())
A = list(map(int, input().split()))

B = []
tmp = []
cnt1 = 0
for a in A:
    if a == 1:
        cnt1 += 1
        if len(tmp) > 0:
            B.append(tmp)
            tmp = []
    else:
        tmp.append(a)
if len(tmp) > 0:
    B.append(tmp)

ans = (1 + N) * N // 2 - (N - cnt1)
for b in B:
    st = SegTree(len(b), math.gcd, 1)
    st.build(b)

    def solve(b):
        global ans
        l = 0
        r = 1
        while r <= len(b):
            while st.query(l, r) > 1:
                r += 1
                if r > len(b):
                    d = len(b) - l - 1
                    ans -= d * (d - 1) // 2
                    return
            d = r - l - 1
            ans -= d * (d - 1) // 2
            while st.query(l, r) == 1:
                l += 1
    solve(b)
print(ans)
0