forked from cp-algorithms/cp-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fft.cpp
More file actions
77 lines (66 loc) · 1.82 KB
/
test_fft.cpp
File metadata and controls
77 lines (66 loc) · 1.82 KB
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
#include <cassert>
#include <vector>
#include <complex>
using namespace std;
namespace recursive {
#include "fft_recursive.h"
#include "fft_multiply.h"
}
namespace iterative {
#include "fft_implementation_iterative.h"
#include "fft_multiply.h"
}
namespace iterative_opt {
#include "fft_implementation_iterative_opt.h"
#include "fft_multiply.h"
}
namespace modular {
long long inverse(long long x, int mod) {
long long res = 1;
int e = mod - 2;
while (e) {
if (e & 1)
res = res * x % mod;
x = x * x % mod;
e >>= 1;
}
return res;
}
#include "fft_implementation_modular_arithmetic.h"
vector<int> multiply(vector<int> a, vector<int> b) {
int n = root_pw;
a.resize(root_pw);
b.resize(root_pw);
fft(a, false);
fft(b, false);
for (int i = 0; i < n; i++)
a[i] = (int)(1LL * a[i] * b[i] % mod);
fft(a, true);
return a;
}
}
int main() {
vector<int> a = {-10, -51, -96, 2, 83, 95};
vector<int> b = {1, -85, -62, -40, 94, -88, 58, 47, -45};
vector<int> expected = {-10, 799, 4859, 11724, 6965, -7158, -18417, -4002, -3689, -475, 868, 9321, 730, -4275};
{
vector<int> result = recursive::multiply(a, b);
for (int i = 0; i < (int)expected.size(); i++)
assert(expected[i] == result[i]);
}
{
vector<int> result = iterative::multiply(a, b);
for (int i = 0; i < (int)expected.size(); i++)
assert(expected[i] == result[i]);
}
{
vector<int> result = iterative_opt::multiply(a, b);
for (int i = 0; i < (int)expected.size(); i++)
assert(expected[i] == result[i]);
}
{
vector<int> result = modular::multiply(a, b);
for (int i = 0; i < (int)expected.size(); i++)
assert((expected[i] - result[i]) % modular::mod == 0);
}
}