結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー tenten
提出日時 2022-08-18 17:54:00
言語 Java
(openjdk 23)
結果
AC  
実行時間 675 ms / 3,000 ms
コード長 1,958 bytes
コンパイル時間 2,031 ms
コンパイル使用メモリ 77,516 KB
実行使用メモリ 182,108 KB
最終ジャッジ日時 2024-10-06 11:25:31
合計ジャッジ時間 10,145 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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