結果

問題 No.206 数の積集合を求めるクエリ
ユーザー goodbatongoodbaton
提出日時 2019-09-25 10:58:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 7,000 ms
コード長 2,547 bytes
コンパイル時間 1,183 ms
コンパイル使用メモリ 106,184 KB
実行使用メモリ 14,064 KB
最終ジャッジ日時 2023-10-21 20:10:30
合計ジャッジ時間 5,911 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 5 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 5 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 5 ms
4,348 KB
testcase_16 AC 5 ms
4,348 KB
testcase_17 AC 154 ms
14,064 KB
testcase_18 AC 139 ms
14,064 KB
testcase_19 AC 149 ms
14,064 KB
testcase_20 AC 139 ms
14,064 KB
testcase_21 AC 151 ms
14,064 KB
testcase_22 AC 144 ms
14,064 KB
testcase_23 AC 152 ms
14,064 KB
testcase_24 AC 160 ms
14,064 KB
testcase_25 AC 152 ms
14,064 KB
testcase_26 AC 155 ms
14,064 KB
testcase_27 AC 144 ms
14,064 KB
testcase_28 AC 152 ms
14,064 KB
testcase_29 AC 152 ms
14,064 KB
testcase_30 AC 145 ms
14,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <complex>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 200010

/* Fast Fourier Transform */

namespace FFT {
  using C = complex<double>;

  void DFT(vector<C> &a, bool rev = false) {
    int N = a.size(), h = 0;
    //const double M_PI = acos(-1);

    for (int i = 0; 1 << i < N; i++) h++;
    for (int i = 0; i < N; i++) {
      int j = 0;
      for (int k = 0; k < h; k++)
        j |= (i >> k & 1) << (h - 1 - k);
      if (i < j) swap(a[i], a[j]);
    }

    for (int i = 1; i < N; i *= 2) {
      for (int j = 0; j < i; j++) {
        C w = polar(1.0, M_PI / i * (rev ? -1 : 1) * j);
        for (int k = 0; k < N; k += i*2) {
          C s = a[j+k], t = a[j+k+i] * w;
          a[j+k+0] = s + t;
          a[j+k+i] = s - t;
        }
      }
    }

    if (rev) for(C &v : a) v /= N;
  }

  vector<ll> conv(const vector<int> &a, const vector<int> &b) {
    int s = 1, t = a.size() + b.size() - 1;
    while(s < t) s *= 2;

    vector<C> F(s), G(s);
    for(int i = 0; i < a.size(); i++) F[i] = a[i];
    for(int i = 0; i < b.size(); i++) G[i] = b[i];
    DFT(F); DFT(G);

    for(int i = 0; i < s; i++) F[i] *= G[i];
    DFT(F, true);

    vector<ll> res(t);
    for(int i = 0; i < t; i++) res[i] = F[i].real() + 0.5; //round
    return res;
  }
};


int main(){
  int L, M, N, Q;
  int A, B;

  scanf("%d%d%d", &L, &M, &N);

  vector<int> a(N+1), b(N+1);

  for (int i=0; i<L; i++) {
    scanf("%d", &A);
    a[A]++;
  }

  for (int i=0; i<M; i++) {
    scanf("%d", &B);
    b[N-B]++;
  }

  auto res = FFT::conv(a, b);

  scanf("%d", &Q);

  for (int i=N; i<N+Q; i++)
    printf("%lld\n", res[i]);

  return 0;
}
0