結果

問題 No.1036 Make One With GCD 2
ユーザー ks2mks2m
提出日時 2020-04-24 22:41:01
言語 Java19
(openjdk 21)
結果
AC  
実行時間 803 ms / 2,000 ms
コード長 980 bytes
コンパイル時間 2,431 ms
コンパイル使用メモリ 73,784 KB
実行使用メモリ 112,884 KB
最終ジャッジ日時 2023-10-14 19:30:09
合計ジャッジ時間 23,103 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 403 ms
110,736 KB
testcase_01 AC 435 ms
93,408 KB
testcase_02 AC 408 ms
111,756 KB
testcase_03 AC 221 ms
64,524 KB
testcase_04 AC 296 ms
78,376 KB
testcase_05 AC 43 ms
49,268 KB
testcase_06 AC 43 ms
49,812 KB
testcase_07 AC 327 ms
73,680 KB
testcase_08 AC 305 ms
69,052 KB
testcase_09 AC 615 ms
93,580 KB
testcase_10 AC 557 ms
98,888 KB
testcase_11 AC 602 ms
93,596 KB
testcase_12 AC 588 ms
94,068 KB
testcase_13 AC 787 ms
111,152 KB
testcase_14 AC 786 ms
111,872 KB
testcase_15 AC 784 ms
106,844 KB
testcase_16 AC 803 ms
107,960 KB
testcase_17 AC 770 ms
111,284 KB
testcase_18 AC 55 ms
50,868 KB
testcase_19 AC 59 ms
50,364 KB
testcase_20 AC 83 ms
52,480 KB
testcase_21 AC 79 ms
52,416 KB
testcase_22 AC 776 ms
107,008 KB
testcase_23 AC 643 ms
89,932 KB
testcase_24 AC 769 ms
111,016 KB
testcase_25 AC 727 ms
95,792 KB
testcase_26 AC 762 ms
96,152 KB
testcase_27 AC 43 ms
49,372 KB
testcase_28 AC 43 ms
49,348 KB
testcase_29 AC 43 ms
49,368 KB
testcase_30 AC 43 ms
49,404 KB
testcase_31 AC 46 ms
49,204 KB
testcase_32 AC 46 ms
49,516 KB
testcase_33 AC 43 ms
49,296 KB
testcase_34 AC 47 ms
49,304 KB
testcase_35 AC 43 ms
49,504 KB
testcase_36 AC 44 ms
49,192 KB
testcase_37 AC 43 ms
47,348 KB
testcase_38 AC 416 ms
112,884 KB
testcase_39 AC 412 ms
112,072 KB
testcase_40 AC 655 ms
92,036 KB
testcase_41 AC 454 ms
112,816 KB
testcase_42 AC 443 ms
112,720 KB
testcase_43 AC 428 ms
112,768 KB
testcase_44 AC 427 ms
112,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] sa = br.readLine().split(" ");
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = Long.parseLong(sa[i]);
		}
		br.close();

		long ans = 0;
		long now = 0;
		int l = 0;
		for (int r = 0; r < n; r++) {
			now = gcd(now, a[r]);
			if (now == 1) {
				long now2 = a[r];
				for (int k = r; k >= l; k--) {
					long next = gcd(now2, a[k]);
					if (next == 1) {
						ans += (long) (n - r) * (k - l + 1);
						l = k + 1;
						if (l > r) {
							now = 0;
						} else {
							now = now2;
						}
						break;
					}
					now2 = next;
				}
			}
		}
		System.out.println(ans);
	}

	static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		} else {
			return gcd(b, a % b);
		}
	}
}
0