HDOJ 1402 A * B Problem Plus -- FFT

本文详细介绍了快速傅里叶变换(FFT)算法的应用——大数乘法问题,并提供了完整的C++实现代码。通过FFT进行多项式乘法可以高效地解决整数相乘问题,特别是在整数长度非常大的情况下。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11652    Accepted Submission(s): 2005


Problem Description
Calculate A * B.


 Input
Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.
 

Output
For each case, output A * B in one line.
 

Sample Input
1 2 1000 2
 

Sample Output
2 2000


分析:FFT模板。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define mp make_pair
using namespace std;

typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
typedef pair pii;
typedef vector vi;
typedef vi::iterator vi_it;
typedef map mii;
typedef priority_queue pqi;
typedef priority_queue, greater > rpqi;

const double PI = acos(-1.0);
const double EPS = 1.0e-6;
const int MAX_LEN = 50000 + 2;
const complex ZERO(0.0, 0.0);
char str[2][MAX_LEN];
complex num[2][MAX_LEN << 2];
int ans[MAX_LEN << 2];

class FFT
{
	public:
	void fft(complex *a, int len, int type = 1);
	
	private:
	void bit_reverse_copy(complex *a, int len);
	int rev(int k, int n);
};

int FFT::rev(int k, int n)
{
	int ret = 0;
	while (k) {
		ret = ret * 2 + k % 2;
		k >>= 1;
		--n;
	}
	
	while (n--) {
		ret <<= 1;
	}
	
	return ret;
}

void FFT::bit_reverse_copy(complex *a, int len)
{
	vector > b(len);
	int i;
	int k = 1, cnt = 0;
	while (k != len) {
		k <<= 1;
		++cnt;
	}
	for (i = 0; i < len; ++i) {
		b[rev(i, cnt)] = a[i];
	}
	for (i = 0; i < len; ++i) {
		a[i] = b[i];
	}
}

//len is a power of 2
void FFT::fft(complex *a, int len, int type)
{
	bit_reverse_copy(a, len);
	for (int i = 1, m = 1; (1 << i) <= len; ++i) {
		m <<= 1;
		complex wm(polar(1.0, type * 2.0 * PI / m));
		for (int k = 0; k < len; k += m) {
			complex w(1.0, 0.0);
			for (int j = 0; j < (m >> 1); ++j) {
				complex t = w * a[k + j + (m >> 1)], u = a[k + j];
				a[k + j] = u + t;
				a[k + j + (m >> 1)] = u - t;
				w = w * wm;
			}
		}
	}
	if (type == -1) {
		for (int j = 0; j < len; ++j) {
			a[j] = complex(a[j].real() / len + EPS, a[j].imag());
		}
	}
}

int main(int argc, char *argv[])
{
//	freopen("D:\\in.txt", "r", stdin);
	FFT f;
	while (scanf("%s%s", str[0], str[1]) == 2) {
		int len[2] = {strlen(str[0]), strlen(str[1])};
		int n = 1, tmp = max(len[0], len[1]) << 1, i;
		while (n < tmp) {
			n <<= 1;
		}
		for (i = 0; i < n; ++i) {
			num[0][i] = ZERO;
			num[1][i] = ZERO;
		}
		for (i = 0; i < len[0]; ++i) {
			num[0][i] = complex((double)(str[0][len[0] - i - 1] - '0'), 0.0);
		}
		for (i = 0; i < len[1]; ++i) {
			num[1][i] = complex((double)(str[1][len[1] - i - 1] - '0'), 0.0);
		}
		
		f.fft(num[0], n);
		f.fft(num[1], n);
		for (i = 0; i < n; ++i) {
			num[0][i] *= num[1][i];
		}
		f.fft(num[0], n, -1);
		
		int pre = 0, top = 0;
		for (i = 0; i < len[0] + len[1]; ++i) {
			int cur = (int)(num[0][i].real() + EPS);
			ans[++top] = (cur + pre) % 10;
			pre = (cur + pre) / 10;
		}
		
		while (!ans[top] && top > 1) {
			--top;
		}
		for (i = top; i; --i) {
			printf("%d", ans[i]);
		}
		putchar('\n');
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值