結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー sten_sansten_san
提出日時 2021-07-30 22:11:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,114 ms / 3,000 ms
コード長 2,536 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 203,132 KB
実行使用メモリ 259,564 KB
最終ジャッジ日時 2023-10-14 05:06:26
合計ジャッジ時間 18,771 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,500 KB
testcase_01 AC 2 ms
5,492 KB
testcase_02 AC 2 ms
5,732 KB
testcase_03 AC 7 ms
12,836 KB
testcase_04 AC 2 ms
5,440 KB
testcase_05 AC 2 ms
5,492 KB
testcase_06 AC 2 ms
5,672 KB
testcase_07 AC 2 ms
5,548 KB
testcase_08 AC 2 ms
5,564 KB
testcase_09 AC 5 ms
6,420 KB
testcase_10 AC 3 ms
6,012 KB
testcase_11 AC 3 ms
6,172 KB
testcase_12 AC 2 ms
5,808 KB
testcase_13 AC 3 ms
6,044 KB
testcase_14 AC 1,069 ms
259,360 KB
testcase_15 AC 1,049 ms
259,352 KB
testcase_16 AC 1,058 ms
259,564 KB
testcase_17 AC 1,069 ms
259,364 KB
testcase_18 AC 1,074 ms
259,492 KB
testcase_19 AC 1,065 ms
259,500 KB
testcase_20 AC 1,041 ms
259,424 KB
testcase_21 AC 930 ms
259,564 KB
testcase_22 AC 1,114 ms
259,484 KB
testcase_23 AC 1,036 ms
259,508 KB
testcase_24 AC 1,023 ms
259,364 KB
testcase_25 AC 1,015 ms
259,424 KB
testcase_26 AC 60 ms
205,716 KB
testcase_27 AC 785 ms
259,348 KB
testcase_28 AC 381 ms
238,108 KB
testcase_29 AC 388 ms
238,436 KB
testcase_30 AC 545 ms
255,128 KB
testcase_31 AC 791 ms
259,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return std::vector(arg, init);
    else return std::vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

constexpr int lim = 14;

int64_t dp[2][1 << lim][1000];

int main() {
    int n, k; cin >> n >> k;

    auto [c, count] = [&] {
        array<int, lim> c = {};
        array<int, 9> count = {};

        auto iter = begin(c);
        for (int i = 1; i <= 9; ++i) {
            int v; cin >> v;
            count[i - 1] = v;
            while (v--) {
                *iter++ = i;
            }
        }

        return make_tuple(c, count);
    }();

    auto pow = [&] {
        array<int, lim> pow = {};

        pow[0] = 1;
        for (int i = 1; i < n; ++i) {
            pow[i] = (pow[i - 1] * 10) % k;
        }

        return pow;
    }();

    auto popcount = [](auto x) {
        return bitset<sizeof(x) * CHAR_BIT>(x).count();
    };

    bool turn = false;

    dp[turn][0][0] = 1;
    for (int i = 1; i <= n; ++i) {
        for (int to = 0; to < (1 << n); ++to) {
            if (i != popcount(to)) {
                continue;
            }

            for (int j = 0; j < n; ++j) {
                if (!(to & (1 << j))) {
                    continue;
                }

                int from = to ^ (1 << j);
                for (int r = 0; r < k; ++r) {
                    dp[!turn][to][r] += dp[turn][from][(k + (r - (pow[i - 1] * c[j])) % k) % k];
                }
            }
        }

        turn = !turn;
        for (int j = 0; j < (1 << n); ++j) {
            for (int r = 0; r < k; ++r) {
                dp[!turn][j][r] = 0;
            }
        }
    }

    auto ans = dp[turn][(1 << n) - 1][0];

    for (int i = 0; i < 9; ++i) {
        int64_t f = 1;
        for (int j = 1; j <= count[i]; ++j) {
            f *= j;
        }
        ans /= f;
    }

    cout << ans << endl;
}

0