結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | tenten |
提出日時 | 2023-06-08 09:05:19 |
言語 | Java21 (openjdk 21) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,426 bytes |
コンパイル時間 | 3,117 ms |
コンパイル使用メモリ | 91,728 KB |
実行使用メモリ | 108,680 KB |
最終ジャッジ日時 | 2024-06-09 18:33:03 |
合計ジャッジ時間 | 34,114 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,047 ms
108,612 KB |
testcase_01 | AC | 637 ms
88,092 KB |
testcase_02 | AC | 629 ms
107,560 KB |
testcase_03 | AC | 290 ms
60,476 KB |
testcase_04 | AC | 359 ms
67,476 KB |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | AC | 403 ms
68,916 KB |
testcase_08 | AC | 370 ms
65,316 KB |
testcase_09 | AC | 745 ms
86,920 KB |
testcase_10 | AC | 738 ms
86,692 KB |
testcase_11 | AC | 737 ms
87,648 KB |
testcase_12 | AC | 759 ms
86,440 KB |
testcase_13 | AC | 969 ms
108,620 KB |
testcase_14 | AC | 868 ms
108,628 KB |
testcase_15 | AC | 843 ms
108,464 KB |
testcase_16 | AC | 848 ms
108,612 KB |
testcase_17 | AC | 862 ms
108,424 KB |
testcase_18 | AC | 87 ms
51,604 KB |
testcase_19 | AC | 88 ms
52,036 KB |
testcase_20 | AC | 104 ms
52,040 KB |
testcase_21 | AC | 101 ms
52,008 KB |
testcase_22 | AC | 826 ms
108,432 KB |
testcase_23 | AC | 677 ms
88,124 KB |
testcase_24 | AC | 853 ms
108,228 KB |
testcase_25 | AC | 802 ms
92,816 KB |
testcase_26 | AC | 829 ms
93,164 KB |
testcase_27 | AC | 48 ms
50,228 KB |
testcase_28 | AC | 49 ms
50,476 KB |
testcase_29 | AC | 50 ms
50,420 KB |
testcase_30 | AC | 49 ms
50,388 KB |
testcase_31 | AC | 52 ms
50,076 KB |
testcase_32 | AC | 53 ms
50,604 KB |
testcase_33 | AC | 49 ms
50,552 KB |
testcase_34 | AC | 51 ms
50,176 KB |
testcase_35 | AC | 55 ms
50,416 KB |
testcase_36 | AC | 53 ms
50,568 KB |
testcase_37 | AC | 51 ms
50,496 KB |
testcase_38 | AC | 866 ms
108,448 KB |
testcase_39 | AC | 1,664 ms
108,640 KB |
testcase_40 | AC | 776 ms
88,288 KB |
testcase_41 | AC | 1,396 ms
107,656 KB |
testcase_42 | AC | 1,275 ms
108,516 KB |
testcase_43 | AC | 1,283 ms
108,680 KB |
testcase_44 | AC | 1,315 ms
108,256 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(); } }