結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー KilisameKilisame
提出日時 2017-04-25 16:18:34
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,190 bytes
コンパイル時間 2,001 ms
コンパイル使用メモリ 75,764 KB
実行使用メモリ 59,592 KB
最終ジャッジ日時 2023-10-11 09:39:21
合計ジャッジ時間 7,326 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
56,072 KB
testcase_01 AC 110 ms
55,832 KB
testcase_02 AC 108 ms
55,944 KB
testcase_03 AC 113 ms
56,156 KB
testcase_04 AC 185 ms
59,276 KB
testcase_05 AC 112 ms
56,176 KB
testcase_06 AC 205 ms
58,836 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 189 ms
59,032 KB
testcase_10 AC 116 ms
55,960 KB
testcase_11 AC 164 ms
59,156 KB
testcase_12 AC 109 ms
55,728 KB
testcase_13 AC 137 ms
58,808 KB
testcase_14 AC 113 ms
55,944 KB
testcase_15 AC 114 ms
55,460 KB
testcase_16 AC 111 ms
56,124 KB
testcase_17 AC 113 ms
56,264 KB
testcase_18 AC 208 ms
59,380 KB
testcase_19 AC 195 ms
58,668 KB
testcase_20 AC 192 ms
58,904 KB
testcase_21 AC 204 ms
56,780 KB
testcase_22 AC 202 ms
58,748 KB
testcase_23 AC 213 ms
58,816 KB
testcase_24 AC 206 ms
58,700 KB
権限があれば一括ダウンロードができます

ソースコード

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]);
        /* 何番目のクリスタルを何個使った時にゴールにたどり着くかの全パターンが求まるので、あとは各パターンごとにクリスタルを使う順が何通りあるかを求め、足し合わせる */
        long result = 0L;
        for (int[] selectPattern : resultSelectPatternList) {
            result += permutation(selectPattern);
        }
        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 long 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.longValue();
    }

    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