結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー nawawannawawan
提出日時 2021-07-30 21:52:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,459 ms / 3,000 ms
コード長 3,252 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 173,648 KB
実行使用メモリ 131,300 KB
最終ジャッジ日時 2023-10-14 04:25:57
合計ジャッジ時間 23,688 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 4 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,356 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 279 ms
125,232 KB
testcase_15 AC 2,437 ms
131,044 KB
testcase_16 AC 2,435 ms
131,168 KB
testcase_17 AC 2,447 ms
131,016 KB
testcase_18 AC 2,451 ms
131,300 KB
testcase_19 AC 2,459 ms
131,064 KB
testcase_20 AC 348 ms
131,300 KB
testcase_21 AC 168 ms
115,472 KB
testcase_22 AC 532 ms
129,048 KB
testcase_23 AC 1,109 ms
129,084 KB
testcase_24 AC 784 ms
128,860 KB
testcase_25 AC 354 ms
127,704 KB
testcase_26 AC 8 ms
4,352 KB
testcase_27 AC 668 ms
95,376 KB
testcase_28 AC 872 ms
41,740 KB
testcase_29 AC 141 ms
42,724 KB
testcase_30 AC 1,167 ms
61,012 KB
testcase_31 AC 1,719 ms
95,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
struct modint{
    long long val;
    modint(): val(0) {}
    modint(long long x){
        if(x < 0) val = x % mod() + mod();
        else val = x % mod();
    }
    modint(const modint &t){
        val = t.val;
    }
    static int &mod(){
        static int m = 0;
        return m;
    }
    static void set_mod(int md){
        mod() = md;
    }
    modint& operator =(const modint m){
        val = m.val;
        return *this;
    }
    modint operator -(){
        return modint(-val);
    }
    modint& operator-=(const modint &m){
        val -= m.val;
        if(val < 0) val += mod();
        return *this;
    }
    modint& operator+=(const modint &m){
        val += m.val;
        if(val >= mod()) val -= mod();
        return *this;
    }
    modint& operator*=(const modint &m){
        val *= m.val;
        val %= mod();
        return *this;
    }
    modint& operator/=(modint m){
        *this *= m.inv();
        return *this;
    }
    modint inv(){
        long long x = 1, y = 0;
        long long a = val, b = mod();
        while(b != 0){
            long long t = a / b;
            a -= t * b;
            x -= t * y;
            swap(a, b);
            swap(x, y);
        }
        x %= mod();
        if(x < 0) x += mod();
        return modint(x);
    }
    modint pow(long long k){
        long long res = 1;
        long long v = val;
        while(k > 0){
            if(k & 1) res = res * v % mod();
            v = v * v % mod();
            k >>= 1;
        }
        return modint(res);
    }
    bool operator==(const modint &m){
        return val == m.val;
    }
    modint operator+(const modint &m){
        return modint(*this) += m;
    }
    modint operator-(const modint &m){
        return modint(*this) -= m;
    }
    modint operator*(const modint &m){
        return modint(*this) *= m;
    }
    modint operator/(const modint &m){
        return modint(*this) /= m;
    }
    bool operator!=(const modint &m){
        return modint(*this).val != m.val;
    }
    bool operator!=(const int &m){
        return modint(*this).val != m;
    }
};
using mint = modint;
int main(){
    int N, K;
    cin >> N >> K;
    modint::set_mod(K); 
    vector<int> c(9);
    for(int i = 0; i < 9; i++) cin >> c[i];
    vector<mint> ten(N + 1);
    ten[0] = 1;
    for(int i = 0; i < N; i++) ten[i + 1] = ten[i] * 10;
    vector<vector<long long>> dp((1 << N), vector<long long>(K));
    dp[0][0] = 1;
    vector<int> num(N);
    int id = 0;
    for(int i = 0; i < 9; i++){
        for(int j = 0; j < c[i]; j++){
            num[id] = i + 1;
            id++;
        }
    }
    for(int bit = 0; bit < (1 << N); bit++){
        int cnt = __builtin_popcount(bit);
        for(int k = 0; k < K; k++){
            if(dp[bit][k] == 0) continue;
            for(int j = 0; j < N; j++){
                if((bit & (1 << j)) == 0){
                    mint te = ten[N - 1 - cnt] * num[j] + k;
                    dp[bit ^ (1 << j)][te.val] += dp[bit][k];
                }
            }
        }
    }
    for(int i = 0; i < 9; i++){
        for(int j = 0; j < c[i]; j++){
            dp[(1 << N) - 1][0] /= (j + 1);
        }
    }
    cout << dp[(1 << N) - 1][0] << endl;
}
0