結果
問題 | No.1634 Sorting Integers (Multiple of K) Hard |
ユーザー |
![]() |
提出日時 | 2021-07-30 21:21:11 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 576 ms / 3,000 ms |
コード長 | 1,352 bytes |
コンパイル時間 | 2,150 ms |
コンパイル使用メモリ | 187,152 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-15 22:45:31 |
合計ジャッジ時間 | 11,878 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h>using namespace std;int main(){int N, K;cin >> N >> K;vector<int> c(10);c[0] = 0;for (int i = 1; i < 10; i++){cin >> c[i];}vector<int> A;for (int i = 1; i < 10; i++){for (int j = 0; j < c[i]; j++){A.push_back(i);}}int N1 = N / 2, N2 = (N + 1) / 2;long long P = 1;for (int i = 0; i < N2; i++){P *= 10;}set<pair<vector<int>, vector<int>>> st;for (int i = 0; i < (1 << N); i++){if (__builtin_popcount(i) == N1){vector<int> B1, B2;for (int j = 0; j < N; j++){if ((i >> j & 1) == 1){B1.push_back(A[j]);} else {B2.push_back(A[j]);}}st.insert(make_pair(B1, B2));}}long long ans = 0;for (auto p : st){vector<int> B1 = p.first;vector<int> B2 = p.second;map<int, int> mp1;while (true){int x = 0;for (int i = 0; i < N1; i++){x = x * 10 + B1[i];}mp1[x * P % K]++;if (!next_permutation(B1.begin(), B1.end())){break;}}while (true){int x = 0;for (int i = 0; i < N2; i++){x = x * 10 + B2[i];}int y = (K - x % K) % K;ans += mp1[y];if (!next_permutation(B2.begin(), B2.end())){break;}}}cout << ans << endl;}