【bzoj 2179】FFT

高精乘,就是裸的FFT啦……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#define Rep(i, x, y) for (int i = x; i <= y; i ++)
#define RepE(i, x) for (int i = pos[x]; i; i = g[i].nex)
#define Dwn(i, x, y) for (int i = x; i >= y; i --)
using namespace std;
typedef long long LL;
const int M = 200005;
struct CP {
	double x, y;
	CP() {}
	CP(double x0, double y0) { x = x0, y = y0; }
	inline CP operator+ (CP b){ return CP(x + b.x, y + b.y); }
	inline CP operator- (CP b){ return CP(x - b.x, y - b.y); }
	inline CP operator* (CP b){ return CP(x * b.x - y * b.y, x * b.y + y * b.x); }
} a[M], b[M];
char s1[M], s2[M];
int n, ans[M], l, N;
double PI = M_PI;
void Dft(CP *a, int n, int f) {
	for (int i = n >> 1, j = 1, k; j < n - 1; j ++) {
		if (i > j) swap(a[i], a[j]);
		for (k = n >> 1; k <= i; k >>= 1) i ^= k;
		i ^= k;
	}
	for (int i = 2; i <= n; i <<= 1) {
		CP wn(cos(2 * PI / i), f * sin(2 * PI / i));
		for (int k = 0; k < n; k += i) {
			CP w(1, 0), x, y;
			Rep(j, 0, i/2-1) x = a[k+j], y = w * a[k+i/2+j], a[k+j] = x + y, a[k+i/2+j] = x - y, w = w * wn;
		}
	}
	if (f == -1) Rep(i, 0, n-1) a[i].x = (a[i].x / N + 0.5);
}
int main()
{
	scanf ("%d%s%s", &n, s1, s2);
	for (N = 1, l = 0; N < n; N <<= 1, l ++); N <<= 1, l ++;
	Rep(i, 0, n-1) a[i].x = s1[n-i-1] - '0', b[i].x = s2[n-i-1] - '0';
	Dft(a, N, 1), Dft(b, N, 1);
	Rep(i, 0, N-1) a[i] = a[i] * b[i];
	Dft(a, N, -1);
	Rep(i, 0, N-1) ans[i] = int(a[i].x);
	Rep(i, 0, N-1) ans[i+1] += ans[i] / 10, ans[i] %= 10;
	int l = N - 1;
	while (l && !ans[l]) l --;
	Dwn(i, l, 0) printf ("%d", ans[i]);
	puts("");

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值