zoukankan      html  css  js  c++  java
  • A. Orac and LCM

    For the multiset of positive integers s={s1,s2,,sk}s={s1,s2,…,sk}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of ss as follow:

    • gcd(s)gcd(s) is the maximum positive integer xx, such that all integers in ss are divisible on xx.
    • lcm(s)lcm(s) is the minimum positive integer xx, that divisible on all integers from ss.

    For example, gcd({8,12})=4,gcd({12,18,6})=6gcd({8,12})=4,gcd({12,18,6})=6 and lcm({4,6})=12lcm({4,6})=12. Note that for any positive integer xx, gcd({x})=lcm({x})=xgcd({x})=lcm({x})=x.

    Orac has a sequence aa with length nn. He come up with the multiset t={lcm({ai,aj}) | i<j}t={lcm({ai,aj}) | i<j}, and asked you to find the value of gcd(t)gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.

    Input

    The first line contains one integer n (2n100000)n (2≤n≤100000).

    The second line contains nn integers, a1,a2,,ana1,a2,…,an (1ai2000001≤ai≤200000).

    Output

    Print one integer: gcd({lcm({ai,aj}) | i<j})gcd({lcm({ai,aj}) | i<j}).

    Examples
    input
    Copy
    2
    1 1
    
    output
    Copy
    1
    
    input
    Copy
    4
    10 24 40 80
    
    output
    Copy
    40
    
    input
    Copy
    10
    540 648 810 648 720 540 594 864 972 648
    
    output
    Copy
    54
    
    Note

    For the first example, t={lcm({1,1})}={1}t={lcm({1,1})}={1}, so gcd(t)=1gcd(t)=1.

    For the second example, t={120,40,80,120,240,80}t={120,40,80,120,240,80}, and it's not hard to see that gcd(t)=40gcd(t)=40.

     题意:一个长度为 N 的数组,求 gcd {lcm({ai , aj}) | i < j}

      gcd_1=gcd( lcm(a1 , a2) , lcm(a1 , a3) ... lcm(a1 , an) ) 

      gcd_2=gcd( lcm(a2 , a3) , lcm(a2, a4) ... lcm(a2 , an) )

     ……

      gcd_n=gcd( lcm(an , 0))

    因为gcd_1一定是a1的倍数,所以:

     gcd_1=lcm (a1 , gcd (a2 , a3 , ... an) )

    #include <bits/stdc++.h>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const int mod = 998244353;
    const int N = 3e5+5;
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m%n);
    }
    ll lcm(ll m, ll n)
    {
        return m*n / gcd(m, n);
    }
    bool cmp(PII a, PII b)
    {
        return a.second<b.second;
    }
    int a[N],suf[N];
    int main() {
        int n,ans=0;
        cin>>n;
        rep(i,1,n)
        {
            cin>>a[i];
        }
        dec(i,n,1)
        {
            suf[i]=gcd(suf[i+1],a[i]);
        }
        rep(i,1,n)
        {
            ans=gcd(ans,lcm(suf[i+1],a[i]));
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    我来说说博客评论的事
    SWFUpload+Javascript仿163邮件上传文件
    如何暂停和终止线程
    分享我的数据处理类库,欢迎拍砖
    求数列两两之差,再求和
    poj 1006 中国剩余定理
    Poj算法做题顺序
    poj 1328
    ZOJ 3279
    poj 2352 树状数组
  • 原文地址:https://www.cnblogs.com/dealer/p/12912959.html
Copyright © 2011-2022 走看看