結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー hiromi_ayasehiromi_ayase
提出日時 2016-07-08 22:41:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 588 ms / 2,000 ms
コード長 1,249 bytes
コンパイル時間 4,106 ms
コンパイル使用メモリ 74,896 KB
実行使用メモリ 87,788 KB
最終ジャッジ日時 2023-08-03 08:34:23
合計ジャッジ時間 27,486 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
56,224 KB
testcase_01 AC 123 ms
55,792 KB
testcase_02 AC 131 ms
55,452 KB
testcase_03 AC 130 ms
55,488 KB
testcase_04 AC 124 ms
55,676 KB
testcase_05 AC 128 ms
55,764 KB
testcase_06 AC 129 ms
55,772 KB
testcase_07 AC 129 ms
55,456 KB
testcase_08 AC 130 ms
55,824 KB
testcase_09 AC 265 ms
61,912 KB
testcase_10 AC 231 ms
59,904 KB
testcase_11 AC 274 ms
61,972 KB
testcase_12 AC 235 ms
59,180 KB
testcase_13 AC 458 ms
70,432 KB
testcase_14 AC 290 ms
61,900 KB
testcase_15 AC 220 ms
59,028 KB
testcase_16 AC 222 ms
59,780 KB
testcase_17 AC 274 ms
61,536 KB
testcase_18 AC 576 ms
83,220 KB
testcase_19 AC 123 ms
55,656 KB
testcase_20 AC 124 ms
55,676 KB
testcase_21 AC 123 ms
55,672 KB
testcase_22 AC 123 ms
55,644 KB
testcase_23 AC 123 ms
55,700 KB
testcase_24 AC 122 ms
55,804 KB
testcase_25 AC 127 ms
55,956 KB
testcase_26 AC 123 ms
55,772 KB
testcase_27 AC 125 ms
55,800 KB
testcase_28 AC 121 ms
56,204 KB
testcase_29 AC 124 ms
55,764 KB
testcase_30 AC 122 ms
55,812 KB
testcase_31 AC 124 ms
55,992 KB
testcase_32 AC 124 ms
55,704 KB
testcase_33 AC 123 ms
53,808 KB
testcase_34 AC 125 ms
55,676 KB
testcase_35 AC 126 ms
55,500 KB
testcase_36 AC 128 ms
53,588 KB
testcase_37 AC 127 ms
55,900 KB
testcase_38 AC 128 ms
55,764 KB
testcase_39 AC 127 ms
55,624 KB
testcase_40 AC 126 ms
55,964 KB
testcase_41 AC 124 ms
55,664 KB
testcase_42 AC 125 ms
55,444 KB
testcase_43 AC 123 ms
55,504 KB
testcase_44 AC 124 ms
55,808 KB
testcase_45 AC 122 ms
55,804 KB
testcase_46 AC 124 ms
55,748 KB
testcase_47 AC 124 ms
55,764 KB
testcase_48 AC 124 ms
55,740 KB
testcase_49 AC 497 ms
83,404 KB
testcase_50 AC 208 ms
58,988 KB
testcase_51 AC 197 ms
58,804 KB
testcase_52 AC 272 ms
62,024 KB
testcase_53 AC 210 ms
58,728 KB
testcase_54 AC 294 ms
59,996 KB
testcase_55 AC 165 ms
55,976 KB
testcase_56 AC 207 ms
58,924 KB
testcase_57 AC 345 ms
67,184 KB
testcase_58 AC 453 ms
79,252 KB
testcase_59 AC 400 ms
71,324 KB
testcase_60 AC 302 ms
64,644 KB
testcase_61 AC 489 ms
87,788 KB
testcase_62 AC 304 ms
64,392 KB
testcase_63 AC 462 ms
78,764 KB
testcase_64 AC 291 ms
59,628 KB
testcase_65 AC 242 ms
62,208 KB
testcase_66 AC 297 ms
63,816 KB
testcase_67 AC 293 ms
64,076 KB
testcase_68 AC 294 ms
59,636 KB
testcase_69 AC 126 ms
55,972 KB
testcase_70 AC 132 ms
55,656 KB
testcase_71 AC 125 ms
55,812 KB
testcase_72 AC 127 ms
55,888 KB
testcase_73 AC 129 ms
55,484 KB
testcase_74 AC 130 ms
55,712 KB
testcase_75 AC 128 ms
55,948 KB
testcase_76 AC 126 ms
55,764 KB
testcase_77 AC 126 ms
55,740 KB
testcase_78 AC 126 ms
55,448 KB
testcase_79 AC 127 ms
55,664 KB
testcase_80 AC 124 ms
55,736 KB
testcase_81 AC 128 ms
55,652 KB
testcase_82 AC 126 ms
55,860 KB
testcase_83 AC 124 ms
55,900 KB
testcase_84 AC 127 ms
56,056 KB
testcase_85 AC 126 ms
55,740 KB
testcase_86 AC 127 ms
55,680 KB
testcase_87 AC 124 ms
55,984 KB
testcase_88 AC 125 ms
56,020 KB
testcase_89 AC 351 ms
66,704 KB
testcase_90 AC 457 ms
71,688 KB
testcase_91 AC 588 ms
83,176 KB
testcase_92 AC 270 ms
62,088 KB
testcase_93 AC 459 ms
71,604 KB
testcase_94 AC 168 ms
55,780 KB
testcase_95 AC 510 ms
75,792 KB
testcase_96 AC 442 ms
70,812 KB
testcase_97 AC 425 ms
68,164 KB
testcase_98 AC 324 ms
59,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.nextLine());
		String[] s = sc.nextLine().split(" ");
		int K = s.length;
		int[] H = new int[K];
		for (int i = 0; i < K; i++) {
			H[i] = Integer.parseInt(s[i]);
			N -= H[i];
		}
		N++;

		if (H[0] == 0) {
			System.out.println(1);
		} else if (N < K || K < 0 || N <= 0) {
			System.out.println("NA");
		} else {
			System.out.println(combMod(N, K, 1000000007));
		}
	}

	public static long combMod(long n, long r, long mod) {
		long comb = 1;
		for (int i = 0; i < r; i++) {
			comb = (comb * (n - i)) % mod;
			comb = (comb * inverse(r - i, mod)) % mod;
		}
		return comb;
	}

	public static long[] extgcd(long a, long b) {
		long u = 1;
		long v = 0;
		long x = 0;
		long y = 1;

		while (a > 0) {
			long q = b / a;
			x -= q * u;
			y -= q * v;
			b -= q * a;

			long tmp;
			tmp = x;
			x = u;
			u = tmp;
			tmp = y;
			y = v;
			v = tmp;
			tmp = b;
			b = a;
			a = tmp;
		}

		return new long[] { b, x, y };
	}

	public static long inverse(long n, long mod) {
		long[] gcd = extgcd(n, mod);
		if (gcd[0] == 1) {
			return (gcd[1] + mod) % mod;
		} else {
			return 0;
		}
	}
}
0