第10个点WA???WTF
查看原帖
第10个点WA???WTF
150218
MikeDuke楼主2020/6/14 16:54

FFT部分直接粘的,应该没毛病吧(?

#include <bits/stdc++.h>

using namespace std;

#define M 3000005

const double Pi = acos(-1.0);

int n, m, limit = 1;
int l, r[M], ans[M];

struct Complex
{
	double x, y;
	Complex (double x_ = 0, double y_ = 0) { x = x_, y = y_; }
} a[M], b[M];

Complex operator + (Complex a, Complex b)
{ return Complex(a.x + b.x, a.y + b.y); }

Complex operator - (Complex a, Complex b)
{ return Complex(a.x - b.x, a.y - b.y); }

Complex operator * (Complex a, Complex b)
{ return Complex(a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x); }

inline int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }
	while (isdigit(ch)) { (x *= 10) += ch - '0'; ch = getchar(); }
	return x * f;
}

void FFT(Complex *A, int typ)
{
	for (int i = 0; i < limit; i++)
		if (i < r[i]) swap(A[i], A[r[i]]);
	for (int mid = 1; mid < limit; mid <<= 1)
	{
		Complex Wn( cos(Pi/mid), typ * sin(Pi/mid) );
		for (int R = mid << 1, j = 0; j < limit; j += R)
		{
			Complex w(1, 0);
			for (int k = 0; k < mid; k++, w = w * Wn)
			{
				Complex x = A[j + k], y = w * A[j + k + mid];
				A[j + k] = x + y, A[j + k + mid] = x - y;
			}
		}
	}
}

int main()
{
	string s1, s2;
	cin >> s1 >> s2;
	n = s1.length(), m = s2.length() ;
	for (int i = n - 1; ~i; i--)
		a[n-1-i].x = s1[i] - '0';
	for (int i = m - 1; ~i; i--)
		b[m-1-i].x = s2[i] - '0';
	n--, m--;
	// for (int i = 0; i <= n; i++) cout << a[i].x;
	// cout << endl;
	// for (int j = 0; j <= m; j++) cout << b[j].x;
	// cout << endl;
	// for ()
	// for (int i = 0; i <= n; i++)
	// 	a[i].x = read();
	// for (int i = 0; i <= m; i++)
	// 	b[i].x = read();

	while (limit <= n + m)  limit <<= 1, l++;

	for (int i = 0; i < limit; i++)
		r[i] = ( r[i>>1] >> 1 ) | ( (i&1) << (l-1) );

	FFT(a, 1), FFT(b, 1);

	for (int i = 0; i <= limit; i++)
		a[i] = a[i] * b[i];

	FFT(a, -1);

	for (int i = 0; i <= n + m; i++)
		ans[i] = (int) (a[i].x / limit + 0.5);
	for (int i = 0; i <= n + m; i++)
		if (ans[i] > 10) ans[i+1] += ans[i] / 10, ans[i] %= 10;

	for (int i = (ans[n+m+1]?n+m+1:n+m); ~i; i--)
		printf("%d", ans[i]);

	return 0;
}
2020/6/14 16:54
加载中...