結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー tentententen
提出日時 2022-08-18 17:54:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 739 ms / 3,000 ms
コード長 1,958 bytes
コンパイル時間 3,870 ms
コンパイル使用メモリ 77,744 KB
実行使用メモリ 182,528 KB
最終ジャッジ日時 2024-04-16 01:51:31
合計ジャッジ時間 12,736 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,160 KB
testcase_01 AC 50 ms
36,828 KB
testcase_02 AC 52 ms
36,816 KB
testcase_03 AC 76 ms
37,592 KB
testcase_04 AC 52 ms
36,956 KB
testcase_05 AC 51 ms
37,124 KB
testcase_06 AC 51 ms
36,964 KB
testcase_07 AC 51 ms
37,144 KB
testcase_08 AC 52 ms
36,900 KB
testcase_09 AC 56 ms
37,460 KB
testcase_10 AC 51 ms
37,272 KB
testcase_11 AC 51 ms
37,592 KB
testcase_12 AC 51 ms
37,480 KB
testcase_13 AC 50 ms
37,264 KB
testcase_14 AC 476 ms
181,968 KB
testcase_15 AC 738 ms
182,208 KB
testcase_16 AC 716 ms
182,188 KB
testcase_17 AC 736 ms
182,152 KB
testcase_18 AC 739 ms
181,652 KB
testcase_19 AC 713 ms
182,272 KB
testcase_20 AC 242 ms
182,528 KB
testcase_21 AC 241 ms
182,176 KB
testcase_22 AC 241 ms
182,056 KB
testcase_23 AC 250 ms
181,932 KB
testcase_24 AC 248 ms
182,164 KB
testcase_25 AC 244 ms
182,420 KB
testcase_26 AC 56 ms
37,800 KB
testcase_27 AC 365 ms
132,072 KB
testcase_28 AC 215 ms
78,424 KB
testcase_29 AC 160 ms
79,104 KB
testcase_30 AC 241 ms
101,144 KB
testcase_31 AC 306 ms
131,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;

public class Main {
    static int n;
    static int k;
    static long[][] dp;
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        n = sc.nextInt();
        k = sc.nextInt();
        values = new int[n];
        int idx = 0;
        for (int i = 1; i <= 9; i++) {
            int x = sc.nextInt();
            for (int j = 0; j < x; j++) {
                values[idx++] = i;
            }
        }
        dp = new long[1 << n][k];
        for (long[] arr : dp) {
            Arrays.fill(arr, -1);
        }
        Arrays.fill(dp[0], 0);
        dp[0][0] = 1;
        System.out.println(dfw((1 << n) - 1, 0));
    }
    
    static long dfw(int mask, int mod) {
        if (dp[mask][mod] < 0) {
            dp[mask][mod] = 0;
            for (int i = 0; i < n; i++) {
                if ((mask & (1 << i)) == 0) {
                    continue;
                }
                if (i > 0 && values[i] == values[i - 1] && ((mask & (1 << (i - 1)))) != 0) {
                    continue;
                }
                dp[mask][mod] += dfw(mask ^ (1 << i), (mod * 10 + values[i]) % k);
            }
        }
        return dp[mask][mod];
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0