結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー KudeKude
提出日時 2021-07-30 21:51:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 666 ms / 3,000 ms
コード長 1,955 bytes
コンパイル時間 4,773 ms
コンパイル使用メモリ 271,436 KB
実行使用メモリ 118,656 KB
最終ジャッジ日時 2023-10-14 04:21:43
合計ジャッジ時間 17,143 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 6 ms
4,396 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 182 ms
4,348 KB
testcase_06 AC 184 ms
4,352 KB
testcase_07 AC 183 ms
4,348 KB
testcase_08 AC 407 ms
76,376 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 659 ms
118,376 KB
testcase_12 AC 538 ms
90,840 KB
testcase_13 AC 665 ms
118,592 KB
testcase_14 AC 666 ms
118,112 KB
testcase_15 AC 662 ms
117,144 KB
testcase_16 AC 660 ms
118,540 KB
testcase_17 AC 142 ms
28,044 KB
testcase_18 AC 183 ms
36,152 KB
testcase_19 AC 275 ms
49,752 KB
testcase_20 AC 169 ms
35,212 KB
testcase_21 AC 22 ms
7,672 KB
testcase_22 AC 289 ms
27,464 KB
testcase_23 AC 352 ms
47,492 KB
testcase_24 AC 534 ms
84,428 KB
testcase_25 AC 575 ms
95,468 KB
testcase_26 AC 620 ms
109,132 KB
testcase_27 AC 648 ms
114,496 KB
testcase_28 AC 660 ms
118,600 KB
testcase_29 AC 660 ms
118,420 KB
testcase_30 AC 659 ms
118,656 KB
testcase_31 AC 662 ms
118,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint;

int n, k;
int c[10];
string s;
int CNT = 0;
unordered_map<string, unordered_map<int, int>> mp;
ll ans;
mint P;

void dfs1(int rest, int i) {
    if (rest == 0) {
        auto& m = mp[s];
        do {
            int r = (mint(stoi(s)) * P).val();
            m[r]++;
        } while(next_permutation(all(s)));
        return;
    }
    if (i == 10) return;
    for(int cnt = 0; cnt <= min(rest, c[i]); cnt++) {
        s.append(cnt, '0' + i);
        dfs1(rest - cnt, i + 1);
        s.resize(s.size() - cnt);
    }
}

string s2;
void dfs2(int rest, int i) {
    if (i == 10) {
        if (rest) return;
        auto& m = mp[s];
        do {
            int r = (-mint(stoi(s2))).val();
            ans += m[r];
        } while(next_permutation(all(s2)));
        return;
    }
    for(int cnt = 0; cnt <= min(rest, c[i]); cnt++) {
        s2.append(cnt, '0' + i);
        s.append(c[i] - cnt, '0' + i);
        dfs2(rest - cnt, i + 1);
        s.resize(s.size() - (c[i] - cnt));
        s2.resize(s2.size() - cnt);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> k;
    rep(i, 9) cin >> c[i + 1];
    if (n == 1) {
        int d = 1;
        while(!c[d]) d++;
        cout << int(d % k == 0) << endl;
        return 0;
    }
    mint::set_mod(k);
    int n1 = n / 2, n2 = n - n1;
    P = 1;
    rep(_, n2) P *= 10;
    dfs1(n1, 1);
    dfs2(n2, 1);
    cout << ans << endl;
}
0