結果

問題 No.1884 Sequence
ユーザー ks2mks2m
提出日時 2022-03-25 23:14:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,029 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 2,894 ms
コンパイル使用メモリ 79,788 KB
実行使用メモリ 64,224 KB
最終ジャッジ日時 2024-04-22 08:09:21
合計ジャッジ時間 31,604 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
41,820 KB
testcase_01 AC 122 ms
41,560 KB
testcase_02 AC 105 ms
41,436 KB
testcase_03 AC 116 ms
41,500 KB
testcase_04 AC 116 ms
41,676 KB
testcase_05 AC 107 ms
41,764 KB
testcase_06 AC 106 ms
42,204 KB
testcase_07 AC 118 ms
41,864 KB
testcase_08 AC 107 ms
41,788 KB
testcase_09 AC 117 ms
41,196 KB
testcase_10 AC 970 ms
64,224 KB
testcase_11 AC 1,029 ms
63,788 KB
testcase_12 AC 626 ms
52,932 KB
testcase_13 AC 640 ms
54,168 KB
testcase_14 AC 522 ms
52,220 KB
testcase_15 AC 831 ms
58,212 KB
testcase_16 AC 920 ms
59,108 KB
testcase_17 AC 717 ms
58,836 KB
testcase_18 AC 732 ms
57,852 KB
testcase_19 AC 700 ms
53,280 KB
testcase_20 AC 739 ms
58,184 KB
testcase_21 AC 678 ms
55,696 KB
testcase_22 AC 780 ms
57,952 KB
testcase_23 AC 959 ms
59,968 KB
testcase_24 AC 870 ms
59,464 KB
testcase_25 AC 962 ms
59,104 KB
testcase_26 AC 950 ms
59,284 KB
testcase_27 AC 540 ms
54,412 KB
testcase_28 AC 605 ms
54,128 KB
testcase_29 AC 645 ms
54,636 KB
testcase_30 AC 813 ms
54,508 KB
testcase_31 AC 969 ms
55,080 KB
testcase_32 AC 904 ms
59,196 KB
testcase_33 AC 819 ms
58,956 KB
testcase_34 AC 833 ms
59,264 KB
testcase_35 AC 551 ms
54,312 KB
testcase_36 AC 846 ms
58,568 KB
testcase_37 AC 839 ms
58,296 KB
testcase_38 AC 829 ms
58,876 KB
testcase_39 AC 646 ms
54,616 KB
testcase_40 AC 650 ms
54,492 KB
testcase_41 AC 889 ms
59,108 KB
testcase_42 AC 910 ms
58,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] a = new long[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextLong();
		}
		sc.close();

		Arrays.sort(a);
		long z = 0;
		long mind = 1000000000000000000L;
		Set<Long> set = new HashSet<>();
		for (int i = 0; i < n; i++) {
			if (a[i] == 0) {
				z++;
			} else {
				if (i > 0 && a[i - 1] > 0) {
					set.add(a[i] - a[i - 1]);
					mind = Math.min(mind, a[i] - a[i - 1]);
				}
			}
		}
		for (long e : set) {
			mind = gcd(mind, e);
		}

		if (judge(n, a, z, mind)) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}

	static long gcd(long a, long b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	static boolean judge(int n, long[] a, long z, long mind) {
		for (int i = (int) z + 1; i < n; i++) {
			long d = a[i] - a[i - 1];
			if (mind == 0) {
				if (d > 0) {
					return false;
				}
			} else if (d % mind != 0 || d == 0) {
				return false;
			} else {
				z -= d / mind - 1;
				if (z < 0) {
					return false;
				}
			}
		}
		return true;
	}
}
0