高精度乘法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;
}

时间: 2024-08-04 10:43:54

高精度乘法FFT 模板的相关文章

[hdu1402]大数乘法(FFT模板)

题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

HDU1402 FFT高精度乘法模板题

#include<bits/stdc++.h> using namespace std; //HDU 1402 求高精度乘法 const double PI = acos(-1.0); //复数结构体 struct Complex { double x,y;//实部和虚部x+yi Complex(double _x = 0.0,double _y = 0.0) { x = _x; y = _y; } Complex operator -(const Complex &b)const {

【BZOJ 2179】【FFT模板】 FFT快速傅立叶

2179: FFT快速傅立叶 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 1595 Solved: 792 [Submit][Status][Discuss] Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出一行,即x*y的结果. Sample Input 1 3 4 Sample Output

高精度乘法程序

对于超过20位的数的乘法问题,我们无法使用普通的方法!!!即使是longlong也会超出范围的!像这样的数,我们只能使用高精度的知识利用数组的方法解决问题!对于高精度乘法的问题,其实思路和高精度加法的思路差不多,都需要使用字符数组来存放每次算完的结果!        1  2  3        *4  5  6    ________________      12  15  18   8  10  124  5   6  _____________________4 13   28   27

大数高精度运算(模板)

前言:高精度运算.是指參与运算的数(加数.减数,因子--)范围大大超出了标准数据类型(整型,实型)能表示的范围的运算. 模板:包含大数加减乘除.大数与int数的乘法,模板能够不断扩充. 代码: /* 所有亲測可用,可是不能用于负数的运算,仅仅能对正数进行大数运算 */ const int ten[4]= {1,10,100,1000}; const int maxl = 300; struct BigNumber { int d[maxl]; char s[maxl]; BigNumber(co

再写FFT模板

没什么好说的,今天有考了FFT(虽然不用FFT也能过)但是确实有忘了怎么写FFT了,于是乎只有重新写一遍FFT模板练一下手了.第一部分普通FFT,第二部分数论FFT,记一下模数2^23*7*17+1 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef double

fft模板 HDU 1402

1 // fft模板 HDU 1402 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 #include <bits/stdc++.h> 11 using

POJ 1306 Combinations 高精度乘法

题目大意:给出mn,让你求C(m,n). 思路:公式都给你了,就100,暴力就可以关键还是高精度.如果按照算法"它让你怎么做你就怎么做",那么很显然你需要写一个高精度除法.然而可以证明,这个除法是不会产生余数的.所以我们可以数论分析,然后避免高精度除法. 方法就是暴力求每个数的质因数,然后把被除数和除数相同的质因数消去,最后除数肯定会被消没.这样只要做高精度乘法就可以了. CODE: #include <cstdio> #include <cstring> #i

poj1001(高精度乘法)

1.题目表述 Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 135893   Accepted: 33256 Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation o