Codeforces Round #641 (Div. 2)
2021/11/12
给一组数 a [ n ] a[n] a[n] ,求 g c d ( { l c m ( a i , a j ) ∣ i < j } ) gcd(\{lcm(a_i,a_j)|i < j\}) gcd({lcm(ai,aj)∣i<j})
不失一般性,假设 a[n] = {a,b,c};
那么考虑 i = a;
则 g c d ( l c m ( a , b ) , l c m ( a , c ) ) = l c m ( a , g c d ( b , c ) ) gcd(lcm(a,b),lcm(a,c)) = lcm(a,gcd(b,c)) gcd(lcm(a,b),lcm(a,c))=lcm(a,gcd(b,c))
由此可知:
设 g 1 = l c m ( a , g c d ( b , c , d . . . a [ n ] ) ) {g_1} =lcm(a,gcd(b,c,d...a[n])) g1=lcm(a,gcd(b,c,d...a[n]))
g 2 = l c m ( b , g c d ( c , d . . . . , a [ n ] ) ) {g_2} =lcm(b,gcd(c,d....,a[n])) g2=lcm(b,gcd(c,d....,a[n]))
a n s = g c d ( g 1 , g 2 . . . . , g n − 1 ) ans = gcd(g_1,g_2....,g_n-1) ans=gcd(g1,g2....,gn−1)
用后缀数组处理下gcd;
// Problem: C. Orac and LCM
// Contest: Codeforces - Codeforces Round #641 (Div. 2)
// URL: https://codeforces.com/problemset/problem/1350/C
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include
using namespace std;
#define _orz ios::sync_with_stdio(false),cin.tie(0)
#define mem(str,num) memset(str,num,sizeof(str))
#define forr(i,a,b) for(int i = a;i <= b;i++)
#define forn(i,n) for(int i = 0; i < n; i++)
#define all(a) (a.begin(),a.end())
#define dbg() cout << "0k!" << endl;
//#define _DEBUG
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5+10;
const ll MOD = 1e9+7;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
ll lcm(ll a, ll b){
ll res = a/gcd(a,b)*b;
return res;
}
ll suf[N];
int a[N];
void so1ve(){
int n; cin >> n;
forr(i,1,n)cin >> a[i];
suf[n] = a[n];
for(int i = n-1; i >= 1; i--){
suf[i] = gcd(suf[i+1],a[i]);
}
ll ans = lcm(a[1],suf[2]);
forr(i,2,n-1) ans = gcd(ans,lcm(a[i],suf[i+1]));
cout << ans << endl;
}
int main()
{
#ifdef _DEBUG
//freopen("input.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;;
while(t--) so1ve();
return 0;
}