結果

問題 No.1036 Make One With GCD 2
ユーザー Salmonize
提出日時 2020-04-25 12:02:19
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,455 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 76,520 KB
最終ジャッジ日時 2024-11-07 04:11:40
合計ジャッジ時間 8,519 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

class SWAG:
    def __init__(self, func=sum, ele=0):
        self.back = list()
        self.back_acc = list()
        self.front = list()
        self.front_acc = list()
        self.func = func
        self.ele = ele

    def length(self):
        return len(self.back) + len(self.front)

    def _displace(self):
        y = self.ele
        while self.back:
            x = self.back.pop()
            y = self.func(x, y)
            self.front.append(x)
            self.front_acc.append(y)
        self.back_acc.clear()
        return

    def push(self, x):
        self.back.append(x)
        y = self.ele if not self.back_acc else self.back_acc[-1]
        self.back_acc.append(self.func(y, x))
        return

    def pop(self):
        if not self.front:
            self._displace()
            if not self.front:
                raise IndexError
        self.front_acc.pop()
        return self.front.pop()

    def fold(self):
        x = self.ele if not self.back_acc else self.back_acc[-1]
        y = self.ele if not self.front_acc else self.front_acc[-1]
        return self.func(x, y)

def gcd(x, y):
  while y:
    x, y = y, x%y
  return x

N = int(input())
A = list(map(int, input().split()))
ans = (N+1)*N//2
que = SWAG(gcd)
ridx = 0
for i in range(N):
  if ridx < i:
    ridx = i
  while ridx < N and gcd(que.fold(), A[ridx]) > 1:
    que.push(A[ridx])
    ridx += 1
  ans -= que.length()
  if que.length():
    que.pop()
print(ans)
0