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
  • 相关阅读:
    JAVA线程池管理及分布式HADOOP调度框架搭建
    技术人员如何创业《二》 合伙人的模式
    JavaScript中的运动数学函数(持续更新)
    JavaScript中的加号
    JavaScript 函数绑定 Function.prototype.bind
    基于C#的波形显示控件的实现[附完整源码下载]
    JavaScript中的声明提升
    JavaScript & HTML5 Canvas 概览 更新时间201404111805
    《编写高质量代码——Web前端开发修炼之道》读后随笔
    B树/B+树/二叉搜索树/AVL树/红黑树
  • 原文地址:https://www.cnblogs.com/rtyfcvb/p/6624044.html
Copyright © 2011-2022 走看看