結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー milanis48663220milanis48663220
提出日時 2021-07-30 22:28:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,135 ms / 3,000 ms
コード長 2,460 bytes
コンパイル時間 1,235 ms
コンパイル使用メモリ 125,828 KB
実行使用メモリ 387,616 KB
最終ジャッジ日時 2023-10-14 05:43:18
合計ジャッジ時間 16,392 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 4 ms
4,624 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 5 ms
5,340 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 1,065 ms
369,664 KB
testcase_15 AC 1,097 ms
387,552 KB
testcase_16 AC 1,135 ms
387,360 KB
testcase_17 AC 1,024 ms
387,616 KB
testcase_18 AC 1,135 ms
387,368 KB
testcase_19 AC 1,025 ms
387,420 KB
testcase_20 AC 525 ms
387,420 KB
testcase_21 AC 458 ms
340,616 KB
testcase_22 AC 589 ms
380,504 KB
testcase_23 AC 1,069 ms
380,488 KB
testcase_24 AC 788 ms
380,548 KB
testcase_25 AC 529 ms
376,604 KB
testcase_26 AC 56 ms
5,624 KB
testcase_27 AC 760 ms
279,892 KB
testcase_28 AC 404 ms
119,456 KB
testcase_29 AC 398 ms
121,844 KB
testcase_30 AC 483 ms
177,024 KB
testcase_31 AC 958 ms
278,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

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


template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

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);
    for(int i = 1; i <= 9; i++) cin >> c[i];
    auto dp = vec3d<ll>(2, 1<<n, k, 0);
    dp[0][0][0] = 1;
    vector<int> pow10(n);
    pow10[0] = 1%k;
    for(int i = 1; i < n; i++){
        pow10[i] = (pow10[i-1]*10)%k;
    }
    int sum = 0;
    int mask = (1<<n)-1;
    auto popcount = [&](int s){
        int ans = 0;
        for(int i = 0; i < n; i++){
            if(s&(1<<i)) ans++;
        }
        return ans;
    };
    for(int i = 1; i <= 9; i++){
        int cur = i%2;
        int pre = cur^1;
        for(int j = 0; j < 1<<n; j++){
            for(int l = 0; l < k; l++){
                dp[cur][j][l] = 0;
            }
        }
        for(int j = 0; j < 1<<n; j++){
            if(popcount(j) != sum) continue;
            int rem = mask^j;
            vector<int> u;
            for(int s = rem; s >= 0; s = (s-1)&rem){
                if(popcount(s) == c[i]){
                    assert((j&s) == 0);
                    int nx = 0;
                    for(int l = 0; l < n; l++){
                        if(s&(1<<l)) nx += pow10[l]*i;
                    }
                    nx %= k;
                    for(int l = 0; l < k; l++){
                        dp[cur][j+s][(nx+l)%k] += dp[pre][j][l];
                    }
                }
                if(s == 0) break;
            }
        }
        sum += c[i];
    }
    cout << dp[1][mask][0] << endl;
}
0