結果

問題 No.1036 Make One With GCD 2
ユーザー htensaihtensai
提出日時 2020-06-12 18:59:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,797 ms / 2,000 ms
コード長 2,542 bytes
コンパイル時間 2,371 ms
コンパイル使用メモリ 74,120 KB
実行使用メモリ 128,580 KB
最終ジャッジ日時 2023-10-14 20:33:27
合計ジャッジ時間 32,929 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,031 ms
128,452 KB
testcase_01 AC 622 ms
102,492 KB
testcase_02 AC 568 ms
128,064 KB
testcase_03 AC 271 ms
66,888 KB
testcase_04 AC 376 ms
80,304 KB
testcase_05 AC 44 ms
49,404 KB
testcase_06 AC 44 ms
49,288 KB
testcase_07 AC 399 ms
73,244 KB
testcase_08 AC 386 ms
71,000 KB
testcase_09 AC 797 ms
101,984 KB
testcase_10 AC 757 ms
101,524 KB
testcase_11 AC 775 ms
102,248 KB
testcase_12 AC 767 ms
101,196 KB
testcase_13 AC 983 ms
128,228 KB
testcase_14 AC 994 ms
128,580 KB
testcase_15 AC 960 ms
126,236 KB
testcase_16 AC 970 ms
124,020 KB
testcase_17 AC 985 ms
128,060 KB
testcase_18 AC 64 ms
51,604 KB
testcase_19 AC 69 ms
51,860 KB
testcase_20 AC 79 ms
52,260 KB
testcase_21 AC 87 ms
52,008 KB
testcase_22 AC 947 ms
126,084 KB
testcase_23 AC 735 ms
94,516 KB
testcase_24 AC 951 ms
125,764 KB
testcase_25 AC 907 ms
105,040 KB
testcase_26 AC 925 ms
104,236 KB
testcase_27 AC 44 ms
49,284 KB
testcase_28 AC 45 ms
49,268 KB
testcase_29 AC 44 ms
49,344 KB
testcase_30 AC 44 ms
49,388 KB
testcase_31 AC 49 ms
49,600 KB
testcase_32 AC 50 ms
49,332 KB
testcase_33 AC 45 ms
49,216 KB
testcase_34 AC 50 ms
49,288 KB
testcase_35 AC 45 ms
49,624 KB
testcase_36 AC 45 ms
49,288 KB
testcase_37 AC 45 ms
49,444 KB
testcase_38 AC 893 ms
127,980 KB
testcase_39 AC 1,797 ms
127,988 KB
testcase_40 AC 748 ms
94,612 KB
testcase_41 AC 1,338 ms
127,448 KB
testcase_42 AC 1,355 ms
127,532 KB
testcase_43 AC 1,278 ms
128,112 KB
testcase_44 AC 1,375 ms
128,340 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
	public static void main (String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] first = br.readLine().split(" ");
		int n = Integer.parseInt(first[0]);
		SegmentTree st = new SegmentTree(n);
		String[] second = br.readLine().split(" ", n);
		for (int i = 0; i < n; i++) {
		    st.set(i, Long.parseLong(second[i]));
		}
		st.updateAll();
		long total = 0;
		int idx = 0;
		for (int i = 0; i < n && idx < n; i++) {
		    idx = Math.max(idx, i);
		    while (idx < n && st.query(i, idx) > 1) {
		        idx++;
		    }
		    if (idx >= n) {
		        break;
		    }
		    total += n - idx;
		}
	    System.out.println(total);
	}
	
	static class Path implements Comparable<Path> {
	    int row;
	    int col;
	    int value;
	    int count;
	    
	    public Path(int row, int col, int value, int count) {
	        this.row = row;
	        this.col = col;
	        this.value = value;
	        this.count = count;
	    }
	    
	    public int compareTo(Path another) {
	        return value - another.value;
	    }
	}
}
class SegmentTree {
    int size;
    int base;
    long[] tree;
    
    public SegmentTree(int size) {
        this.size = size;
        base = 1;
        while (base < size) {
            base <<= 1;
        }
        tree = new long[base * 2 - 1];
    }
    
    public void set(int idx, long value) {
        tree[idx + base - 1] = value;
    }
    
    public void updateAll() {
        update(0);
    }
    
    public long update(int idx) {
        if (idx >= base - 1) {
            return tree[idx];
        }
        return tree[idx] = getGCD(update(2 * idx + 1), update(2 * idx + 2));
    }
    
    public long query(int min, int max) {
        return query(min, max, 0, 0, base);
    }
    
    public long query(int min, int max, int idx, int left, int right) {
        if (min >= right || max < left) {
            return 0;
        }
        if (min <= left && right - 1 <= max) {
            return tree[idx];
        }
        long x1 = query(min, max, 2 * idx + 1, left, (left + right) / 2);
        long x2 = query(min, max, 2 * idx + 2, (left + right) / 2, right);
        return getGCD(x1, x2);
    }
    
    private long getGCD(long x, long y) {
        if (x == 0) {
            return y;
        }
        if (y == 0) {
            return x;
        }
        if (x % y == 0) {
            return y;
        } else {
            return getGCD(y, x % y);
        }
    }
 }
0