結果

問題 No.206 数の積集合を求めるクエリ
ユーザー leaf_1415leaf_1415
提出日時 2019-01-11 02:48:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 539 ms / 7,000 ms
コード長 1,824 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 70,996 KB
実行使用メモリ 47,604 KB
最終ジャッジ日時 2023-08-16 14:18:00
合計ジャッジ時間 16,262 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 363 ms
44,408 KB
testcase_01 AC 360 ms
44,484 KB
testcase_02 AC 361 ms
44,508 KB
testcase_03 AC 361 ms
44,300 KB
testcase_04 AC 362 ms
44,276 KB
testcase_05 AC 363 ms
44,504 KB
testcase_06 AC 362 ms
44,360 KB
testcase_07 AC 362 ms
44,364 KB
testcase_08 AC 358 ms
44,568 KB
testcase_09 AC 362 ms
44,584 KB
testcase_10 AC 359 ms
44,344 KB
testcase_11 AC 357 ms
44,296 KB
testcase_12 AC 368 ms
44,504 KB
testcase_13 AC 366 ms
44,412 KB
testcase_14 AC 369 ms
44,392 KB
testcase_15 AC 367 ms
44,476 KB
testcase_16 AC 368 ms
44,400 KB
testcase_17 AC 414 ms
47,604 KB
testcase_18 AC 392 ms
46,608 KB
testcase_19 AC 410 ms
47,300 KB
testcase_20 AC 388 ms
45,996 KB
testcase_21 AC 396 ms
46,580 KB
testcase_22 AC 393 ms
46,236 KB
testcase_23 AC 411 ms
47,368 KB
testcase_24 AC 539 ms
47,388 KB
testcase_25 AC 537 ms
47,152 KB
testcase_26 AC 516 ms
46,836 KB
testcase_27 AC 474 ms
45,696 KB
testcase_28 AC 523 ms
46,480 KB
testcase_29 AC 526 ms
46,600 KB
testcase_30 AC 521 ms
45,776 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