結果

問題 No.206 数の積集合を求めるクエリ
ユーザー utsumi_pgutsumi_pg
提出日時 2021-02-12 06:25:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 580 ms / 7,000 ms
コード長 1,675 bytes
コンパイル時間 1,000 ms
コンパイル使用メモリ 84,968 KB
実行使用メモリ 33,408 KB
最終ジャッジ日時 2023-09-25 21:05:11
合計ジャッジ時間 10,759 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 16 ms
4,380 KB
testcase_07 AC 16 ms
4,376 KB
testcase_08 AC 16 ms
4,376 KB
testcase_09 AC 16 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 17 ms
4,380 KB
testcase_13 AC 16 ms
4,380 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 16 ms
4,380 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 567 ms
33,276 KB
testcase_18 AC 559 ms
33,388 KB
testcase_19 AC 566 ms
33,408 KB
testcase_20 AC 551 ms
33,376 KB
testcase_21 AC 559 ms
33,332 KB
testcase_22 AC 559 ms
33,356 KB
testcase_23 AC 567 ms
33,356 KB
testcase_24 AC 580 ms
33,372 KB
testcase_25 AC 575 ms
33,268 KB
testcase_26 AC 568 ms
33,408 KB
testcase_27 AC 562 ms
33,272 KB
testcase_28 AC 571 ms
33,340 KB
testcase_29 AC 568 ms
33,280 KB
testcase_30 AC 564 ms
33,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <complex>
#include <cmath>
using namespace std;
 
int L, M, N;
int Q;
 
vector<complex<double>> DFT(vector<complex<double>> f, bool inverse){
	if(f.size() == 1) return f;
	vector<complex<double>> va, vb;
	for(int i = 0; i < f.size(); i++){
		if(i % 2 == 0) va.push_back(f[i]);
		else vb.push_back(f[i]);
	}
	va = DFT(va, inverse);
	vb = DFT(vb, inverse);
	double c = inverse ? 1.0 : -1.0;
	complex<double> cur = complex<double>(1.0, 0.0);
	complex<double> zeta = polar(1.0, c * acos(0.0) * 4.0 / (double)f.size());
	vector<complex<double>> res;
	for(int i = 0; i < f.size(); i++){
		res.push_back(va[i % (f.size() / 2)] + cur * vb[i % (f.size() / 2)]);
		cur *= zeta;
	}
	return res;
 
}
 
vector<double> FFT(vector<double> f, vector<double> g){
	vector<complex<double>> cf, cg;
	int n = 1;
	for(;;){
		if(n >= f.size() + g.size()) break;
		n *= 2;
	}
	cf.resize(n);
	cg.resize(n);
	for(int i = 0; i < f.size(); i++) cf[i] = complex<double>(f[i], 0.0);
	for(int i = 0; i < g.size(); i++) cg[i] = complex<double>(g[i], 0.0);
	cf = DFT(cf, true);
	cg = DFT(cg, true);
	for(int i = 0; i < n; i++) cf[i] *= cg[i];
	cf = DFT(cf, false);
	vector<double> res;
	for(int i = 0; i < n; i++) res.push_back(cf[i].real() / (double)n);
	return res;
}
 
int main(){
	scanf("%d %d %d", &L, &M, &N);
	vector<double> va(N), vb(N);
	for(int i = 0; i < L; i++){
		int a;
		scanf("%d", &a);
		a--;
		va[a] += 1.0;
	}
	for(int i = 0; i < M; i++){
		int a;
		scanf("%d", &a);
		a--;
		vb[N - a - 1] += 1.0;
	}
	vector<double> c = FFT(va, vb);
	scanf("%d", &Q);
	for(int i = 0; i < Q; i++) printf("%d\n", (int)floor(c[i + N - 1] + 0.0001));
	return 0;
}
0