結果

問題 No.206 数の積集合を求めるクエリ
ユーザー leaf_1415leaf_1415
提出日時 2019-01-11 02:48:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 493 ms / 7,000 ms
コード長 1,824 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 71,152 KB
実行使用メモリ 47,516 KB
最終ジャッジ日時 2024-05-03 22:37:37
合計ジャッジ時間 13,434 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
44,300 KB
testcase_01 AC 307 ms
44,276 KB
testcase_02 AC 304 ms
44,400 KB
testcase_03 AC 308 ms
44,400 KB
testcase_04 AC 306 ms
44,404 KB
testcase_05 AC 302 ms
44,284 KB
testcase_06 AC 306 ms
44,472 KB
testcase_07 AC 306 ms
44,332 KB
testcase_08 AC 305 ms
44,484 KB
testcase_09 AC 304 ms
44,480 KB
testcase_10 AC 306 ms
44,280 KB
testcase_11 AC 303 ms
44,276 KB
testcase_12 AC 310 ms
44,628 KB
testcase_13 AC 309 ms
44,468 KB
testcase_14 AC 310 ms
44,336 KB
testcase_15 AC 309 ms
44,580 KB
testcase_16 AC 307 ms
44,456 KB
testcase_17 AC 364 ms
47,512 KB
testcase_18 AC 334 ms
46,864 KB
testcase_19 AC 354 ms
47,308 KB
testcase_20 AC 336 ms
45,832 KB
testcase_21 AC 342 ms
46,420 KB
testcase_22 AC 338 ms
46,488 KB
testcase_23 AC 360 ms
47,400 KB
testcase_24 AC 493 ms
47,516 KB
testcase_25 AC 489 ms
47,092 KB
testcase_26 AC 467 ms
46,608 KB
testcase_27 AC 426 ms
45,808 KB
testcase_28 AC 483 ms
46,376 KB
testcase_29 AC 473 ms
46,472 KB
testcase_30 AC 464 ms
45,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <complex>
#include <vector>
#define llint long long 
#define PI 3.141592653589793238

using namespace std;
typedef complex<double> Complex;

void DFT(vector<Complex> &f, int n, vector<Complex> &F)
{
	F.resize(n);
	if(n == 1){
		F[0] = f[0];
		return;
	}
	
	vector<Complex> f0(n/2), f1(n/2), F0, F1;
	
	for(int i = 0; i < n/2; i++) f0[i] = f[i*2];
	DFT(f0, n/2, F0);
	for(int i = 0; i < n/2; i++) f1[i] = f[i*2+1];
	DFT(f1, n/2, F1);
	
	Complex z = Complex(cos(2*PI/n), sin(2*PI/n)), x = Complex(1, 0);
	for(int i = 0; i < n; i++){
		F[i] = F0[i%(n/2)] + x * F1[i%(n/2)];
		x *= z;
	}
}

//The answer must be divided by n.
void IDFT(vector<Complex> &f, int n, vector<Complex> &F)
{
	F.resize(n);
	if(n == 1){
		F[0] = f[0];
		return;
	}
	
	vector<Complex> f0(n/2), f1(n/2), F0, F1;
	
	for(int i = 0; i < n/2; i++) f0[i] = f[i*2];
	IDFT(f0, n/2, F0);
	for(int i = 0; i < n/2; i++) f1[i] = f[i*2+1];
	IDFT(f1, n/2, F1);
	
	Complex z = Complex(cos(2*PI/n), -sin(2*PI/n)), x = Complex(1, 0);
	for(int i = 0; i < n; i++){
		F[i] = F0[i%(n/2)] + x * F1[i%(n/2)];
		x *= z;
	}
}

llint round(Complex c){
	return (int)(c.real()+0.5);
}

llint L, M, N, Q;
llint A[100005], B[100005];
llint a[100005], b[100005];
const llint S = 1<<18;
vector<Complex> f(S), g(S), h, F, G, H(S);

int main(void)
{
	cin >> L >> M >> N;
	for(int i = 0; i < L; i++) cin >> A[i];
	for(int i = 0; i < M; i++) cin >> B[i];
	cin >> Q;
	
	for(int i = 0; i < L; i++) a[A[i]] = 1;
	for(int i = 0; i < M; i++) b[B[i]] = 1;
	
	for(int i = 1; i <= 100000; i++) f[100001-i] = a[i];
	for(int i = 1; i <= 100000; i++) g[i] = b[i];
	DFT(f, S, F), DFT(g, S, G);
	for(int i = 0; i < S; i++) H[i] = F[i]*G[i];
	IDFT(H, S, h);
	for(int i = 0; i < S; i++) h[i] /= S;
	for(int i = 0; i < Q; i++) cout << round(h[100001-i]) << endl;
	
	return 0;
}
0