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
  • 相关阅读:
    关于一道PHP面试题的解法
    ThinkPHP学习(二)
    ThinkPHP学习(一)
    Apache 创建虚拟主机目录和设置默认访问页面
    awk全集
    初识云计算&openstack
    Python collections
    Python 函数/高阶函数
    Python dic/set/迭代
    python matplotlib 图标绘制
  • 原文地址:https://www.cnblogs.com/rtyfcvb/p/6624044.html
Copyright © 2011-2022 走看看