結果

問題 No.1036 Make One With GCD 2
ユーザー tentententen
提出日時 2023-06-08 09:07:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,747 ms / 2,000 ms
コード長 3,427 bytes
コンパイル時間 5,471 ms
コンパイル使用メモリ 80,932 KB
実行使用メモリ 109,308 KB
最終ジャッジ日時 2023-08-29 00:06:06
合計ジャッジ時間 31,821 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 930 ms
109,020 KB
testcase_01 AC 562 ms
88,556 KB
testcase_02 AC 555 ms
108,648 KB
testcase_03 AC 274 ms
58,652 KB
testcase_04 AC 335 ms
68,160 KB
testcase_05 AC 37 ms
49,640 KB
testcase_06 AC 36 ms
49,396 KB
testcase_07 AC 376 ms
68,812 KB
testcase_08 AC 336 ms
66,932 KB
testcase_09 AC 674 ms
87,156 KB
testcase_10 AC 660 ms
86,020 KB
testcase_11 AC 687 ms
87,180 KB
testcase_12 AC 637 ms
87,628 KB
testcase_13 AC 789 ms
109,016 KB
testcase_14 AC 808 ms
108,964 KB
testcase_15 AC 781 ms
108,760 KB
testcase_16 AC 787 ms
109,308 KB
testcase_17 AC 790 ms
108,240 KB
testcase_18 AC 69 ms
51,464 KB
testcase_19 AC 68 ms
52,024 KB
testcase_20 AC 89 ms
52,452 KB
testcase_21 AC 84 ms
52,572 KB
testcase_22 AC 817 ms
109,096 KB
testcase_23 AC 653 ms
88,336 KB
testcase_24 AC 807 ms
108,708 KB
testcase_25 AC 757 ms
91,148 KB
testcase_26 AC 777 ms
94,084 KB
testcase_27 AC 37 ms
49,492 KB
testcase_28 AC 38 ms
49,616 KB
testcase_29 AC 39 ms
49,408 KB
testcase_30 AC 36 ms
49,496 KB
testcase_31 AC 41 ms
49,456 KB
testcase_32 AC 42 ms
49,732 KB
testcase_33 AC 38 ms
49,532 KB
testcase_34 AC 41 ms
49,536 KB
testcase_35 AC 37 ms
49,452 KB
testcase_36 AC 37 ms
49,512 KB
testcase_37 AC 37 ms
49,608 KB
testcase_38 AC 829 ms
109,040 KB
testcase_39 AC 1,747 ms
108,644 KB
testcase_40 AC 724 ms
88,572 KB
testcase_41 AC 1,401 ms
108,808 KB
testcase_42 AC 1,365 ms
109,128 KB
testcase_43 AC 1,235 ms
108,924 KB
testcase_44 AC 1,267 ms
108,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0