結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2023-06-08 09:05:19 |
言語 | Java (openjdk 23) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,426 bytes |
コンパイル時間 | 3,350 ms |
コンパイル使用メモリ | 92,908 KB |
実行使用メモリ | 96,136 KB |
最終ジャッジ日時 | 2024-12-30 15:50:55 |
合計ジャッジ時間 | 39,372 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 39 RE * 2 |
ソースコード
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();}}