#641 (Div. 2)C. Orac and LCM

题目描述

For the multiset of positive integers s={s1,s2,…,sk}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, gcd({8,12})=4,gcd({12,18,6})=6 and lcm({4,6})=12. Note that for any positive integer x, gcd({x})=lcm({x})=x.
Orac has a sequence a with length n. He come up with the multiset t={lcm({ai,aj}) | i<j}, and asked you to find the value of gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.

Input

The first line contains one integer n (2≤n≤100000).
The second line contains n integers, a1,a2,…,an (1≤ai≤200000).

Output

Print one integer: gcd({lcm({ai,aj}) | i<j}).

Examples

input
2
1 1
output
1
input
4
10 24 40 80
output
40
input
10
540 648 810 648 720 540 594 864 972 648
output
54

Note

For the first example, t={lcm({1,1})}={1}, so gcd(t)=1.
For the second example, t={120,40,80,120,240,80}, and it’s not hard to see that gcd(t)=40.

题目大意

给出n个数,让你给这些数两两之间求最小公倍数。再求出这些最小公倍数的最大公约数。

题目分析

先预处理出2e5以内的素数。
假设a[i]和a[j]都没有2这个因子,那么lcm(a[i],a[j])也就没有2这个因子。那么ans中也就没有2这个因子。因为如果某个质因子p能存在于最后的gcd中,那说明p在所有lcm(a[i],a[j])中都出现了,也就是说最多只能有一个a[i]可以不包含p。

因此,我们可以将这n个数进行拆分,拆分成某个素数的k次幂,并将k存到对应的容器中(可以用邻接表),如:20可以拆成22和51,然后在pre[2]中放入2,pre[5]中放入1。

最后遍历每一个素数对应的容器,当该容器中的数小于n-1时,说明ans不包含该容器对应的质因数。
然后我们要分类讨论容器中的数为n和n-1时的情况。
1.容器中的数为n时,ans中的对应因子为该素数在容器中的次小值次幂。
2.容器中的数为n-1时,ans中的对应因子为该素数在容器中的最小值次幂。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=2e5+15,M=1e6;
int prime[N],cnt;
bool st[M];
vector<int> pre[N];     //存将输入数拆分后对应的幂的容器
void get_prime(int x)   //筛素数(线性筛法)
{
    for(int i=2;i<x;i++)
    {
        if(!st[i]) prime[cnt++]=i;
        for(int j=0;prime[j]<=x/i;j++)
        {
            st[prime[j]*i]=true;
            if(i%prime[j]==0) break;
        }
    }
}
void cal(int x)     //拆分函数,将输入的数拆分
{
	for(int i=0;i<cnt;i++)
	{
		if(prime[i]*prime[i]>x) break;
		if(x%prime[i]==0)
		{
			int num=0;
			while(x%prime[i]==0)    //将其拆分为p的num次幂
			{
				num++;
				x/=prime[i];
			}
			pre[prime[i]].push_back(num);   //将num放入容器中
		}
	}
	if(x>1) pre[x].push_back(1);    //若x>1,说明x为一个素数,将其放入容器
}
int main()
{
	int n;
	cin>>n;
	get_prime(2e5+10);     //预处理2e5以内的素数
	for(int i=1;i<=n;i++)  //输入n个数并将其拆分
	{
		int x;
		cin>>x;
		cal(x);
	}
	LL ans=1;
	for(int i=0;i<cnt;i++)     //遍历素数
	{
		int x=prime[i];
		sort(pre[x].begin(),pre[x].end());  //将容器中的数排序
		if(pre[x].size()==n)      //分类讨论的两种情况
		{
			int m=pre[x][1];
			int t=1;
			for(int i=1;i<=m;i++)   //t即为ans的对应因子
			t*=x;
			
			ans*=t;       //乘上该因子
		}
		else if(pre[x].size()==n-1)
		{
			int m=pre[x][0];
			int t=1;
			for(int i=1;i<=m;i++)
			t*=x;
			
			ans*=t;
		}
	}
	cout<<ans<<endl;     //输出答案
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值