include stdlib include stdio include math ifndef M_PI define M_PI 1415

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.141592654
#endif
typedef struct complex {
double r, m;
} complex;
complex add(complex a, complex b) {
a.r += b.r;
a.m += b.m;
return a;
}
complex sub(complex a, complex b) {
a.r -= b.r;
a.m -= b.m;
return a;
}
complex mul(complex a, complex b) {
complex result;
result.r = a.r*b.r - a.m*b.m;
result.m = a.r*b.m + a.m*b.r;
return result;
}
complex* fft (complex* a, int n, int type) {
complex *a0, *a1, *y0, *y1, *y, w, wn, t;
int i;
if (n == 1) {
return a;
}
wn.r = cos(2*M_PI/n);
wn.m = type*sin(2*M_PI/n);
w.r = 1;
w.m = 0;
a0 = (complex*)malloc(sizeof(complex)*(n/2));
a1 = (complex*)malloc(sizeof(complex)*(n/2));
y = (complex*)malloc(sizeof(complex)*n);
/*
for (k = 0; k < n_2; k++) {
a_even[k] = a[k<<1];
a_odd[k] = a[(k<<1)+1];
}
*/
for (i = 0; i<n/2; i++) {
a0[i] = a[i*2];
a1[i] = a[i*2 + 1];
printf("%d, %d\n", (int)a0[i].r, (int)a1[i].r);
}
// free (a);
y0 = fft(a0, n/2, type);
y1 = fft(a1, n/2, type);
for (i=0; i<n/2; i++) {
t = mul(w, y1[i]);
y[i] = add(y0[i], t);
y[i+n/2] = sub(y0[i], t);
w = mul(w,wn);
}
//free(a0);
//free(a1);
free(y0);
free(y1);
return y;
}
int main() {
int i, n1, n2, n;
complex *a, *b, *y1, *y2;
printf ("Степень первого многочлена -> ");
scanf ("%d", &n1);
printf ("Степень второго многочлена -> ");
scanf ("%d", &n2);
for (n=2; n<=n1+n2; n*=2);
a = (complex*)malloc(sizeof(complex)*n);
b = (complex*)malloc(sizeof(complex)*n);
for (i=0; i<=n1; i++) {
printf("a[%d] -> ", i);
scanf("%lf", &(a[i].r));
a[i].m = 0;
}
for (i = n1+1; i<n; i++) {
a[i].r = 0;
a[i].m = 0;
}
for (i=0; i<=n2; i++){
printf("b[%d] -> ", i);
scanf("%lf", &(b[i].r));
b[i].m = 0;
}
for (i = n2+1; i<n; i++) {
b[i].r = 0;
b[i].m = 0;
}
printf("\n");
y1 = fft(a, n, 1);
y2 = fft(b, n, 1);
for (i=0; i<n; i++) {
y1[i] = mul(y1[i], y2[i]);
}
a = fft (y1,n,-1);
i = n - 1;
while (rint(a[i].r/n)==0.0) {
i--;
}
while (i >= 0) {
printf("c[%d] = %.0lf\n", i, (a[i].r/n));
i--;
}
printf("\n");
free(y1);
free(y2);
free(a);
free(b);
return 0;
}