結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ei1333333ei1333333
提出日時 2017-11-17 01:39:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 140 ms / 7,000 ms
コード長 1,997 bytes
コンパイル時間 2,124 ms
コンパイル使用メモリ 206,460 KB
実行使用メモリ 23,148 KB
最終ジャッジ日時 2024-05-04 01:17:06
合計ジャッジ時間 5,040 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 134 ms
23,012 KB
testcase_18 AC 122 ms
23,016 KB
testcase_19 AC 131 ms
22,876 KB
testcase_20 AC 126 ms
23,040 KB
testcase_21 AC 125 ms
22,816 KB
testcase_22 AC 126 ms
23,040 KB
testcase_23 AC 133 ms
22,928 KB
testcase_24 AC 139 ms
22,992 KB
testcase_25 AC 140 ms
23,036 KB
testcase_26 AC 130 ms
23,020 KB
testcase_27 AC 127 ms
22,912 KB
testcase_28 AC 133 ms
22,984 KB
testcase_29 AC 135 ms
23,148 KB
testcase_30 AC 132 ms
22,812 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