NC11166 H.Hash Function(fft)
Posted Bcoi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NC11166 H.Hash Function(fft)相关的知识,希望对你有一定的参考价值。
目录
Description
有 \\(n\\) 个数,要求找到最小的 \\(seed\\), 使得 \\(a[i]%seed\\) 都不相同
State
\\(1<=n<=5*10^5\\)
\\(0<=a[i]<=5*10^5\\)
Input
3
1 2 4
Output
4
Solution
对于任意的 \\(i,j\\), \\(a[i]!=a[j](mod\\ p)\\),也就是 \\(a[i]-a[j]!=0(mod\\ p)\\),需要计算出所有的差,利用 \\(fft/ntt\\) 的系数可以快速的解决
得到了所有差值之后,枚举 \\(p\\),如果 \\(p\\) 的倍数存在,那么 \\(p\\) 是不成立的
Code
const int N = 2e6 + 5;
int n, m, k, _;
//int a[N];
struct Complex
{
double x,y;
Complex (double x = 0, double y = 0) : x(x), y(y){}
}a[N],b[N];
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); }
int lim = 1, bit = 0, rev[N];
void init(int n)
{
while(lim <= n) lim <<= 1, bit ++;
for(int i = 0; i < lim; i ++){
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));
}
}
void fft(Complex *a, int opt)
{
for(int i = 0; i < lim; i ++){
if(i < rev[i]) swap(a[i], a[rev[i]]);
}
for(int mid = 1; mid < lim; mid *= 2){
Complex wn(cos(pi / mid), opt * sin(pi / mid));
for(int len = mid * 2, i = 0; i < lim; i += len){
Complex w(1, 0);
for(int j = 0; j < mid; j ++, w = w * wn){
Complex x = a[i + j], y = a[i + j + mid] * w;
a[i + j] = x + y;
a[i + j + mid] = x - y;
}
}
}
if(opt == 1) return ;
for(int i = 0; i < lim; i ++){
a[i].x = int(a[i].x / lim + 0.5);
}
}
int vis[N];
bool judge(int x)
{
for(int i = x; i < lim; i += x){
if(vis[i]) return 0;
}
return 1;
}
signed main()
{
const int _5 = 5e5;
//ios;
while(~ sd(n)){
init(1e6);
rep(i, 1, n){
int x = read();
a[x] = 1;
b[_5 - x] = 1;
}
fft(a, 1); fft(b, 1);
for(int i = 0; i < lim; i ++) a[i] = a[i] * b[i];
fft(a, -1);
for(int i = 0; i < lim; i ++) vis[abs(i - _5)] = a[i].x;
for(int i = n;; i ++){
if(judge(i)){
pd(i);
return 0;
}
}
return 0;
}
//PAUSE;
return 0;
}
以上是关于NC11166 H.Hash Function(fft)的主要内容,如果未能解决你的问题,请参考以下文章