結果

問題 No.2581 [Cherry Anniversary 3] 28輪の桜のブーケ
ユーザー ochiaigawaochiaigawa
提出日時 2023-12-19 11:15:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 3,000 ms
コード長 1,813 bytes
コンパイル時間 3,341 ms
コンパイル使用メモリ 213,564 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-19 11:15:55
合計ジャッジ時間 5,911 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,676 KB
testcase_01 AC 8 ms
6,676 KB
testcase_02 AC 13 ms
6,676 KB
testcase_03 AC 56 ms
6,676 KB
testcase_04 AC 33 ms
6,676 KB
testcase_05 AC 9 ms
6,676 KB
testcase_06 AC 46 ms
6,676 KB
testcase_07 AC 44 ms
6,676 KB
testcase_08 AC 14 ms
6,676 KB
testcase_09 AC 30 ms
6,676 KB
testcase_10 AC 42 ms
6,676 KB
testcase_11 AC 13 ms
6,676 KB
testcase_12 AC 55 ms
6,676 KB
testcase_13 AC 63 ms
6,676 KB
testcase_14 AC 54 ms
6,676 KB
testcase_15 AC 48 ms
6,676 KB
testcase_16 AC 52 ms
6,676 KB
testcase_17 AC 51 ms
6,676 KB
testcase_18 AC 61 ms
6,676 KB
testcase_19 AC 65 ms
6,676 KB
testcase_20 AC 64 ms
6,676 KB
testcase_21 AC 63 ms
6,676 KB
testcase_22 AC 166 ms
6,676 KB
testcase_23 AC 172 ms
6,676 KB
testcase_24 AC 163 ms
6,676 KB
testcase_25 AC 159 ms
6,676 KB
testcase_26 AC 167 ms
6,676 KB
testcase_27 AC 7 ms
6,676 KB
testcase_28 AC 7 ms
6,676 KB
testcase_29 AC 132 ms
6,676 KB
testcase_30 AC 23 ms
6,676 KB
testcase_31 AC 54 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
const int N = 28;
const int n = 14;
int main() {
  cin.tie(0); cout.tie(0);
  ios::sync_with_stdio(false);
  int M;
  cin >> M;
  vector<int> G(N), H(N);
  for(int i = 0; i < N; i++) {
    cin >> G[i];
  }
  for(int i = 0; i < N; i++) {
    cin >> H[i];
  }
  vector<vector<int>> A(n + 1);
  for(int bit = 0; bit < 1 << n; bit++) {
    int c = 0;
    int S = 0;
    for(int i = 0; i < n; i++) {
      if(bit >> i & 1) {
        c++;
        S = (S + G[i]) % M;
      } else {
        S = (S + H[i]) % M;
      }
    }
    A[c].emplace_back(S);
  }
  for(int i = 0; i <= n; i++) {
    sort(A[i].begin(), A[i].end());
  }
  vector<vector<int>> B(n + 1);
  for(int bit = 0; bit < 1 << n; bit++) {
    int c = 0;
    int S = 0;
    for(int i = 0; i < n; i++) {
      if(bit >> i & 1) {
        c++;
        S = (S + G[i + n]) % M;
      } else {
        S = (S + H[i + n]) % M;
      }
    }
    B[c].emplace_back(S);
  }
  for(int i = 0; i <= n; i++) {
    sort(B[i].begin(), B[i].end());
  }
  int Q;
  cin >> Q;
  while(Q--) {
    int K, X;
    cin >> K >> X;
    int ans = 0;
    for(int i = 0; i <= n; i++) {
      if(K - i < 0 || K - i > n) {
        continue;
      }
      for(int x : A[i]) {
        if(x > X) {
          // [0, M - 1 - x]
          ans += lower_bound(B[K - i].begin(), B[K - i].end(), M - x) - B[K - i].begin();
          // [M - x + X, M - 1]
          ans += B[K - i].end() - lower_bound(B[K - i].begin(), B[K - i].end(), M - x + X);
        } else {
          // [X - x, M - 1 - x]
          ans += lower_bound(B[K - i].begin(), B[K - i].end(), M - x) - lower_bound(B[K - i].begin(), B[K - i].end(), X - x);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
0