CF1834E MEX of LCM

 

// LUOGU_RID: 129683316

/*
6
3
1 2 3
5
1 2 3 4 5
2 
2 3
1
1000000000
12
1 8 4 2 3 5 7 2 9 10 11 13
12
7 2 5 4 2 1 1 2 3 11 8 9

4 7 1 1 16 13

*/
/*
#include<cstdio>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=3e5+5;
int n;
ll a[N];
set<ll> ans,s;
ll lcm(ll x,ll y) {return x/__gcd(x,y)*y;}

int main()
{
    int T;cin>>T;
    while(T--)
    {
        cin>>n; ans.clear(),s.clear();
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=n;i++)
        {
            set<ll> now;
            for(auto x:s) if(lcm(x,a[i])<1e9) now.insert(lcm(x,a[i]));
            now.insert(a[i]),swap(now,s);
            for(auto x:s) ans.insert(x);
        }
        ll lst=0,res=0;
        for(auto x:ans)
            if(x>lst+1) {res=lst+1;break;}
            else lst=x;
        printf("%lld\n",res?res:lst+1);
    }
    return 0;
}
*/

#include<cstdio>
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=3e5+10;

int n;
ll a[maxn];

ll gcd(ll a,ll b){
    return b==0? a: gcd(b,a%b);
}
ll lcm(ll a,ll b){
    return a*b/gcd(a,b); 
}

set<ll> s,ans;

int main()
{
    int T;cin>>T;
    while(T--)
    {
        
        cin>>n; s.clear();ans.clear();
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=n;i>=1;i--){
            set<ll> now;
            for(set<ll>::iterator it=s.begin();it!=s.end();it++)  if(lcm(*it,a[i])<1e9) now.insert(lcm(*it,a[i]));
            now.insert(a[i]); swap(now,s);
            for(set<ll>::iterator it=s.begin();it!=s.end();it++) ans.insert(*it);
        }
        
        ll lst=0,res=0;  
        for(set<ll>::iterator it=ans.begin();it!=ans.end();it++){
            if(lst+1<*it){ res=lst+1; break; }
            else lst=*it;
        }
        
        if(res==0) cout<<lst+1<<'\n';
        else cout<<res<<'\n';
        
    }
    
    
    return 0;
 } 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

posted @ 2023-10-22 09:03  JMXZ  阅读(7)  评论(0)    收藏  举报