結果
| 問題 | 
                            No.1036 Make One With GCD 2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Salmonize
                         | 
                    
| 提出日時 | 2020-04-25 16:46:01 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,279 ms / 2,000 ms | 
| コード長 | 1,456 bytes | 
| コンパイル時間 | 243 ms | 
| コンパイル使用メモリ | 82,304 KB | 
| 実行使用メモリ | 269,588 KB | 
| 最終ジャッジ日時 | 2024-09-16 13:45:54 | 
| 合計ジャッジ時間 | 23,097 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge6 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 41 | 
ソースコード
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) 
            
            
            
        
            
Salmonize