結果
| 問題 |
No.389 ロジックパズルの組み合わせ
|
| コンテスト | |
| ユーザー |
hiromi_ayase
|
| 提出日時 | 2016-07-08 22:39:10 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,240 bytes |
| コンパイル時間 | 2,043 ms |
| コンパイル使用メモリ | 77,268 KB |
| 実行使用メモリ | 83,808 KB |
| 最終ジャッジ日時 | 2024-10-13 04:38:24 |
| 合計ジャッジ時間 | 24,448 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 78 WA * 21 |
ソースコード
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 (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;
}
}
}
hiromi_ayase