码迷,mamicode.com
首页 > 其他好文 > 详细

高精度乘法FFT 模板

时间:2014-12-26 11:13:24      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:快速傅立叶变换   高精度乘法   fft   

渣模板,不知为何常数还挺大。。


CODE:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 200010
#define PI 3.1415926535897932384626
using namespace std;

struct Complex{
	double real,imag;
	
	Complex(double _,double __):real(_),imag(__) {}
	Complex() {}
	Complex operator +(const Complex &a)const {
		return Complex(real + a.real,imag + a.imag);
	}
	Complex operator -(const Complex &a)const {
		return Complex(real - a.real,imag - a.imag);
	}
	Complex operator *(const Complex &a)const {
		return Complex(real * a.real - imag * a.imag,real * a.imag + imag * a.real);
	}
	void operator *=(const Complex &a) {
		*this = *this * a;
	}
	void Read() {
		int temp;
		scanf("%1d",&temp);
		real = temp;
	}
}a[MAX],b[MAX],ans[MAX];

int n;
int out[MAX];

void FFT(Complex x[],int n,int flag)
{
	static Complex temp[MAX];
	if(n == 1)	return ;
	for(int i = 0; i < n; i += 2)
		temp[i >> 1] = x[i],temp[(i + n) >> 1] = x[i + 1];
	memcpy(x,temp,sizeof(Complex) * n);
	Complex *l = x,*r = x + (n >> 1);
	
	FFT(l,n >> 1,flag),FFT(r,n >> 1,flag);
	
	Complex unit(cos(flag * 2 * PI / n),sin(flag * 2 * PI / n)),w(1.0,.0);
	for(int i = 0; i < (n >> 1); ++i,w *= unit)
		temp[i] = l[i] + w * r[i],temp[i + (n >> 1)] = l[i] - w * r[i];
	memcpy(x,temp,sizeof(Complex) * n);
}

char s1[MAX],s2[MAX];
int l1,l2;

int main()
{
	bool flag = false;
	char c;
	while(c = getchar(),c == '\n' || c == '\r' || c == ' ' || c == '\t');
	if(c != '-')	ungetc(c,stdin);
	else	flag ^= 1;
	scanf("%s",s1);
	while(c = getchar(),c == '\n' || c == '\r' || c == ' ' || c == '\t');
	if(c != '-')	ungetc(c,stdin);
	else	flag ^= 1;
	scanf("%s",s2);
	l1 = strlen(s1),l2 = strlen(s2);
	for(int i = l1 - 1,pos = 0; ~i; --i)
		a[pos++].real = s1[i] - '0';
	for(int i = l2 - 1,pos = 0; ~i; --i)
		b[pos++].real = s2[i] - '0';
	if((l1 == 1 && s1[0] == '0') || (l2 == 1 && s2[0] == '0')) {
		puts("0");
		return 0;
	}
	int l;
	for(l = 1; l <= l1 + l2; l <<= 1);
	FFT(a,l,1),FFT(b,l,1);
	for(int i = 0; i < l; ++i)
		ans[i] = a[i] * b[i];
	FFT(ans,l,-1);
	for(int i = 0; i < l; ++i)
		out[i] = int(ans[i].real / l + .5);
	int temp = 0;
	for(int i = 0; i < l; ++i) {
		out[i] += temp;
		temp = out[i] / 10;
		out[i] %= 10;
	}
	while(temp)	out[l++] = temp % 10,temp /= 10;
	while(!out[l - 1])	--l;
	if(flag)	putchar('-');
	for(int i = l - 1; ~i; --i)
		printf("%d",out[i]);
	return 0;
}


高精度乘法FFT 模板

标签:快速傅立叶变换   高精度乘法   fft   

原文地址:http://blog.csdn.net/jiangyuze831/article/details/42168567

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!