From b35dd73c096b435fbab787212840971efd46f04c Mon Sep 17 00:00:00 2001 From: Mateus de Castro Date: Sat, 6 Mar 2021 20:04:31 -0300 Subject: [PATCH] Add tourist fft --- tests/math/fft-tourist.test.cpp | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/math/fft-tourist.test.cpp diff --git a/tests/math/fft-tourist.test.cpp b/tests/math/fft-tourist.test.cpp new file mode 100644 index 0000000..148a243 --- /dev/null +++ b/tests/math/fft-tourist.test.cpp @@ -0,0 +1,51 @@ +// @problem_url: https://www.spoj.com/problems/MUL/ +#include + +using namespace std; +typedef long double ld; + +// @include: math/fft-tourist.cpp + +int main() { + int n; + cin >> n; + + while (n--) { + string a, b; + cin >> a >> b; + + int size = a.size() + b.size(); + while (size - (size & -size) != 0) size -= size & -size; + size *= 2; + + vector va(size); + vector vb(size); + + for (int i = 0; i < a.size(); i++) va[a.size() - i - 1] = a[i] - '0'; + for (int i = 0; i < b.size(); i++) vb[b.size() - i - 1] = b[i] - '0'; + + auto ans = fft::multiply(va, vb); + + string answer; + int goes = 0; + + for (auto c : ans) { + int x = c + goes; + + goes = 0; + if (x > 9) { + goes = x / 10; + x = x % 10; + } + + answer.push_back(x + '0'); + } + answer.push_back(goes + '0'); + + while (answer.back() == '0') answer.pop_back(); + if (answer.empty()) answer.push_back('0'); + reverse(answer.begin(), answer.end()); + + cout << answer << endl; + } +}