結果
問題 |
No.1634 Sorting Integers (Multiple of K) Hard
|
ユーザー |
![]() |
提出日時 | 2021-07-30 23:17:21 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,404 ms / 3,000 ms |
コード長 | 2,645 bytes |
コンパイル時間 | 1,288 ms |
コンパイル使用メモリ | 113,360 KB |
最終ジャッジ日時 | 2025-01-23 12:52:28 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 28 |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <cassert> #include <numeric> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n, k; cin >> n >> k; vector<ll> c(10); vector<ll> u; for(ll i = 1; i <= 9; i++) { cin >> c[i]; for(ll j = 0; j < c[i]; j++) u.push_back(i); } vector<ll> pow10(n); pow10[0] = 1%k; for(ll i = 1; i < n; i++){ pow10[i] = (pow10[i-1]*10)%k; } set<vector<ll>> st; ll m = n/2; auto inv = [&](vector<ll> v){ assert(c.size() == 10); vector<ll> ans(10); for(ll i = 1; i <= 9; i++) ans[i] = c[i]-v[i]; return ans; }; for(ll i = 0; i < 1<<n; i++){ vector<ll> v(10); for(ll j = 0; j < n; j++){ if(i&(1<<j)) v[u[j]]++; } if(accumulate(v.begin(), v.end(), 0) == m){ st.insert(v); } } auto list = [&](vector<ll> vv)->map<ll, ll>{ vector<ll> v; for(ll i = 1; i <= 9; i++){ for(ll j = 0; j < vv[i]; j++) v.push_back(i); } ll n = v.size(); // debug_value(n) vector<ll> u(n); for(ll i = 0; i < n; i++) u[i] = i; set<ll> st; do{ ll sum = 0; for(ll i = 0; i < n; i++){ sum = sum*10+v[u[i]]; } st.insert(sum); }while(next_permutation(u.begin(), u.end())); map<ll, ll> ans; for(ll x: st){ ll rem = x%k; if(ans.count(rem)) ans[rem]++; else ans[rem] = 1; } return ans; }; auto cnt = [&](vector<ll> v)->ll{ assert(accumulate(v.begin(), v.end(), 0) == m); auto u = inv(v); auto mpv = list(v); auto mpu = list(u); ll ans = 0; for(auto [x, c]: mpv){ ll rem = (k-x*pow10[n-m])%k; rem = (rem+k)%k; // debug_value(rem) if(mpu.count(rem) > 0) ans += mpu[rem]*c; } return ans; }; ll ans = 0; for(auto v: st) ans += cnt(v); cout << ans << endl; }