Orac and LCM
题目大意:给 n 个数,先求出所有 lcm(a[i],a[j]),i<j ;然后对新得到的数组所有数求gcd;
其实就是要知道这个推论:
GCD(LCM(a1,a2),LCM(a1,a3),LCM(a1,a4))==LCM(a1,GCD(a2,a3,a4))
预处理出gcd后缀,然后求出每个数的 lcm(a[i],gcd(a[i+1]…a[n])) ,对求的数再做一遍gcd;
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,LL>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std
;
const int N
=100100;
const int M
=50100;
const LL mod
=10007;
LL a
[N
],g
[N
],b
[N
];
int main(){
int n
;cin
>>n
;
for(int i
=1;i
<=n
;i
++) cin
>>a
[i
];
for(int i
=n
;i
>=1;i
--) g
[i
]=__gcd(g
[i
+1],a
[i
]);
for(int i
=1;i
<=n
;i
++) b
[i
]=a
[i
]*g
[i
+1]/g
[i
];
LL ans
=0;
for(int i
=1;i
<=n
;i
++) ans
=__gcd(ans
,b
[i
]);
cout
<<ans
<<endl
;
return 0;
}