結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー tentententen
提出日時 2023-01-13 15:43:07
言語 Java21
(openjdk 21)
結果
AC  
実行時間 758 ms / 3,000 ms
コード長 2,610 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 88,184 KB
実行使用メモリ 191,244 KB
最終ジャッジ日時 2023-08-25 22:43:36
合計ジャッジ時間 16,375 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
49,408 KB
testcase_01 AC 37 ms
49,704 KB
testcase_02 AC 36 ms
49,420 KB
testcase_03 AC 49 ms
50,092 KB
testcase_04 AC 37 ms
49,372 KB
testcase_05 AC 37 ms
49,740 KB
testcase_06 AC 38 ms
49,352 KB
testcase_07 AC 39 ms
49,952 KB
testcase_08 AC 39 ms
47,692 KB
testcase_09 AC 48 ms
50,196 KB
testcase_10 AC 38 ms
49,496 KB
testcase_11 AC 43 ms
50,184 KB
testcase_12 AC 39 ms
49,488 KB
testcase_13 AC 38 ms
49,356 KB
testcase_14 AC 678 ms
187,716 KB
testcase_15 AC 697 ms
190,684 KB
testcase_16 AC 758 ms
190,576 KB
testcase_17 AC 694 ms
190,744 KB
testcase_18 AC 698 ms
189,436 KB
testcase_19 AC 684 ms
189,112 KB
testcase_20 AC 677 ms
189,096 KB
testcase_21 AC 622 ms
189,304 KB
testcase_22 AC 677 ms
190,676 KB
testcase_23 AC 682 ms
191,016 KB
testcase_24 AC 618 ms
191,244 KB
testcase_25 AC 669 ms
189,896 KB
testcase_26 AC 55 ms
50,488 KB
testcase_27 AC 513 ms
141,136 KB
testcase_28 AC 262 ms
89,472 KB
testcase_29 AC 267 ms
89,452 KB
testcase_30 AC 357 ms
114,256 KB
testcase_31 AC 510 ms
142,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] values = new int[n];
        int idx = 0;
        long div = 1;
        for (int i = 1; i <= 9; i++) {
            int x = sc.nextInt();
            div *= fact(x);
            for (int j = 0; j < x; j++) {
                values[idx++] = i; 
            }
        }
        long[][] dp = new long[1 << n][];
        for (int i = 0; i < n; i++) {
            dp[1 << i] = new long[k];
            dp[1 << i][values[i] % k]++; 
        }
        for (int i = 1; i < (1 << n); i++) {
            if (dp[i] != null) {
                continue;
            }
            dp[i] = new long[k];
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) == 0) {
                    continue;
                }
                int key = (i ^ (1 << j));
                for (int a = 0; a < k; a++) {
                    dp[i][(a * 10 + values[j]) % k] += dp[key][a];
                }
            }
        }
        System.out.println(dp[(1 << n) - 1][0] / div);
    }
    
    static long fact(int x) {
        if (x == 0) {
            return 1;
        } else {
            return fact(x - 1) * x;
        }
    }
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0