快读是复制粘贴的,复杂度应该正确(看题解了),本地随机数据大概跑300ms
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
template <typename T> inline void read(T &a)
{
a=0;T w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){a=(a<<3)+(a<<1)+(ch^48);ch=getchar();}
a*=w;
}
int t, p[300005], a[300005];
int e[5000005] = {0};
int gcd(int a, int b){
if(a % b == 0)
return b;
return gcd(b, a % b);
}
int f[25][300005];
signed main(){
// freopen("E.in", "r", stdin);
// freopen("E.out", "w", stdout);
read(t);
int now = 1;
e[1] = 1;
p[1] = 1;
for(int i = 2; now < 300004; i ++){
if(e[i])
continue;
e[i] = 1;
p[++now] = i;
for(int j = i; i * j <= 5000000; j ++)
e[i * j] = 1;
}
while(t --){
int n, t = 1;
read(n);
for(int i = 1; i <= n; i ++){
read(a[i]);
// a[i] = i;
if(a[i] != 1)
t = 0;
}
if(t){
printf("2\n");
continue;
}
// a[i] = 1;
for(int i = 1; i <= n; i ++)
f[0][i] = a[i];
for(int j = 1; j <= 19; j ++){
for(int i = 1; i <= n - (1 << (j)) + 1; i ++){
if(f[j - 1][i] == -1 || f[j - 1][i + (1 << (j - 1))] == -1)
f[j][i] = -1;
else
f[j][i] = f[j - 1][i] * f[j - 1][i + (1 << (j - 1))] / gcd(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
if(f[j][i] > p[n + 1])
f[j][i] = -1;
}
}
map<int, bool> e;
for(int i = 1; i <= n; i ++){
int sum = a[i];
if(sum >= p[n + 1])
continue;
e[sum] = 1;
for(int j = i + 1; j <= n; j ++){
sum = sum * a[j] / gcd(sum, a[j]);
if(sum >= p[n + 1])
break;
e[sum] = 1;
int t = j;
for(int k = 19; k >= 0; k --){
if(t + (1 << k) - 1 <= n && f[k][t] != -1 && sum * f[k][t] / gcd(sum, f[k][t]) == sum)
t += (1 << k) - 1;
}
j = t;
}
}
int x = 1;
while(e.find(x) != e.end())
x ++;
// for(int i = 1; i <= n; i ++)
// printf("%lld ", a[i]);
printf("%lld\n", x);
}
return 0;
}