CodeForces - 1350C Orac and LCM(数论)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1350C Orac and LCM(数论)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出 n 個數,先求出兩兩 lcm 后的集合 t ,再求這個集合 t 的 gcd
題目分析:做這個題得知道一個前置知識:對于 lcm 和 gcd 運算來說,每個質因子都相互獨立
知道這個知識點后,對于每個數先質因子分解,對于某個質因子 p 而言,分三種情況討論:
稍微解釋一下,因為對于某個質因子 p 來說,如果先求 lcm 再求 gcd 的話,會給 gcd 造成影響的一定是指數最小的兩個數所求出的 lcm,而指數最小的兩個數所求出的 lcm 顯然就是指數次小的那個數的指數了
其實情況 2 和情況 1 類似,只不過是將情況 1 中的第 n 個數的指數用 0 代替了而已,這也情況 2 中的最小值不就是情況 1 中的次小值了嘛
當 p 在任意兩個數中的指數均為 0 時,此時次小值所代表的指數就是 0 了,貢獻為 p^0 = 1,為了降低復雜度將其視為沒有貢獻即可
最后將所有質因子的貢獻累乘起來就是答案了,注意結果需要開 long long 儲存
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;vector<int>p[N];void only(int n) {for(int i=2;i*i<=n;i++){if(n%i==0){int cnt=0;while(n%i==0){cnt++;n/=i;}p[i].push_back(cnt);}}if(n!=1)p[n].push_back(1); }LL q_pow(LL a,LL b) {LL ans=1;while(b){if(b&1)ans*=a;a*=a;b>>=1;}return ans; }int main() { #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){int num;scanf("%d",&num);only(num);}LL ans=1;for(int i=1;i<N;i++){if(p[i].size()==n){sort(p[i].begin(),p[i].end());ans*=q_pow(i,max(p[i][0],p[i][1]));}if(p[i].size()==n-1){ans*=q_pow(i,*min_element(p[i].begin(),p[i].end()));}}printf("%lld\n",ans);return 0; }?
總結
以上是生活随笔為你收集整理的CodeForces - 1350C Orac and LCM(数论)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1350D O
- 下一篇: CodeForces - 1350B O