• lightoj 1236 唯一分解定理


    Pairs Forming LCM

    Find the result of the following code:

    long long pairsFormLCM( int n ) {
        long long res = 0;
        for( int i = 1; i <= n; i++ )
            for( int j = i; j <= n; j++ )
               if( lcm(i, j) == n ) res++; // lcm means least common multiple
        return res;
    }

    A straight forward implementation of the code may time out. If you analyze the code, you will find that the code actually counts the number of pairs (i, j) for which lcm(i, j) = n and (i ≤ j).


    Input

    Input starts with an integer T (≤ 200), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 1014).

    Output

    For each case, print the case number and the value returned by the function 'pairsFormLCM(n)'.

    Sample Input

    15

    2

    3

    4

    6

    8

    10

    12

    15

    18

    20

    21

    24

    25

    27

    29

    Sample Output

    Case 1: 2

    Case 2: 2

    Case 3: 3

    Case 4: 5

    Case 5: 4

    Case 6: 5

    Case 7: 8

    Case 8: 5

    Case 9: 8

    Case 10: 8

    Case 11: 5

    Case 12: 11

    Case 13: 3

    Case 14: 4

    Case 15: 2

    题意:求1到n(1e14)之内,有多少对数(i,j),其中i<=j,使得LCM(i,j)=n,LCM为最小公倍数。

    分析:参考这个博客https://www.cnblogs.com/DOLFAMINGO/p/8371570.html

    1.设pi为第i个质数。设两个数A、B,他们可表示为:A = p1^a1 * p2^a2…… ,B = p1^b1 * p2^b2……。

    那么他们的最小公倍数为:LCM(A, B) = p1^max(a1,b1) * p2^max(a2, b2)……

    2.对n进行质因数分解,得到:n = p1^c1 * p2^c2……。当 LCM(A, B) = n时,

    ci = max(ai, bi),即要么 ci = ai,要么ci = bi。

    3 当ci = ai时, bi的可选择范围为[0,ci]共ci+1种;同理当ci = bi时,ai也有ci+1种选择。

    但是 (ai=ci,bi=ci)被重复计算了一次,所以对于素数pi,总共有 2*ci+1种选择。

    所以,当不考虑A、B的大小时,总共有 ∏ (2*ci+1)对(A,B),使得 LCM(A, B) = n。

    4.再考虑回A、B的大小限制,即A<=B,可知除了A = B = n时,其他的组合都出现了两次,

    即(A,B)和(B,A)都存在,而我们只需要A<=B的那一个。

    总的来说,最终有 ((∏ 2*ci+1)+1)/2对 (A,B)满足条件。

    #include<cstdio>
    #include<string>
    bool a[9999998];
    int p[5000006],cnt=1;
    void prime()
    {
        for(int i=4;i<9999998;i+=2) a[i]=1;
        p[0]=2;
        for(int i=3;i<9999998;i++)
        {
            if(a[i]==0)
            {
                p[cnt++]=i;
                for(int j=i+i;j<9999998;j+=i) a[j]=1;
            }
        }
    }
    
    int main()
    {
        int T,cas=0;
        long long N;
        scanf("%d",&T);
        prime();
        while(T--)
        {
            scanf("%lld",&N);
            long long ans=1,temp;
            for(int i=0;i<cnt&&(long long)p[i]*(long long)p[i]<=N;i++)
            {
                temp=0;
                while(N%p[i]==0)
                {
                    temp++;
                    N/=p[i];
                }
                ans*=(2*temp+1);
            }
            if(N!=1) ans*=3;
            printf("Case %d: %lld
    ",++cas,(ans+1)/2);
        }
        return 0;
    } 
    View Code
  • 相关阅读:
    bootstrap table load属性
    Jquery中hide()、show()、remove()特性
    Jquery精准计算
    .nojekyll 文件是什么
    快来用 Nuxt 开发静态网站
    CI:持续集成
    把组件库发布到 npm
    JS的各种模块化规范
    打包发布:让组件库支持按需加载
    docz: 组件库文档 so easy!
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8677940.html
Copyright © 2020-2023  润新知