Skip to content

Commit b35dd73

Browse files
committed
Add tourist fft
1 parent e2b1f60 commit b35dd73

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

tests/math/fft-tourist.test.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// @problem_url: https://www.spoj.com/problems/MUL/
2+
#include<bits/stdc++.h>
3+
4+
using namespace std;
5+
typedef long double ld;
6+
7+
// @include: math/fft-tourist.cpp
8+
9+
int main() {
10+
int n;
11+
cin >> n;
12+
13+
while (n--) {
14+
string a, b;
15+
cin >> a >> b;
16+
17+
int size = a.size() + b.size();
18+
while (size - (size & -size) != 0) size -= size & -size;
19+
size *= 2;
20+
21+
vector<int> va(size);
22+
vector<int> vb(size);
23+
24+
for (int i = 0; i < a.size(); i++) va[a.size() - i - 1] = a[i] - '0';
25+
for (int i = 0; i < b.size(); i++) vb[b.size() - i - 1] = b[i] - '0';
26+
27+
auto ans = fft::multiply(va, vb);
28+
29+
string answer;
30+
int goes = 0;
31+
32+
for (auto c : ans) {
33+
int x = c + goes;
34+
35+
goes = 0;
36+
if (x > 9) {
37+
goes = x / 10;
38+
x = x % 10;
39+
}
40+
41+
answer.push_back(x + '0');
42+
}
43+
answer.push_back(goes + '0');
44+
45+
while (answer.back() == '0') answer.pop_back();
46+
if (answer.empty()) answer.push_back('0');
47+
reverse(answer.begin(), answer.end());
48+
49+
cout << answer << endl;
50+
}
51+
}

0 commit comments

Comments
 (0)