[Luogu 1919]【模板】A*B Problem升级版(FFT快速傅里叶)

Description

给出两个n位10进制整数x和y,你需要计算x*y。

Input

第一行一个正整数n。 第二行描述一个位数为n的正整数x。 第三行描述一个位数为n的正整数y。

Output

输出一行,即x*y的结果。(注意判断前导0)

Sample Input

1
3
4

Sample Output

12

HINT

n<=60000

题解

A*B Problem。和 A+B Problem 一样简单。

1 input() and print(int(input()) * int(input()))

对于一个大数 $\overline{a_na_{n-1}\cdots a_0}$ ,显然我们可以将其记为 $N=a_0\cdot 10^0+a_1\cdot 10^1+\cdots+a_n\cdot10^n$ 。将 $10^k$ 变为形式幂级数 $x^k$ : $N=a_0\cdot x^0+a_1\cdot x^1+\cdots+a_n\cdot x^n$ 。显然这是一个多项式。䨻 $FFT$ 的板子即可。

注意输入的数有前导零...

 //It is made by Awson on 2018.1.27
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <complex>
#include <iostream>
#include <algorithm>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int INF = ~0u>>;
const double pi = acos(-1.0);
const int N = 6e4*;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(int x) {
if (x > ) write(x/);
putchar(x%+);
} int n, m, L, R[N+], sum[N+];
dob a[N+], b[N+]; int getnum() {char ch = getchar(); while (ch < '' || ch > '') ch = getchar(); return ch-; }
void FFT(dob *A, int o) {
for (int i = ; i < n; i++) if (i > R[i]) swap(A[i], A[R[i]]);
for (int i = ; i < n; i <<= ) {
dob wn(cos(pi/i), sin(pi*o/i)), x, y;
for (int j = ; j < n; j += (i<<)) {
dob w(, );
for (int k = ; k < i; k++, w *= wn) {
x = A[j+k], y = w*A[i+j+k];
A[j+k] = x+y, A[i+j+k] = x-y;
}
}
}
}
void work() {
read(n); n--;
for (int i = n; i >= ; i--) a[i] = getnum();
for (int i = n; i >= ; i--) b[i] = getnum();
m = n<<;
for (n = ; n <= m; n <<= ) L++;
for (int i = ; i < n; i++) R[i] = (R[i>>]>>)|((i&)<<(L-));
FFT(a, ), FFT(b, );
for (int i = ; i < n; i++) a[i] *= b[i];
FFT(a, -);
for (int i = ; i <= m; i++) sum[i] = int(a[i].real()/n+0.5);
for (int i = ; i <= m; i++) sum[i+] += sum[i]/, sum[i] %= ;
if (sum[m+]) m++; while (!sum[m]) m--;
for (int i = m; i >= ; i--) write(sum[i]);
}
int main() {
work();
return ;
}
上一篇:YOLO系列目标检测算法详解


下一篇:YOLO v1