2021-08-01 多项式乘法(fft)

多项式乘法

题目1

题意:
求一个最小模数 mod ,使得 n 个数 a 1 , a 2 , a 3 , . . . , a n a_1, a_2, a_3, ...,a_n a1,a2,a3,...,an 模 mod 都不相等

思路:
a mod m == b mod m <==> m | abs(a - b)
所以求出 n 个数中 两两的差值 , 枚举模数 用筛法思想去判断是否符合要求(不能是任意差值的约数)
用 fft 去快速计算 n 个数两两的所有差值

#include <bits/stdc++.h>
using namespace std;

const int N = 1 << 20;
const double PI = acos(-1);

int n, m;
struct Complex{
	double x, y;
	Complex operator+ (const Complex& t) const {
		return {x + t.x, y + t.y};
	}
	Complex operator- (const Complex& t) const {
		return {x - t.x, y - t.y};
	}
	Complex operator* (const Complex& t) const {
		return {x * t.x - y * t.y, x * t.y + y * t.x};
	}
	Complex operator* (const double& t) const {
		return {x * t, y * t};
	}
}A[N], B[N], a[N], b[N], ans[N];
int rev[N], bit, tot;

void fft(Complex a[], int inv) {
	for (int i = 0; i < tot; i ++ )
		if (i < rev[i]) 
			swap(a[i], a[rev[i]]);
	for (int mid = 1; mid < tot; mid <<= 1) {
		auto w1 = Complex({cos(PI / mid), inv * sin(PI / mid)});
		for (int i = 0; i < tot; i += mid * 2) {
			auto wk = Complex({1, 0});
			for (int j = 0; j < mid; j ++ , wk = wk * w1) {
				auto x = a[i + j], y = wk * a[i + j + mid];
				a[i + j] = x + y, a[i + j + mid] = x - y;
			}
		}
	}
}
int P = 500000;
bool st[N];

int main() {
	int nn;
	scanf("%d", &nn);
	for (int i = 0; i < nn; i ++ ) {
		int x;
		scanf("%d", &x);
		a[x].x ++ ;
		b[P - x].x ++ ;
	}
	n = P, m = P;
	while((1 << bit) < n + m + 1) bit ++ ;
	tot = 1 << bit;
	for (int i = 0; i < tot; i ++ ) 
		rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
	
	fft(a, 1), fft(b, 1);
	for (int i = 0; i < tot; i ++ ) ans[i] = a[i] * b[i];
	fft(ans, -1);
	
	for (int i = P + 1; i < tot; i ++ ) {
		if (abs((int)(ans[i].x / tot + 0.5))) {
			//cout << abs(i - P) << endl; 
			st[abs(i - P)] = 1;
		}
	}
	
	int res = nn - 1;
	bool flag = 1;
	while(flag) {
		res ++ ;
		flag = 0;
		for (int i = res; i <= P; i += res) {
			if (st[i]) {
				flag = 1;
				break;
			}
		}
	}
	
	printf("%d\n", res);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值