結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー milanis48663220milanis48663220
提出日時 2021-07-30 23:17:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,421 ms / 3,000 ms
コード長 2,645 bytes
コンパイル時間 1,651 ms
コンパイル使用メモリ 117,808 KB
実行使用メモリ 4,456 KB
最終ジャッジ日時 2023-10-14 08:16:04
合計ジャッジ時間 24,658 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 4 ms
4,356 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 577 ms
4,352 KB
testcase_06 AC 627 ms
4,352 KB
testcase_07 AC 574 ms
4,348 KB
testcase_08 AC 566 ms
4,356 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 894 ms
4,368 KB
testcase_12 AC 1,421 ms
4,372 KB
testcase_13 AC 885 ms
4,412 KB
testcase_14 AC 1,315 ms
4,372 KB
testcase_15 AC 1,313 ms
4,424 KB
testcase_16 AC 917 ms
4,368 KB
testcase_17 AC 347 ms
4,352 KB
testcase_18 AC 289 ms
4,352 KB
testcase_19 AC 693 ms
4,372 KB
testcase_20 AC 245 ms
4,352 KB
testcase_21 AC 56 ms
4,352 KB
testcase_22 AC 1,085 ms
4,352 KB
testcase_23 AC 1,177 ms
4,352 KB
testcase_24 AC 1,274 ms
4,348 KB
testcase_25 AC 1,278 ms
4,352 KB
testcase_26 AC 1,271 ms
4,368 KB
testcase_27 AC 1,236 ms
4,456 KB
testcase_28 AC 1,175 ms
4,368 KB
testcase_29 AC 1,049 ms
4,384 KB
testcase_30 AC 903 ms
4,372 KB
testcase_31 AC 923 ms
4,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0