Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Complex{
- double re; double im;
- public Complex(double real, double imag) {
- re = real;
- im = imag;
- }
- public String toString() {
- double tRe = (double)Math.round(re * 100000) / 100000;
- double tIm = (double)Math.round(im * 100000) / 100000;
- if (tIm == 0) return tRe + "";
- if (tRe == 0) return tIm + "i";
- if (tIm < 0) return tRe + "-" + (-tIm) + "i";
- return tRe + "+" + tIm + "i";
- }
- public Complex conj() {
- return new Complex(re, -im);
- }
- public Complex plus(Complex b) {
- Complex a = this;
- double real = a.re + b.re;
- double imag = a.im + b.im;
- return new Complex(real, imag);
- }
- public Complex minus(Complex b) {
- Complex a = this;
- double real = a.re - b.re;
- double imag = a.im - b.im;
- return new Complex(real, imag);
- }
- public Complex times(Complex b) {
- Complex a = this;
- double real = a.re * b.re - a.im * b.im;
- double imag = a.re * b.im + a.im * b.re;
- return new Complex(real, imag);
- }
- public Complex times(double alpha) {
- return new Complex(alpha * re, alpha * im);
- }
- public Complex reciprocal() {
- double scale = re*re + im*im;
- return new Complex(re / scale, -im / scale);
- }
- public Complex divides(Complex b) {
- Complex a = this;
- return a.times(b.reciprocal());
- }
- public Complex exp() {
- return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
- }
- public Complex pow(int k) {
- Complex c = new Complex(1,0);
- for (int i = 0; i <k ; i++) {
- c = c.times(this);
- }
- return c;
- }
- }
- public class Izziv8{
- public static int N, nPos;
- public static Complex coeffA[];
- public static Complex coeffB[];
- public static void read(){
- Scanner sc = new Scanner(System.in);
- N = sc.nextInt();
- int j = N;
- while((int)Math.pow(2,j) < 2*N) j++;
- nPos = (int)Math.pow(2,j);
- coeffA = new Complex[nPos];
- coeffB = new Complex[nPos];
- for(int i=0;i<N;i++) coeffA[i]=new Complex(sc.nextDouble(),0);
- for(int i=0;i<N;i++) coeffB[i]=new Complex(sc.nextDouble(),0);
- for(int i=N;i<nPos;i++) coeffA[i]=coeffB[i]=new Complex(0,0);
- sc.close();
- }
- public static void solve(){
- Complex[] p = FFT(coeffA);
- Complex[] s = FFT(coeffB);
- Complex[] c = new Complex[nPos];
- for(int i=0;i<nPos;i++) c[i] = p[i].times(s[i]);
- Complex[] y = iFFT2(c);
- Complex _n = new Complex(nPos,0);
- for(int i=0;i<nPos;i++) y[i] = y[i].divides(_n);
- for(int i=0;i<nPos;i++) System.out.printf("%s ",y[i].toString());
- }
- public static Complex[] FFT(Complex[] a){
- int n = a.length;
- if(n == 1) return a;
- Complex[] pEven = new Complex[n/2];
- for(int i=0;i<n/2;i++) pEven[i] = a[i*2];
- Complex[] ys = FFT(pEven);
- Complex[] pOdd = new Complex[n/2];
- for(int i=0;i<n/2;i++) pOdd[i] = a[i*2+1];
- Complex[] yl = FFT(pOdd);
- Complex w = new Complex(Math.cos(2*Math.PI/n),Math.sin(2*Math.PI/n));
- Complex wk = new Complex(1,0);
- Complex[] y = new Complex[n];
- for(int i=0;i<n/2;i++){
- y[i] = ys[i].plus(wk.times(yl[i]));
- y[i+n/2] = ys[i].minus(wk.times(yl[i]));
- wk = wk.times(w);
- }
- for(int i=0;i<n;i++) System.out.printf("%s ",y[i].toString());
- System.out.printf("\n");
- return y;
- }
- public static Complex[] iFFT2(Complex [] a){
- int n = a.length;
- if(n==1) return a;
- Complex[] pOdd = new Complex[n/2];
- Complex[] pEven = new Complex[n/2];
- for(int i=0;i<n/2;i++) pEven[i] = a[i*2];
- for(int i=0;i<n/2;i++) pOdd[i] = a[i*2+1];
- Complex[] ys = FFT(pEven);
- Complex[] yl = FFT(pOdd);
- Complex w = new Complex(Math.cos(2*Math.PI/n), Math.sin(2*Math.PI/n));
- w = w.reciprocal();
- Complex wk = new Complex(1,0);
- Complex[] y = new Complex[n];
- for(int i=0;i<n/2;i++){
- y[i] = ys[i].plus(wk.times(yl[i]));
- y[i+n/2] = ys[i].minus(wk.times(yl[i]));
- wk = wk.times(w);
- }
- for(int i=0;i<n;i++) System.out.printf("%s ",y[i].toString());
- System.out.printf("\n");
- return y;
- }
- public static void main(String[] args) {
- read();
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement