結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー milanis48663220milanis48663220
提出日時 2021-07-30 23:10:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,620 bytes
コンパイル時間 1,294 ms
コンパイル使用メモリ 119,420 KB
実行使用メモリ 4,484 KB
最終ジャッジ日時 2023-10-14 08:08:14
合計ジャッジ時間 25,880 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 587 ms
4,348 KB
testcase_06 AC 652 ms
4,348 KB
testcase_07 AC 587 ms
4,348 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 WA -
testcase_12 AC 1,493 ms
4,348 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 292 ms
4,352 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1,134 ms
4,352 KB
testcase_23 AC 1,262 ms
4,348 KB
testcase_24 AC 1,337 ms
4,352 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
    int n, k; cin >> n >> k;
    vector<int> c(10);
    vector<int> u;
    for(int i = 1; i <= 9; i++) {
        cin >> c[i];
        for(int j = 0; j < c[i]; j++) u.push_back(i);
    }
    vector<int> pow10(n);
    pow10[0] = 1%k;
    for(int i = 1; i < n; i++){
        pow10[i] = (pow10[i-1]*10)%k;
    }
    set<vector<int>> st;
    int m = n/2;
    auto inv = [&](vector<int> v){
        assert(c.size() == 10);
        vector<int> ans(10);
        for(int i = 1; i <= 9; i++) ans[i] = c[i]-v[i];
        return ans; 
    };
    for(int i = 0; i < 1<<n; i++){
        vector<int> v(10);
        for(int 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<int> vv)->map<int, ll>{
        vector<int> v;
        for(int i = 1; i <= 9; i++){
            for(int j = 0; j < vv[i]; j++) v.push_back(i);
        }
        int n = v.size();
        // debug_value(n)
        vector<int> u(n);
        for(int i = 0; i < n; i++) u[i] = i;
        set<int> st;
        do{
            int sum = 0;
            for(int i = 0; i < n; i++){
                sum = sum*10+v[u[i]];
            }
            st.insert(sum);
        }while(next_permutation(u.begin(), u.end()));
        map<int, ll> ans;
        for(int x: st){
            int rem = x%k;
            if(ans.count(rem)) ans[rem]++;
            else ans[rem] = 1;
        }
        return ans;
    };
    auto cnt = [&](vector<int> v)->ll{
        auto u = inv(v);
        auto mpv = list(v);
        auto mpu = list(u);
        ll ans = 0;
        for(auto [x, c]: mpv){
            int 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