結果

問題 No.389 ロジックパズルの組み合わせ
ユーザー hiromi_ayase
提出日時 2016-07-08 22:41:24
言語 Java
(openjdk 23)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 1,249 bytes
コンパイル時間 2,147 ms
コンパイル使用メモリ 77,524 KB
実行使用メモリ 74,240 KB
最終ジャッジ日時 2024-10-13 06:07:42
合計ジャッジ時間 23,616 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 99
権限があれば一括ダウンロードができます

ソースコード

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