zoukankan      html  css  js  c++  java
  • H

    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)'.

    大体题意:问1-n有多少对数使得lcm=n;

    解题思路:

    先来看个知识点:

    素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

    for i in range(1,n):

    ei 从0取到ei的所有组合

    必能包含所有n的因子。

    现在取n的两个因子a,b

    a=p1 ^ a1 * p2 ^ a2 *..........*pn ^ an

    b=p1 ^ b1 * p2 ^ b2 *..........*pn ^ bn

    gcd(a,b)=p1 ^ min(a1,b1) * p2 ^ min(a2,b2) *..........*pn ^ min(an,bn)

    lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pn ^ max(an,bn)

    题解:

    先对n素因子分解,n = p1 ^ e1 * p2 ^ e2 *..........*pk ^ ek,

    lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pk ^ max(ak,bk)

    所以,当lcm(a,b)==n时,max(a1,b1)==e1,max(a2,b2)==e2,…max(ak,bk)==ek

    当ai == ei时,bi可取 [0, ei] 中的所有数  有 ei+1 种情况,bi==ei时同理。

    那么就有2(ei+1)种取法,但是当ai = bi = ei 时有重复,所以取法数为2(ei+1)-1=2*ei+1。
    除了 (n, n) 所有的情况都出现了两次  那么满足a<=b的有 (2*ei + 1)) / 2 + 1个

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 
     8 const int maxn=1e7+10;
     9 const int maxnn=1e6;
    10 int T;
    11 long long a;
    12 bool ip[maxn];
    13 unsigned int p[maxnn],num;//输入数据太大会导致超时
    14 
    15 void is_prime()
    16 {
    17     memset(ip,1,sizeof(ip));
    18     ip[0]=ip[1]=0;
    19     for(long long i=2;i<=maxn;i++)
    20     {
    21         if(ip[i])
    22         {
    23             p[num++]=i;
    24             for(long long j=i+i;j<=maxn;j+=i)
    25             {
    26                 ip[j]=0;
    27             }
    28         }
    29     }
    30 }
    31 
    32 int main()
    33 {
    34     ios::sync_with_stdio(false);
    35     cin>>T;
    36     is_prime();
    37     for(int i=1;i<=T;i++)
    38     {
    39         cin>>a;
    40         long long ans=1;
    41         for(int i=0;i<num&&p[i]*p[i]<=a;i++)
    42         {
    43             if(a%p[i]==0)
    44             {
    45                 int cnt=0;
    46                 while(a%p[i]==0)
    47                 {
    48                     a/=p[i];
    49                     cnt++;
    50                 }
    51                 ans*=(2*cnt+1);
    52             }
    53         }
    54         if(a>1) ans*=(2*1+1);
    55 
    56         cout<<"Case"<<" "<<i<<":"<<" "<<(ans+1)/2<<endl;
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    营山护照办理
    非北京人员 办理护照
    护照填写注意事项
    美国会议签证——我是正当理由去美国,我能支付(或有人为我支付)我在美国期间的所有费用,办完事我肯定回来, 邀请信,行程表这些材料齐全即可
    urllib2使用2
    python 异常
    python urllib和urllib2 区别
    python类继承
    gcc编译4个阶段
    Vim中如何全选并复制?
  • 原文地址:https://www.cnblogs.com/Fy1999/p/8948183.html
Copyright © 2011-2022 走看看