zoukankan      html  css  js  c++  java
  • LightOJ 1236 Pairs Forming LCM(算术基本定理)

     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

    题意:给一个n,有( 1<=i <= j <= n),求所有lcm(i , j)=n,满足条件的i,j的对数。

    题解:假设n= p1^a1 * p2^a2 * p3^a3...*pn-1^an-1*pn^an;

    对于其中的一项pi^ai来说,选择的两个数之一必须包含它,另外一个随意。。也就是说。。Pi^ai放在第一个数,也可以放在第二个数。不包含pi^ai的那个数字有(ai+1)种方案,所以一共有2*ai+2种方案,但是两边都选ai的时候会重复,所以对于第i个素因子有ai*2 +1种方案。所以ans = (2*a1 + 1)*(2 * a2 + 1).....*(2 * an + 1);其中除了(n , n),其它所有的都被计算了两次。所以ans = (ans + 1)/2

    代码:

    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    const int maxn = 1e7 + 10;
    typedef long long ll;
    bool is_not[maxn];
    int pri[maxn/10] , n;
    int tot;
    void init()
    {
        memset(is_not,0,sizeof(is_not));
       tot = 0;
       for(int i = 2; i < maxn;i++){
           if(!is_not[i]) pri[tot++] = i;
           for(int j = 0 ; j < tot&&pri[j]*i < maxn;j++){
               is_not[i * pri[j]] = 1;
               if(i%pri[j] == 0) break;
           }
       }
     
    }
    ll facor(ll n){
        ll n1 = n;
        ll cot = 0;
        ll cnt = 1;
        for(int i = 0 ;pri[i] * pri[i] <= n;i++){
                cot = 0;
                while(n%pri[i] == 0){
                    n/=pri[i];
                    cot++;
                }
            if(cot) cnt *= cot * 2 + 1;
        }
        if(n > 1) cnt *= 3;
        return (cnt + 1)>>1;
    }
    int main()
    {
        init();
        int t;
        scanf("%d",&t);
        for(int cas = 1; cas <= t;cas++)
        {
            scanf("%lld",&n);
            printf("Case %d: %lld
    ",cas,facor(n));
        }
    }
    View Code
  • 相关阅读:
    关于Windows程序设计的初步认识
    C++虚函数作用原理(二)——类的继承
    史诗级Java资源大全中文版
    马上加薪!测试,你的职业发展...
    你不知道的接口自动化测试!
    69道Spring面试题和答案,简单明了无套路
    大厂都在问的多线程面试题,你不了解下嘛?
    现代Java进阶之路必备技能——2019 版
    80后程序员降薪6K,预感中年危机来袭,准备跳槽却碰壁
    微服务、分布式、高并发都不懂,你拿什么去跳槽?
  • 原文地址:https://www.cnblogs.com/rtyfcvb/p/6624044.html
Copyright © 2011-2022 走看看