結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ei1333333ei1333333
提出日時 2017-11-17 01:39:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 7,000 ms
コード長 1,997 bytes
コンパイル時間 2,023 ms
コンパイル使用メモリ 204,128 KB
実行使用メモリ 23,112 KB
最終ジャッジ日時 2023-08-16 17:28:56
合計ジャッジ時間 5,916 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 6 ms
4,376 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 150 ms
23,016 KB
testcase_18 AC 139 ms
22,900 KB
testcase_19 AC 147 ms
22,968 KB
testcase_20 AC 140 ms
22,644 KB
testcase_21 AC 141 ms
22,588 KB
testcase_22 AC 138 ms
23,112 KB
testcase_23 AC 147 ms
23,104 KB
testcase_24 AC 158 ms
22,900 KB
testcase_25 AC 157 ms
23,072 KB
testcase_26 AC 149 ms
22,584 KB
testcase_27 AC 142 ms
22,988 KB
testcase_28 AC 150 ms
22,624 KB
testcase_29 AC 152 ms
22,964 KB
testcase_30 AC 148 ms
23,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

using int64 = long long;

namespace FastFourierTransform
{
  using C = complex< long double >;

  void DiscreteFourierTransform(vector< C > &F, bool rev)
  {
    const int N = (int) F.size();
    const double PI = (rev ? -1 : 1) * acos(-1);
    for(int i = 0, j = 1; j + 1 < N; j++) {
      for(int k = N >> 1; k > (i ^= k); k >>= 1);
      if(i > j) swap(F[i], F[j]);
    }

    C wr, w, s, t;
    for(int i = 1; i < N; i <<= 1) {
      wr = polar(1.0, PI / i);
      for(int j = 0; j < N; j += i * 2) {
        w = 1.0;
        for(int k = 0; k < i; k++) {
          s = F[j + k];
          t = C(F[j + k + i].real() * w.real() - F[j + k + i].imag() * w.imag(), F[j + k + i].real() * w.imag() + F[j + k + i].imag() * w.real());
          F[j + k] = s + t, F[j + k + i] = s - t;
          w *= wr;
        }
      }
    }
    if(rev) {
      double temp = 1.0 / N;
      for(int i = 0; i < N; i++) F[i] *= temp;
    }
  }

  vector< int64 > Multiply(const vector< int64 > &A, const vector< int64 > &B)
  {
    int sz = 1;
    while(sz < A.size() + B.size() - 1) sz <<= 1;
    vector< C > F(sz), G(sz);
    for(int i = 0; i < A.size(); i++) F[i] = A[i];
    for(int i = 0; i < B.size(); i++) G[i] = B[i];
    DiscreteFourierTransform(F, false);
    DiscreteFourierTransform(G, false);
    for(int i = 0; i < sz; i++) F[i] *= G[i];
    DiscreteFourierTransform(F, true);
    vector< int64 > X(A.size() + B.size() - 1);
    for(int i = 0; i < A.size() + B.size() - 1; i++) X[i] = F[i].real() + 0.5;
    return (X);
  }
};

int main()
{
  int N, M, P;
  scanf("%d %d %d", &N, &M, &P);
  vector< int64 > A(P), B(P);
  for(int i = 0; i < N; i++) {
    int x;
    scanf("%d", &x);
    A[x - 1] = 1;
  }
  for(int i = 0; i < M; i++) {
    int x;
    scanf("%d", &x);
    B[x - 1] = 1;
  }
  reverse(begin(B), end(B));
  auto C = FastFourierTransform::Multiply(A, B);
  int Q;
  scanf("%d", &Q);
  for(int i = 0; i < Q; i++) printf("%lld\n", C[P + i - 1]);
}
0