UR #3 核聚变反应强度( gcd )
生活随笔
收集整理的這篇文章主要介紹了
UR #3 核聚变反应强度( gcd )
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
tags: -分解質(zhì)因數(shù) , gcd
題目大意
給定\(n\)個(gè)數(shù),求\(a_1\)與\(a_i\)次小公約數(shù)
分析
易知次小公約數(shù)是\(\gcd\)的因數(shù),于是用\(\gcd\)除去它的最小質(zhì)因子即可。
沒有次小公約數(shù)的情況是\(\gcd = 1\),特判一下即可
直接枚舉的時(shí)間復(fù)雜度為\(O(n \sqrt a)\)
由于數(shù)據(jù)規(guī)模較大考慮優(yōu)化
由于是求\(sgcd(a_1,a_i)\)于是結(jié)果一定是\(a_1\)的質(zhì)因數(shù)組成,于是預(yù)處理\(a_1\)的質(zhì)因數(shù),然后每次處理時(shí)除去最小的即可,\(10^{12}< 2^{38}\)于是可以知道得到質(zhì)因數(shù)的個(gè)數(shù)小于\(38\)個(gè),于是時(shí)間復(fù)雜度就變?yōu)榱?span id="ozvdkddzhkzd" class="math inline">\(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; }轉(zhuǎn)載于:https://www.cnblogs.com/bobble/p/9445219.html
總結(jié)
以上是生活随笔為你收集整理的UR #3 核聚变反应强度( gcd )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客网多校第4场 D Another D
- 下一篇: java day10(续day9)