結果
問題 | No.1631 Sorting Integers (Multiple of K) Easy |
ユーザー | sten_san |
提出日時 | 2021-07-30 22:11:14 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,011 ms / 3,000 ms |
コード長 | 2,536 bytes |
コンパイル時間 | 1,967 ms |
コンパイル使用メモリ | 196,336 KB |
最終ジャッジ日時 | 2025-01-23 11:54:15 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 28 |
ソースコード
#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; }