結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | LyricalMaestro |
提出日時 | 2024-01-24 03:30:24 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,976 ms / 2,000 ms |
コード長 | 2,480 bytes |
コンパイル時間 | 288 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 336,836 KB |
最終ジャッジ日時 | 2024-09-28 06:59:33 |
合計ジャッジ時間 | 45,648 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,404 ms
302,092 KB |
testcase_01 | AC | 1,088 ms
279,492 KB |
testcase_02 | AC | 1,090 ms
306,384 KB |
testcase_03 | AC | 333 ms
160,776 KB |
testcase_04 | AC | 580 ms
243,196 KB |
testcase_05 | AC | 36 ms
52,352 KB |
testcase_06 | AC | 34 ms
52,676 KB |
testcase_07 | AC | 414 ms
186,816 KB |
testcase_08 | AC | 341 ms
158,836 KB |
testcase_09 | AC | 1,508 ms
274,852 KB |
testcase_10 | AC | 1,456 ms
335,220 KB |
testcase_11 | AC | 1,596 ms
278,616 KB |
testcase_12 | AC | 1,474 ms
336,836 KB |
testcase_13 | AC | 1,882 ms
292,172 KB |
testcase_14 | AC | 1,898 ms
296,004 KB |
testcase_15 | AC | 1,710 ms
283,112 KB |
testcase_16 | AC | 1,728 ms
284,260 KB |
testcase_17 | AC | 1,841 ms
289,712 KB |
testcase_18 | AC | 55 ms
66,432 KB |
testcase_19 | AC | 55 ms
67,968 KB |
testcase_20 | AC | 61 ms
70,144 KB |
testcase_21 | AC | 58 ms
70,272 KB |
testcase_22 | AC | 1,675 ms
280,568 KB |
testcase_23 | AC | 1,326 ms
263,636 KB |
testcase_24 | AC | 1,750 ms
287,056 KB |
testcase_25 | AC | 1,606 ms
270,076 KB |
testcase_26 | AC | 1,706 ms
276,744 KB |
testcase_27 | AC | 37 ms
52,352 KB |
testcase_28 | AC | 36 ms
52,864 KB |
testcase_29 | AC | 37 ms
52,480 KB |
testcase_30 | AC | 37 ms
52,608 KB |
testcase_31 | AC | 44 ms
60,416 KB |
testcase_32 | AC | 44 ms
60,416 KB |
testcase_33 | AC | 35 ms
52,480 KB |
testcase_34 | AC | 44 ms
60,288 KB |
testcase_35 | AC | 37 ms
52,736 KB |
testcase_36 | AC | 37 ms
52,736 KB |
testcase_37 | AC | 37 ms
52,736 KB |
testcase_38 | AC | 1,043 ms
303,728 KB |
testcase_39 | AC | 1,976 ms
303,072 KB |
testcase_40 | AC | 1,302 ms
264,492 KB |
testcase_41 | AC | 1,932 ms
303,352 KB |
testcase_42 | AC | 1,924 ms
303,364 KB |
testcase_43 | AC | 1,723 ms
303,092 KB |
testcase_44 | AC | 1,842 ms
302,976 KB |
ソースコード
## https://yukicoder.me/problems/no/1036 def calc_gcd(A, B): """ 正の整数A, Bの最大公約数を計算する """ a = max(A, B) b = min(A, B) while a % b > 0: c = a % b a = b b = c return b class SparseTable: def __init__(self, A): self.N = len(A) k = 0 while (1 << k) < self.N: k += 1 self.max_k = k self.table = [[] for _ in range(self.N)] for k in range(self.max_k + 1): for i in range(self.N): if k == 0: self.table[i].append(A[i]) else: if i + (1 << k) - 1 < self.N: if len(self.table[i]) < k: continue if len(self.table[i + (1 << (k - 1))]) < k: continue a = self.table[i][k - 1] b = self.table[i + (1 << (k - 1))][k - 1] self.table[i].append(calc_gcd(a, b)) def query(self, l, r): length = r - l + 1 if length == 1: return self.table[l][0] k = 0 while (1 << k) < length: k += 1 k -= 1 a = self.table[l][k] b = self.table[r - ((1 << k) - 1)][k] return calc_gcd(a, b) def main(): N = int(input()) A = list(map(int, input().split())) st = SparseTable(A) answer = 0 left = 0 right = 0 a = A[0] while left < N and a == 1: answer += N - left left += 1 right += 1 if left < N: a = A[left] else: a = 0 while left < N: while right < N - 1 and calc_gcd(a, A[right + 1]) > 1: a = calc_gcd(a, A[right + 1]) right += 1 if right < N - 1: answer += N - (right + 1) if left < right: left += 1 a = st.query(left, right) else: left += 1 right += 1 a = A[left] while left < N and a == 1: answer += N - left left += 1 right += 1 if left < N: a = A[left] else: a = 0 else: break print(answer) if __name__ == "__main__": main()