結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | tenten |
提出日時 | 2023-06-08 09:07:23 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,643 ms / 2,000 ms |
コード長 | 3,427 bytes |
コンパイル時間 | 2,931 ms |
コンパイル使用メモリ | 90,804 KB |
実行使用メモリ | 96,368 KB |
最終ジャッジ日時 | 2024-06-09 18:34:02 |
合計ジャッジ時間 | 31,287 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,036 ms
94,376 KB |
testcase_01 | AC | 615 ms
76,924 KB |
testcase_02 | AC | 602 ms
95,508 KB |
testcase_03 | AC | 285 ms
49,332 KB |
testcase_04 | AC | 352 ms
56,284 KB |
testcase_05 | AC | 45 ms
37,104 KB |
testcase_06 | AC | 46 ms
36,912 KB |
testcase_07 | AC | 373 ms
59,044 KB |
testcase_08 | AC | 348 ms
54,900 KB |
testcase_09 | AC | 714 ms
76,380 KB |
testcase_10 | AC | 684 ms
74,620 KB |
testcase_11 | AC | 711 ms
75,204 KB |
testcase_12 | AC | 689 ms
75,656 KB |
testcase_13 | AC | 850 ms
95,000 KB |
testcase_14 | AC | 896 ms
95,536 KB |
testcase_15 | AC | 844 ms
94,228 KB |
testcase_16 | AC | 832 ms
95,452 KB |
testcase_17 | AC | 843 ms
95,680 KB |
testcase_18 | AC | 77 ms
38,484 KB |
testcase_19 | AC | 89 ms
38,952 KB |
testcase_20 | AC | 94 ms
39,112 KB |
testcase_21 | AC | 94 ms
39,296 KB |
testcase_22 | AC | 816 ms
94,060 KB |
testcase_23 | AC | 668 ms
77,172 KB |
testcase_24 | AC | 846 ms
94,312 KB |
testcase_25 | AC | 782 ms
80,036 KB |
testcase_26 | AC | 800 ms
81,004 KB |
testcase_27 | AC | 44 ms
37,120 KB |
testcase_28 | AC | 45 ms
37,156 KB |
testcase_29 | AC | 44 ms
37,076 KB |
testcase_30 | AC | 43 ms
37,016 KB |
testcase_31 | AC | 46 ms
37,304 KB |
testcase_32 | AC | 47 ms
36,932 KB |
testcase_33 | AC | 43 ms
36,796 KB |
testcase_34 | AC | 47 ms
36,904 KB |
testcase_35 | AC | 46 ms
36,900 KB |
testcase_36 | AC | 44 ms
36,876 KB |
testcase_37 | AC | 46 ms
37,072 KB |
testcase_38 | AC | 837 ms
96,208 KB |
testcase_39 | AC | 1,643 ms
95,068 KB |
testcase_40 | AC | 663 ms
77,056 KB |
testcase_41 | AC | 1,279 ms
96,368 KB |
testcase_42 | AC | 1,273 ms
95,192 KB |
testcase_43 | AC | 1,258 ms
95,412 KB |
testcase_44 | AC | 1,327 ms
94,952 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); SegmentTree st = new SegmentTree(n); for (int i = 0; i < n; i++) { st.setValue(i, sc.nextLong()); } int right = 0; long ans = 0; for (int i = 0; i < n; i++) { right = Math.max(i, right); while (right < n && st.getValue(i, right + 1) > 1) { right++; } if (right >= n) { break; } ans += n - right; } System.out.println(ans); } static class SegmentTree { int size; long[] tree; public SegmentTree(int x) { size = 1; while (x >= size) { size <<= 1; } tree = new long[size * 2 - 1]; } public void setValue(int idx, long value) { int current = size - 1 + idx; tree[current] = value; calc((current - 1) / 2); } private void calc(int idx) { tree[idx] = getGCD(tree[idx * 2 + 1], tree[idx * 2 + 2]); if (idx > 0) { calc((idx - 1) / 2); } } public long getValue(int left, int right) { return getTreeValue(0, 0, size, left, right); } public long getTreeValue(int idx, int min, int max, int left, int right) { if (left >= max || right <= min) { return 0; } if (left <= min && max <= right) { return tree[idx]; } return getGCD(getTreeValue(idx * 2 + 1, min, (min + max) / 2, left, right), getTreeValue(idx * 2 + 2, (min + max) / 2, max, left, right)); } private long getGCD(long x, long y) { if (y == 0) { return x; } else if (x == 0) { return y; } else { return getGCD(y, x % y); } } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }