結果
| 問題 |
No.498 ワープクリスタル (給料日編)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-04-25 16:25:32 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,317 bytes |
| コンパイル時間 | 3,164 ms |
| コンパイル使用メモリ | 80,300 KB |
| 実行使用メモリ | 61,724 KB |
| 最終ジャッジ日時 | 2024-09-13 08:50:10 |
| 合計ジャッジ時間 | 8,109 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 4 |
| other | AC * 2 WA * 19 |
ソースコード
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
private static int gx;
private static int gy;
private static int[][] crystalArray;
private static List<int[]> resultSelectPatternList;
private static final BigInteger DIVISER = new BigInteger("1000000007");
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
gx = Integer.parseInt(sc.next());
gy = Integer.parseInt(sc.next());
int k = Integer.parseInt(sc.next());
/* k種のクリスタル。[k][0]=x移動量, [k][1]=y移動量, [k][2]=個数 */
crystalArray = new int[k][3];
for (int i = 0; i < k; i++) {
crystalArray[i][0] = Integer.parseInt(sc.next());
crystalArray[i][1] = Integer.parseInt(sc.next());
crystalArray[i][2] = Integer.parseInt(sc.next());
}
sc.close();
/* ナップサック問題のように、k種目のクリスタルを0個使う場合~N(k)個使う場合に、ゴールまでの相対座標を求める。 */
resultSelectPatternList = new ArrayList<int[]>();
selectCrystal(0, new int[k], new int[2]);
/* 何番目のクリスタルを何個使った時にゴールにたどり着くかの全パターンが求まるので、あとは各パターンごとにクリスタルを使う順が何通りあるかを求め、足し合わせる */
BigInteger result = BigInteger.ZERO;
for (int[] selectPattern : resultSelectPatternList) {
result.add(permutation(selectPattern));
while (result.compareTo(DIVISER) > 0) {
result = result.mod(DIVISER);
}
}
System.out.println(result);
}
private static void selectCrystal(int index, int[] selected, int[] position) {
if (index == crystalArray.length) {
if (position[0] == gx && position[1] == gy) {
resultSelectPatternList.add(selected);
}
return;
}
for (int i = 0; i <= crystalArray[index][2]; i++) {
int[] currentSelect = Arrays.copyOf(selected, selected.length);
int[] currentPosition = Arrays.copyOf(position, position.length);
currentSelect[index] = i;
currentPosition[0] += crystalArray[index][0] * i;
currentPosition[1] += crystalArray[index][1] * i;
selectCrystal(index + 1, currentSelect, currentPosition);
}
}
private static BigInteger permutation(int[] pattern) {
int total = 0;
for (int i = 0; i < pattern.length; i++) {
total = total += pattern[i];
}
BigInteger totalFact = factorialBI(total);
for (int i = 0; i < pattern.length; i++) {
totalFact = totalFact.divide(factorialBI(pattern[i]));
}
while (totalFact.compareTo(DIVISER) > 0) {
totalFact = totalFact.mod(DIVISER);
}
return totalFact;
}
private static BigInteger factorialBI(int n) {
BigInteger bi = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
bi = bi.multiply(new BigInteger(String.valueOf(i)));
}
return bi;
}
}