結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー SSRSSSRS
提出日時 2021-07-30 21:21:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 550 ms / 3,000 ms
コード長 1,352 bytes
コンパイル時間 1,957 ms
コンパイル使用メモリ 185,052 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-14 03:10:57
合計ジャッジ時間 11,768 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 4 ms
4,352 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 60 ms
4,348 KB
testcase_06 AC 71 ms
4,352 KB
testcase_07 AC 60 ms
4,352 KB
testcase_08 AC 253 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 483 ms
4,348 KB
testcase_12 AC 518 ms
4,352 KB
testcase_13 AC 474 ms
4,348 KB
testcase_14 AC 549 ms
4,348 KB
testcase_15 AC 550 ms
4,348 KB
testcase_16 AC 459 ms
4,352 KB
testcase_17 AC 118 ms
4,352 KB
testcase_18 AC 128 ms
4,352 KB
testcase_19 AC 246 ms
4,348 KB
testcase_20 AC 113 ms
4,352 KB
testcase_21 AC 19 ms
4,352 KB
testcase_22 AC 328 ms
4,348 KB
testcase_23 AC 388 ms
4,356 KB
testcase_24 AC 476 ms
4,352 KB
testcase_25 AC 510 ms
4,356 KB
testcase_26 AC 525 ms
4,356 KB
testcase_27 AC 518 ms
4,352 KB
testcase_28 AC 474 ms
4,348 KB
testcase_29 AC 418 ms
4,348 KB
testcase_30 AC 343 ms
4,348 KB
testcase_31 AC 392 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, K;
  cin >> N >> K;
  vector<int> c(10);
  c[0] = 0;
  for (int i = 1; i < 10; i++){
    cin >> c[i];
  }
  vector<int> A;
  for (int i = 1; i < 10; i++){
    for (int j = 0; j < c[i]; j++){
      A.push_back(i);
    }
  }
  int N1 = N / 2, N2 = (N + 1) / 2;
  long long P = 1;
  for (int i = 0; i < N2; i++){
    P *= 10;
  }
  set<pair<vector<int>, vector<int>>> st;
  for (int i = 0; i < (1 << N); i++){
    if (__builtin_popcount(i) == N1){
      vector<int> B1, B2;
      for (int j = 0; j < N; j++){
        if ((i >> j & 1) == 1){
          B1.push_back(A[j]);
        } else {
          B2.push_back(A[j]);
        }
      }
      st.insert(make_pair(B1, B2));
    }
  }
  long long ans = 0;
  for (auto p : st){
    vector<int> B1 = p.first;
    vector<int> B2 = p.second;
    map<int, int> mp1;
    while (true){
      int x = 0;
      for (int i = 0; i < N1; i++){
        x = x * 10 + B1[i];
      }
      mp1[x * P % K]++;
      if (!next_permutation(B1.begin(), B1.end())){
        break;
      }
    }
    while (true){
      int x = 0;
      for (int i = 0; i < N2; i++){
        x = x * 10 + B2[i];
      }
      int y = (K - x % K) % K;
      ans += mp1[y];
      if (!next_permutation(B2.begin(), B2.end())){
        break;
      }
    }
  }
  cout << ans << endl;
}
0