結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー nebukuro09
提出日時 2021-07-30 21:44:39
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,329 ms / 3,000 ms
コード長 1,264 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 120,352 KB
実行使用メモリ 133,752 KB
最終ジャッジ日時 2024-06-22 12:00:34
合計ジャッジ時間 19,905 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void main() {
    auto s = readln.split;
    auto N = s[0].to!int;
    auto K = s[1].to!int;
    auto A = 0 ~ readln.split.map!(to!int).array;

    int[] B;
    foreach (i; 1..10) foreach (_; 0..A[i]) B ~= i;

    int[] base = new int[](N+1);
    base[0] = 1;
    foreach (i; 1..N+1) base[i] = base[i-1] * 10 % K;

    auto dp = new long[][](1<<N, K);
    dp[0][0] = 1;
    foreach (mask; 0..1<<N) {
        foreach (i; 0..N) foreach (j; 0..K) {
            if (mask & (1 << i)) continue;
            if (i > 0 && B[i] == B[i-1] && !(mask & (1 << (i-1)))) continue;
            int keta = mask.popcnt;
            int num = base[keta] * B[i] % K + j;
            num %= K;
            dp[(mask | (1 << i))][num] += dp[mask][j];
        }
    }

    dp[(1<<N)-1][0].writeln;
}

long[] solve(int N, int K, int[] A) {
    auto ans = new long[](K);

    do {
        int n = 0;
        int keta = 1;
        foreach (i; 0..N) {
            (n += (keta * A[i] % K)) %= K;
            keta = keta * 10 % K;
        }
        ans[n] += 1;
    } while (nextPermutation(A));

    return ans;
}
0