結果

問題 No.206 数の積集合を求めるクエリ
ユーザー saksak
提出日時 2020-08-02 05:11:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 666 ms / 7,000 ms
コード長 2,828 bytes
コンパイル時間 2,381 ms
コンパイル使用メモリ 213,100 KB
実行使用メモリ 57,464 KB
最終ジャッジ日時 2023-09-24 23:17:04
合計ジャッジ時間 12,309 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 13 ms
5,152 KB
testcase_07 AC 13 ms
5,164 KB
testcase_08 AC 14 ms
5,184 KB
testcase_09 AC 13 ms
5,100 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 18 ms
5,316 KB
testcase_13 AC 17 ms
5,160 KB
testcase_14 AC 18 ms
5,232 KB
testcase_15 AC 17 ms
5,120 KB
testcase_16 AC 17 ms
5,144 KB
testcase_17 AC 538 ms
57,232 KB
testcase_18 AC 502 ms
56,460 KB
testcase_19 AC 518 ms
57,464 KB
testcase_20 AC 491 ms
56,512 KB
testcase_21 AC 501 ms
56,644 KB
testcase_22 AC 497 ms
56,796 KB
testcase_23 AC 519 ms
57,256 KB
testcase_24 AC 666 ms
57,240 KB
testcase_25 AC 650 ms
57,312 KB
testcase_26 AC 620 ms
56,588 KB
testcase_27 AC 587 ms
56,344 KB
testcase_28 AC 640 ms
56,860 KB
testcase_29 AC 635 ms
56,648 KB
testcase_30 AC 627 ms
56,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(int x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { int d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

vector<complex<double>> theta;

void set_theta(ll x) { theta.resize(x); double rad = 2*M_PI / x; rep(i,x) { theta[i].real(cos(rad*i)); theta[i].imag(sin(rad*i)); } }
vector<complex<double>> calc_theta(vector<complex<double>> vec) {
  ll l = vec.size(), s = theta.size();
  if (l==1) return vec;
  vector<complex<double>> odd(l/2), even(l/2); rep(i,l) { if (i%2==0) even[i/2] = vec[i]; else odd[i/2] = vec[i]; }
  vector<complex<double>> otheta = calc_theta(odd), etheta = calc_theta(even);
  vector<complex<double>> result(l); rep(i,l) result[i] = theta[s/l*i]*otheta[i%(l/2)]+etheta[i%(l/2)];
  return result;
}

vector<ll> FFT(vector<double> v1, vector<double> v2) {
  ll s = 1; while (s<v1.size()+v2.size()) s *= 2; set_theta(s);
  vector<complex<double>> vec1(s), vec2(s);
  rep(i,v1.size()) vec1[i].real(v1[i]);
  rep(i,v2.size()) vec2[i].real(v2[i]);
  vector<complex<double>> f1 = calc_theta(vec1), f2 = calc_theta(vec2);
  vector<complex<double>> fres(s); rep(i,s) fres[i] = f1[i]*f2[i];
  vector<complex<double>> rtmp = calc_theta(fres);
  vector<ll> result(s); rep(i,s) result[i] = round((rtmp[(s-i)%s].real())/s);
  return result;
}

// ----------------------------------------------------------------------
// ----------------------------------------------------------------------

int main() {
  ll L, M, N; cin >> L >> M >> N; N++;
  ll A[L]; rep(i,L) cin >> A[i]; sort(A,A+L);
  ll B[M]; rep(i,M) cin >> B[i]; sort(B,B+M);
  ll Q; cin >> Q;

  ll p2 = 1; while (N>p2) p2 *= 2;
  set_theta(p2*4);
  vector<double> v1(N); rep(i,L) v1[A[i]] = 1;
  vector<double> v2(N); rep(i,M) v2[N-B[i]] = 1;
  vector<ll> v12 = FFT(v1,v2);
  // debug(all(v1)); debug(all(v2));
  // debug(all(v12));

  rep(i,Q) cout << v12[N+i] << endl;
  return 0;
}
0