Submission #7142996


Source Code Expand

Copy
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
#include <complex>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()

const double pi = acos(-1);
vector<complex<double>> tmp;
size_t sz = 1;

//mulが勝手に呼び出してくれる
vector<complex<double>> fft(vector<complex<double>> a, bool inv = false){
	size_t mask = sz - 1;
	size_t p = 0;
	for(size_t i = sz >> 1; i >= 1; i >>= 1){
		auto& cur = (p & 1) ? tmp : a;
		auto& nex = (p & 1) ? a : tmp;
		complex<double> e = polar(1., 2 * pi * i * (inv ? -1 : 1) / sz);
		complex<double> w = 1;
		for(size_t j = 0; j< sz; j += i){
			for(size_t k = 0; k < i; k++){
				nex[j + k] = cur[((j << 1) & mask) + k] + w * cur[(((j << 1) + i) & mask) + k];
			}
			w *= e;
		}
		p++;
	}
	if(p & 1) swap(a, tmp);
	if(inv) for(size_t i = 0; i < sz; i++) a[i] /= sz;
	return a;
}

//これを使う. a[i] = 多項式Aにおけるx^iの項の係数, b[i]も同様.
vector<ll> mul(vector<ll> a, vector<ll> b){
	size_t m = a.size() + b.size() - 1;
	sz = 1;
	while(m > sz) sz <<= 1;
	tmp.resize(sz);
	vector<complex<double>> A(sz), B(sz);
	rep(i, a.size()) A[i].real(a[i]);
	rep(i, b.size()) B[i].real(b[i]);
	A = fft(A);
	B = fft(B);
	rep(i, sz) A[i] *= B[i];
	A = fft(A, true);
	a.resize(m);
	rep(i, m) a[i] = round(A[i].real());
	return a;
}

int main(void){
	ll n; cin>>n;
	vector<ll> A,B,C;
	A.push_back(0); B.push_back(0);
	rep(i,n){
		ll a,b;cin>>a>>b;
		A.push_back(a);
		B.push_back(b);
	}
	//多項式Cの係数配列 = 多項式Aの係数配列*多項式Bの係数配列
	C = mul(A,B);
	reps(i,2*n){
		cout<<C[i]<<endl;
	}
	return 0;
}

Submission Info

Submission Time
Task C - 高速フーリエ変換
User rstoick
Language C++14 (GCC 5.4.1)
Score 100
Code Size 2291 Byte
Status
Exec Time 434 ms
Memory 19948 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 00_sample_01
All 100 / 100 00_sample_01, 01_00_01, 01_01_19, 01_02_31, 01_03_22, 01_04_31, 01_05_40, 01_06_15, 01_07_39, 01_08_28, 01_09_30, 01_10_23, 01_11_33, 01_12_11, 01_13_28, 01_14_41, 01_15_26, 01_16_49, 01_17_34, 01_18_02, 01_19_33, 01_20_29, 02_00_51254, 02_01_82431, 02_02_17056, 02_03_34866, 02_04_6779, 02_05_65534, 02_06_65535, 02_07_65536, 02_08_65537, 02_09_65538, 02_10_100000
Case Name Status Exec Time Memory
00_sample_01 1 ms 256 KB
01_00_01 1 ms 256 KB
01_01_19 1 ms 256 KB
01_02_31 1 ms 256 KB
01_03_22 1 ms 256 KB
01_04_31 1 ms 256 KB
01_05_40 1 ms 256 KB
01_06_15 1 ms 256 KB
01_07_39 1 ms 256 KB
01_08_28 1 ms 256 KB
01_09_30 1 ms 256 KB
01_10_23 1 ms 256 KB
01_11_33 1 ms 256 KB
01_12_11 1 ms 256 KB
01_13_28 1 ms 256 KB
01_14_41 1 ms 256 KB
01_15_26 1 ms 256 KB
01_16_49 1 ms 256 KB
01_17_34 1 ms 256 KB
01_18_02 1 ms 256 KB
01_19_33 1 ms 256 KB
01_20_29 1 ms 256 KB
02_00_51254 230 ms 10228 KB
02_01_82431 384 ms 19436 KB
02_02_17056 81 ms 4988 KB
02_03_34866 169 ms 9716 KB
02_04_6779 30 ms 1536 KB
02_05_65534 276 ms 10740 KB
02_06_65535 283 ms 10740 KB
02_07_65536 322 ms 18668 KB
02_08_65537 336 ms 18668 KB
02_09_65538 324 ms 18668 KB
02_10_100000 434 ms 19948 KB