2021-08-01

多项式乘法

题目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;
}
上一篇:2021“MINIEYE杯”中国大学生算法设计超级联赛(1)部分题解


下一篇:cf1487