Advertisement
daskalot

Untitled

Apr 24th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Complex{
  3. double re; double im;
  4. public Complex(double real, double imag) {
  5. re = real;
  6. im = imag;
  7. }
  8. public String toString() {
  9. double tRe = (double)Math.round(re * 100000) / 100000;
  10. double tIm = (double)Math.round(im * 100000) / 100000;
  11. if (tIm == 0) return tRe + "";
  12. if (tRe == 0) return tIm + "i";
  13. if (tIm < 0) return tRe + "-" + (-tIm) + "i";
  14. return tRe + "+" + tIm + "i";
  15. }
  16. public Complex conj() {
  17. return new Complex(re, -im);
  18. }
  19. public Complex plus(Complex b) {
  20. Complex a = this;
  21. double real = a.re + b.re;
  22. double imag = a.im + b.im;
  23. return new Complex(real, imag);
  24. }
  25. public Complex minus(Complex b) {
  26. Complex a = this;
  27. double real = a.re - b.re;
  28. double imag = a.im - b.im;
  29. return new Complex(real, imag);
  30. }
  31. public Complex times(Complex b) {
  32. Complex a = this;
  33. double real = a.re * b.re - a.im * b.im;
  34. double imag = a.re * b.im + a.im * b.re;
  35. return new Complex(real, imag);
  36. }
  37. public Complex times(double alpha) {
  38. return new Complex(alpha * re, alpha * im);
  39. }
  40. public Complex reciprocal() {
  41. double scale = re*re + im*im;
  42. return new Complex(re / scale, -im / scale);
  43. }
  44. public Complex divides(Complex b) {
  45. Complex a = this;
  46. return a.times(b.reciprocal());
  47. }
  48. public Complex exp() {
  49. return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
  50. }
  51. public Complex pow(int k) {
  52. Complex c = new Complex(1,0);
  53. for (int i = 0; i <k ; i++) {
  54. c = c.times(this);
  55. }
  56. return c;
  57. }
  58. }
  59. public class Izziv8{
  60. public static int N, nPos;
  61. public static Complex coeffA[];
  62. public static Complex coeffB[];
  63. public static void read(){
  64. Scanner sc = new Scanner(System.in);
  65. N = sc.nextInt();
  66. int j = N;
  67. while((int)Math.pow(2,j) < 2*N) j++;
  68. nPos = (int)Math.pow(2,j);
  69. coeffA = new Complex[nPos];
  70. coeffB = new Complex[nPos];
  71. for(int i=0;i<N;i++) coeffA[i]=new Complex(sc.nextDouble(),0);
  72. for(int i=0;i<N;i++) coeffB[i]=new Complex(sc.nextDouble(),0);
  73. for(int i=N;i<nPos;i++) coeffA[i]=coeffB[i]=new Complex(0,0);
  74. sc.close();
  75. }
  76. public static void solve(){
  77. Complex[] p = FFT(coeffA);
  78. Complex[] s = FFT(coeffB);
  79. Complex[] c = new Complex[nPos];
  80. for(int i=0;i<nPos;i++) c[i] = p[i].times(s[i]);
  81. Complex[] y = iFFT2(c);
  82. Complex _n = new Complex(nPos,0);
  83. for(int i=0;i<nPos;i++) y[i] = y[i].divides(_n);
  84. for(int i=0;i<nPos;i++) System.out.printf("%s ",y[i].toString());
  85. }
  86. public static Complex[] FFT(Complex[] a){
  87. int n = a.length;
  88. if(n == 1) return a;
  89. Complex[] pEven = new Complex[n/2];
  90. for(int i=0;i<n/2;i++) pEven[i] = a[i*2];
  91. Complex[] ys = FFT(pEven);
  92. Complex[] pOdd = new Complex[n/2];
  93. for(int i=0;i<n/2;i++) pOdd[i] = a[i*2+1];
  94. Complex[] yl = FFT(pOdd);
  95. Complex w = new Complex(Math.cos(2*Math.PI/n),Math.sin(2*Math.PI/n));
  96. Complex wk = new Complex(1,0);
  97. Complex[] y = new Complex[n];
  98. for(int i=0;i<n/2;i++){
  99. y[i] = ys[i].plus(wk.times(yl[i]));
  100. y[i+n/2] = ys[i].minus(wk.times(yl[i]));
  101. wk = wk.times(w);
  102. }
  103. for(int i=0;i<n;i++) System.out.printf("%s ",y[i].toString());
  104. System.out.printf("\n");
  105. return y;
  106. }
  107. public static Complex[] iFFT2(Complex [] a){
  108. int n = a.length;
  109. if(n==1) return a;
  110. Complex[] pOdd = new Complex[n/2];
  111. Complex[] pEven = new Complex[n/2];
  112. for(int i=0;i<n/2;i++) pEven[i] = a[i*2];
  113. for(int i=0;i<n/2;i++) pOdd[i] = a[i*2+1];
  114. Complex[] ys = FFT(pEven);
  115. Complex[] yl = FFT(pOdd);
  116. Complex w = new Complex(Math.cos(2*Math.PI/n), Math.sin(2*Math.PI/n));
  117. w = w.reciprocal();
  118. Complex wk = new Complex(1,0);
  119. Complex[] y = new Complex[n];
  120. for(int i=0;i<n/2;i++){
  121. y[i] = ys[i].plus(wk.times(yl[i]));
  122. y[i+n/2] = ys[i].minus(wk.times(yl[i]));
  123. wk = wk.times(w);
  124. }
  125. for(int i=0;i<n;i++) System.out.printf("%s ",y[i].toString());
  126. System.out.printf("\n");
  127. return y;
  128. }
  129. public static void main(String[] args) {
  130. read();
  131. solve();
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement