結果

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

ソースコード

diff #

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

public class Main {
    static int k;
    static HashMap<Long, HashMap<Integer, Long>> dp = new HashMap<>();
    static long[] base = new long[11];
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        k = sc.nextInt();
        base[0] = 1;
        for (int i = 1; i <= 10; i++) {
            base[i] = base[i - 1] * 10;
        }
        long mask = 0;
        for (int i = 0; i < 9; i++) {
            mask += sc.nextInt() * base[i];
        }
        System.out.println(dfw(mask, 0));
    }
    
    static long dfw(long mask, int mod) {
        if (mask == 0) {
            if (mod == 0) {
                return 1;
            } else {
                return 0;
            }
        }
        if (!dp.containsKey(mask)) {
            dp.put(mask, new HashMap<>());
        }
        if (!dp.get(mask).containsKey(mod)) {
            long ans = 0;
            for (int i = 0; i < 10; i++) {
                if (mask % base[i + 1] / base[i] == 0) {
                    continue;
                }
                ans += dfw(mask - base[i], (mod * 10 + i + 1) % k);
            }
            dp.get(mask).put(mod, ans);
        }
        return dp.get(mask).get(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