分解质因数求lcm

https://ac.nowcoder.com/acm/contest/6037/E

#include<bits/stdc++.h>
typedef long long ll ;
#define int ll
using namespace std ;
const int maxn = 1e5+1;
const int mod = 1e9+9;
int prime[10009] , len ;
bool is[100009];
int a[maxn];
int pr[maxn];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}

int quickpow(int a , int b){
    int ans = 1 ;
    while(b){
        if(b&1){
            ans = ans * a % mod ;
        }
        b >>= 1 ;
        a = a * a % mod ;
    }
    return ans ;
}
void sieve(int x){
    for(int i = 2 ; i <= x ; i++){
        if(!is[i]) prime[++len] = i ;
        for(int j = 1 ; prime[j] * i <= x ; j++){
            is[prime[j]*i] = 1 ;
            if(i % prime[j] == 0) break;
        }
    }
}

void work(int x){
    for(int i = 1 ; ; i++){
        if(!is[x]) break;
        int p = prime[i];
        if(x % p == 0){
            int cnt = 0 ;
            while(x % p ==0){
                x /= p ;
                cnt++;
            }
            pr[p] = max(pr[p] , cnt);
        }
    }
    if(x > 1) pr[x] = max(pr[x] , (ll)1);
}

signed main(){
    sieve(100000);
    int n ;
    n = read();
    for(int i = 1 ; i <= n ; i++){
        a[read()] = 1 ;
    }
    for(int i = 1 ; i <= 1e5 ; i++){
        if(a[i]){
            work(i);
        }
    }
    int ans = 1 ;
    for(int i = 1 ; i <= 1e5 ; i++){
        if(pr[i]){
            ans = ans * quickpow(i , pr[i]) % mod;
        }
    }
    cout << ans << endl;
}

猜你喜欢

转载自www.cnblogs.com/nonames/p/13387960.html
今日推荐