Submission #421613


Source Code Expand

#include <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;

typedef long long ll;typedef unsigned long long ull;typedef long double ld;

#define ALL(c) c.begin(),c.end()
template<class T> bool IN(T l,T v,T r){return l<=v && v <r;}
template<class T> void UNIQUE(T v){v.erase(unique(ALL(v)),v.end());}
//debug
#define DUMP(x) cerr << #x <<" = " << (x)
#define LINE() cerr<< " (L" << __LINE__ << ")"

struct range{
	struct Iter{
		int v,step;
		Iter& operator++(){v+=step;return *this;}
		bool operator!=(Iter& itr){return v<itr.v;}
		int& operator*(){return v;}
	};
	Iter i, n;
	range(int i, int n,int step):i({i,step}), n({n,step}){}
	range(int i, int n):range(i,n,1){}
	range(int n):range(0,n){}
	Iter& begin(){return i;}
	Iter& end(){return n;}
};
struct rrange{
	struct Iter{
		int v,step;
		Iter& operator++(){v-=step;return *this;}
		bool operator!=(Iter& itr){return v>itr.v;}
		int& operator*(){return v;}
	};
	Iter i, n;
	rrange(int i, int n,int step):i({i-1,step}), n({n-1,step}){}
	rrange(int i, int n):rrange(i,n,1){}
	rrange(int n) :rrange(0,n){}
	Iter& begin(){return n;}
	Iter& end(){return i;}
};

//input
template<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}
template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}
template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}
template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}
template<typename T> istream& operator >> (istream& is,vector<T>& as){for(int i:range(as.size()))is >>as[i];return is;}
//output
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<" "<<p.second;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<" "<<get<1>(t);}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);}
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){for(int i:range(as.size())){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){for(int i:range(as.size())){if(i!=0)os<<endl; os<<as[i];}return os;}

// values
template<typename T> inline T INF(){assert(false);};
template<> inline int INF<int>(){return 1<<28;};
template<> inline ll INF<ll>(){return 1LL<<58;};
template<> inline double INF<double>(){return 1e16;};
template<> inline long double INF<long double>(){return 1e16;};

template<class T> inline T EPS(){assert(false);};
template<> inline int EPS<int>(){return 1;};
template<> inline ll EPS<ll>(){return 1LL;};
template<> inline double EPS<double>(){return 1e-8;};
template<> inline long double EPS<long double>(){return 1e-8;};

// min{2^r | n < 2^r}
template<typename T> T upper_pow2(T n){ T res=1;while(res<n)res<<=1;return res;}
// max{d | 2^d  <= n}
template<typename T> T msb(T n){ int d=63;while((1LL<<d)>n)d--;return d;}

template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}



class FFT{
public:
	using D=double;
	template<class D> using C=complex<D>;
	const C<D> I = C<D>(0, 1);

	vector<C<D>> tmp=vector<C<D>>(500000);
	
	// O(nlogn)
	// simple code
	void fft(vector<C<D>>& f,int l,int N,int s,D th){
		if(N==1) return;
		int s2=s*2, N2=N/2;
		fft(f,l,N2,s2,th), fft(f,l+s,N2,s2,th);

		for(int i:range(N2)){
			C<D> lv =f[l+s2*i], rv = exp(i*s*th*I) * f[l+s+s2*i];
			tmp[i] = lv + rv, tmp[i+N2] = lv - rv;
		}
		for(int i:range(N)) f[l+s*i] = tmp[i];
	}

	void fft(vector<C<D>>& f,D th){
		fft(f,0,f.size(),1,th);
	}

	//O(nlogn)
	// [s_1,...,s_n]
	// s_k=Σ(0<=i<=k)a_i*b_(k-i) 
	void convolution(vector<C<D>>& as,vector<C<D>>& bs){
		int n = 1, vwn = as.size() + bs.size() - 1;
		while(n < vwn) n <<= 1;
		as.resize(n), bs.resize(n);
		fft(as,2*M_PI/n); fft(bs,2*M_PI/n);
		for(int i:range(n)) as[i] *= bs[i];
		fft(as,-2*M_PI/n);
		for(int i:range(n)) as[i] /= n;
	}

	vector<D> convolution(const vector<D>& as,const vector<D>& bs){
		vector<C<D>> acs(as.size()),bcs(bs.size());
		for(int i:range(as.size()))acs[i]=as[i];
		for(int i:range(as.size()))bcs[i]=bs[i];
		convolution(acs,bcs);
		vector<D> res(acs.size());
		for(int i:range(acs.size()))res[i]=acs[i].real();
		return res;
	}
};

class Main{
	public:

	void run(){
		int N;cin >> N;
		vector<double> as(N+1),bs(N+1);
		for(int i:range(1,N+1)){
			int a,b;cin >> a >> b;
			as[i]=a;
			bs[i]=b;
		}
		FFT fft;
		as = fft.convolution(as,bs);
		for(int i:range(1,2*N+1)) cout << ll(round(as[i])) <<endl;
	}
};

int main(){
	cout <<fixed<<setprecision(20);
	cin.tie(0);
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}

Submission Info

Submission Time
Task C - 高速フーリエ変換
User shimomire
Language C++11 (GCC 4.9.2)
Score 100
Code Size 6113 Byte
Status AC
Exec Time 1605 ms
Memory 20464 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 33
Set Name Test Cases
Sample 00_sample_01
All 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 AC 42 ms 8612 KB
01_00_01 AC 41 ms 8728 KB
01_01_19 AC 41 ms 8612 KB
01_02_31 AC 42 ms 8612 KB
01_03_22 AC 45 ms 8632 KB
01_04_31 AC 45 ms 8616 KB
01_05_40 AC 43 ms 8616 KB
01_06_15 AC 45 ms 8596 KB
01_07_39 AC 42 ms 8564 KB
01_08_28 AC 42 ms 8604 KB
01_09_30 AC 42 ms 8616 KB
01_10_23 AC 40 ms 8604 KB
01_11_33 AC 42 ms 8616 KB
01_12_11 AC 42 ms 8612 KB
01_13_28 AC 42 ms 8608 KB
01_14_41 AC 42 ms 8604 KB
01_15_26 AC 42 ms 8604 KB
01_16_49 AC 45 ms 8608 KB
01_17_34 AC 43 ms 8612 KB
01_18_02 AC 43 ms 8604 KB
01_19_33 AC 42 ms 8612 KB
01_20_29 AC 42 ms 8608 KB
02_00_51254 AC 695 ms 14544 KB
02_01_82431 AC 1464 ms 20112 KB
02_02_17056 AC 347 ms 11476 KB
02_03_34866 AC 620 ms 14296 KB
02_04_6779 AC 116 ms 9372 KB
02_05_65534 AC 831 ms 14764 KB
02_06_65535 AC 875 ms 14756 KB
02_07_65536 AC 1283 ms 19860 KB
02_08_65537 AC 1281 ms 19860 KB
02_09_65538 AC 1331 ms 19864 KB
02_10_100000 AC 1605 ms 20464 KB