結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー KilisameKilisame
提出日時 2017-04-25 16:25:32
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,317 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 81,588 KB
実行使用メモリ 61,064 KB
最終ジャッジ日時 2023-10-11 09:40:12
合計ジャッジ時間 7,534 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 117 ms
56,128 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 195 ms
59,128 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
    }

}
0