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
  • 相关阅读:
    make[1]: *** [/workopenwrt/trunk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/stamp/.tools_install_nnnnn] Error 2 make[1]: Leaving directory `/work/openwrt/trunk' make: *** [world]
    安卓端与开发板通信
    openwrt串口的使用
    .OpenWrt驱动程序Makefile的分析概述 、驱动程序代码参考、以及测试程序代码参考
    openwrt系统之字符设备驱动软件包加载、测试程序加载
    cc2530串口通信流程
    cc2530操作任务系统初始化分析
    总结OpenWrt系统基本操作方法
    Zigbee协议栈OSAL层API函数【转载】
    cc2530启动流程---广播发送数据
  • 原文地址:https://www.cnblogs.com/rtyfcvb/p/6624044.html
Copyright © 2011-2022 走看看