UR #3 核聚变反应强度( gcd )
生活随笔
收集整理的這篇文章主要介紹了
UR #3 核聚变反应强度( gcd )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
tags: -分解質因數 , gcd
題目大意
給定\(n\)個數,求\(a_1\)與\(a_i\)次小公約數
分析
易知次小公約數是\(\gcd\)的因數,于是用\(\gcd\)除去它的最小質因子即可。
沒有次小公約數的情況是\(\gcd = 1\),特判一下即可
直接枚舉的時間復雜度為\(O(n \sqrt a)\)
由于數據規模較大考慮優化
由于是求\(sgcd(a_1,a_i)\)于是結果一定是\(a_1\)的質因數組成,于是預處理\(a_1\)的質因數,然后每次處理時除去最小的即可,\(10^{12}< 2^{38}\)于是可以知道得到質因數的個數小于\(38\)個,于是時間復雜度就變為了\(O(50n)\)啦!
代碼
#include<iostream> #include<cmath> #include<cstring> #include<cstdio> #include<algorithm> #define LL long long #define maxn 100010 #define rep(i,a,b) for(int i = a; i <= b ;i++)using namespace std;int n; LL ys[40];LL gcd(LL a, LL b) {while (b ^= a ^= b ^= a %= b);return a; }int main(){scanf("%d",&n);LL l,tmp; int tot = 0;scanf("%lld", &l);tmp = l;for (int i = 2; 1ll * i * i <= l ;i ++)if (l % i == 0){ys[++tot] = i;while (l % i == 0) l /= i;}if (l != 1) ys[++tot] = l;l = tmp ;if (l != 1) printf("%lld ",l / ys[1]);else printf("-1 ");rep(i,2,n){LL aa;scanf("%lld",&aa);LL g = gcd(aa,l);if (g == 1) printf("-1 ");else rep (j,1,tot) if (g % ys[j] == 0 ){printf("%lld ",g / ys[j]);break; }}return 0; }轉載于:https://www.cnblogs.com/bobble/p/9445219.html
總結
以上是生活随笔為你收集整理的UR #3 核聚变反应强度( gcd )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客网多校第4场 D Another D
- 下一篇: java day10(续day9)