結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー tentententen
提出日時 2022-08-18 17:04:37
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,989 bytes
コンパイル時間 2,364 ms
コンパイル使用メモリ 78,668 KB
実行使用メモリ 176,152 KB
最終ジャッジ日時 2024-04-16 01:16:49
合計ジャッジ時間 11,724 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
42,420 KB
testcase_01 AC 55 ms
36,964 KB
testcase_02 AC 53 ms
36,876 KB
testcase_03 AC 130 ms
45,744 KB
testcase_04 AC 54 ms
36,868 KB
testcase_05 AC 54 ms
37,144 KB
testcase_06 AC 53 ms
37,168 KB
testcase_07 AC 53 ms
36,752 KB
testcase_08 AC 53 ms
36,984 KB
testcase_09 AC 65 ms
37,816 KB
testcase_10 AC 58 ms
37,132 KB
testcase_11 AC 61 ms
37,224 KB
testcase_12 AC 54 ms
37,196 KB
testcase_13 AC 56 ms
36,788 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

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