結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
|
提出日時 | 2022-03-27 23:35:34 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 722 ms / 2,000 ms |
コード長 | 1,190 bytes |
コンパイル時間 | 861 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 270,880 KB |
最終ジャッジ日時 | 2024-11-06 16:43:15 |
合計ジャッジ時間 | 19,241 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
from math import gcdclass SWAG:def __init__(self,f):self.st1 = []self.st2 = []self.rst1 = []self.rst2 = []self.f = fdef calc_all(self):calc = 0if self.st1:calc = self.f(calc,self.rst1[-1])if self.st2:calc = self.f(calc,self.rst2[-1])return calcdef pop_front(self):if self.st1 == []:self.st1 = self.st2[::-1]self.rst1 = self.st1[:]for i in range(1,len(self.st1)):self.rst1[i] = self.f(self.rst1[i-1],self.st1[i])self.st2 = []self.rst2 = []self.st1.pop()self.rst1.pop()def push_back(self,val):if self.st2 == []:self.rst2.append(val)else:self.rst2.append(self.f(self.rst2[-1],val))self.st2.append(val)n = int(input())A = list(map(int,input().split()))swag = SWAG(gcd)ans = 0now = 0for i,a in enumerate(A):while now < n and swag.calc_all() != 1:swag.push_back(A[now])now += 1if swag.calc_all() == 1:ans += n-now+1swag.pop_front()print(ans)