結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー tenten
提出日時 2022-08-18 17:18:36
言語 Java
(openjdk 23)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,021 bytes
コンパイル時間 2,284 ms
コンパイル使用メモリ 78,052 KB
実行使用メモリ 196,404 KB
最終ジャッジ日時 2024-10-06 10:27:15
合計ジャッジ時間 11,489 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23 TLE * 5
権限があれば一括ダウンロードができます

ソースコード

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;
        long div = 1;
        for (int i = 1; i <= 9; i++) {
            int x = sc.nextInt();
            for (int j = 0; j < x; j++) {
                values[idx++] = i;
            }
            div *= proc(x);
        }
        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) / div);
    }
    
    static long proc(int x) {
        if (x == 0) {
            return 1;
        } else {
            return x * proc(x - 1);
        }
    }
    
    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;
                }
                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