結果

問題 No.1036 Make One With GCD 2
ユーザー tentententen
提出日時 2023-06-08 09:05:19
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,426 bytes
コンパイル時間 4,544 ms
コンパイル使用メモリ 85,388 KB
実行使用メモリ 109,060 KB
最終ジャッジ日時 2023-08-29 00:04:54
合計ジャッジ時間 36,995 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,054 ms
107,696 KB
testcase_01 AC 631 ms
88,248 KB
testcase_02 AC 627 ms
106,836 KB
testcase_03 AC 307 ms
60,628 KB
testcase_04 AC 383 ms
67,980 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 403 ms
68,664 KB
testcase_08 AC 374 ms
64,232 KB
testcase_09 AC 765 ms
89,324 KB
testcase_10 AC 741 ms
86,568 KB
testcase_11 AC 788 ms
87,204 KB
testcase_12 AC 751 ms
87,100 KB
testcase_13 AC 948 ms
105,912 KB
testcase_14 AC 928 ms
105,744 KB
testcase_15 AC 877 ms
105,212 KB
testcase_16 AC 891 ms
105,400 KB
testcase_17 AC 911 ms
106,040 KB
testcase_18 AC 73 ms
51,836 KB
testcase_19 AC 85 ms
52,252 KB
testcase_20 AC 96 ms
52,320 KB
testcase_21 AC 95 ms
52,056 KB
testcase_22 AC 875 ms
103,196 KB
testcase_23 AC 713 ms
87,820 KB
testcase_24 AC 898 ms
105,412 KB
testcase_25 AC 843 ms
90,864 KB
testcase_26 AC 865 ms
93,736 KB
testcase_27 AC 43 ms
49,392 KB
testcase_28 AC 44 ms
49,372 KB
testcase_29 AC 42 ms
49,368 KB
testcase_30 AC 44 ms
49,612 KB
testcase_31 AC 49 ms
49,940 KB
testcase_32 AC 48 ms
49,520 KB
testcase_33 AC 43 ms
49,612 KB
testcase_34 AC 49 ms
49,452 KB
testcase_35 AC 42 ms
49,396 KB
testcase_36 AC 43 ms
49,356 KB
testcase_37 AC 43 ms
49,364 KB
testcase_38 AC 879 ms
106,372 KB
testcase_39 AC 1,739 ms
109,060 KB
testcase_40 AC 720 ms
88,604 KB
testcase_41 AC 1,380 ms
106,128 KB
testcase_42 AC 1,370 ms
107,784 KB
testcase_43 AC 1,337 ms
106,480 KB
testcase_44 AC 1,397 ms
106,580 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