题目大意
给你 n n n 个数,找出一个数 x x x(可以不在这些数之中),让这 n n n 个数对 x x x 取模互不相等
解题思路
首先,很容易发现答案一定在
[
n
,
m
a
x
(
a
i
)
i
∈
[
1
,
n
]
+
1
]
[n, max(a_i) i \in [1, n] + 1]
[n,max(ai)i∈[1,n]+1] 之中
然后我们可以得出一个结论,任意两个数之差的绝对值一定不是答案,且任意两数之差的绝对值的因子也一定不是答案
首先我们可以用FFT处理出任意两数之差的绝对值,可以参考这篇博客如何求出任意两数之和,我们给数变为一个负下标然后加上偏移量就可以处理任意两数之差
然后用用正向筛选法可以在
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn) 内筛选出所有因子。
Code
#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
using namespace std;
const int MAXN = 2e6 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const double PI = acos(-1.0);
struct Complex
{
double x, y;
Complex operator+(const Complex &W) const
{
return {x + W.x, y + W.y};
}
Complex operator-(const Complex &W) const
{
return {x - W.x, y - W.y};
}
Complex operator*(const Complex &W) const
{
return {x * W.x - y * W.y, x * W.y + y * W.x};
}
};
int n, m;
int vis[MAXN];
Complex a[MAXN], b[MAXN] ;
int R[MAXN];
int tot, bit;
void inif(int n)
{
tot = 1, bit = 0;
while (tot <= n)
tot <<= 1, ++bit;
for (int i = 0; i <= tot; ++i)
R[i] = (R[i >> 1] >> 1) | ((i & 1) << (bit - 1));
}
void FFT(Complex f[], int total, int type, int n, int m)
{
for (int i = 0; i < total; ++i)
if (i < R[i])
swap(f[i], f[R[i]]);
for (int tot = 2; tot <= total; tot <<= 1)
{
Complex w1 = {cos(2 * PI / tot), type * sin(2 * PI / tot)};
for (int pos = 0; pos < total; pos += tot)
{
Complex w = {1, 0};
for (int i = pos; i < pos + tot / 2; ++i, w = w * w1)
{
Complex x = f[i];
Complex y = w * f[i + tot / 2];
f[i] = x + y;
f[i + tot / 2] = x - y;
}
}
}
if (type == -1)
{
for (int i = 0; i <= n + m; ++i)
f[i].x = (ll)(f[i].x / tot + 0.5);
}
}
void solve(){
cin >> n;
m = n;
const int P = 5e5 + 7;
int maxx = 0;
for(int i = 1; i <= n; i++){
int x;
cin >> x;
maxx = max(maxx, x);
a[x].x++;
b[P - x].x++;
}
int k = n;
n = m = P;
inif(n + m);
FFT(a, tot, 1, n, m), FFT(b, tot, 1, n, m);
for (int i = 0; i < tot; ++i)
a[i] = a[i] * b[i];
FFT(a, tot, -1, n, m);
for(int i = 0; i < MAXN; i++){
if(a[i].x){
vis[abs(i - P)]++;
}
}
for(int i = k; i <= maxx+1; i++){
int f = 1;
for(int j = i; j <= maxx; j += i){
if(vis[j]){
f = 0;
break;
}
}
if(f){
cout << i << endl;
return ;
}
}
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
qc;
int T;
// cin >> T;
T = 1;
while(T--){
solve();
}
return 0;
}