LightOJ - 1236 - Pairs Forming LCM--唯一分解定理--计算 LCM

Description

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 ,i,j 在0-N内,计算 lcm(i,j) = N 的对数【其中:i < j】

 

思路:

  对n进行素数分解,a和b与n对应分解
     n=p1^e1*p2^e2*...*pk^ek;
     a=p1^a1*p2^a2*…*pk^ak
     b=p1^b1*p2^b2*…*pk^bk
  对于a,b它们的最小公倍数为n,则a1<=e1,b1<=e1,则(a1,b1)有2*(e1+1)-1对则总的对数有t=(2*e1+1)*(2*e2+1)....,其中除了(n,n),
  其他的都重复了两次则答案为(t+1)/2.  
  注意由于n的范围是10^14,因此素数筛到10^7,然而还是会漏过素因子,但显然只会漏过最多一个素因子,因为sqrt(10^14)==10^7,如
  果有两个大于10^7的素因子,根据素数分解公式,必定含有这两个大于10^7的素因子,相乘一定大于10^14大于n,与分解等式矛盾。因此只需最后特判最后一个即可。

 

代码:

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <algorithm>
#include <cmath>
#include<fstream>
#define LL long long
#define INF 0x7fffffff
#define MOD 1000000007
#define Pair pair<LL, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1.0)
using namespace std;
const int maxn = 1e7+10;
//在素数打表的过程中,用下边注释了的方法会超时
//int prime[maxn+1];
//素数打表
//void getPrime()
//{
//    memset(prime,0,sizeof(prime));
//    for(int i=2;i<=maxn;i++)
//    {
//        if(!prime[i]) prime[++prime[0]]=i;
//        for(int j=1;j<=prime[0]&&prime[j]<=maxn/i;j++)
//        {
//            prime[prime[j]*i]=1;
//            if(i%prime[j]==0) break;
//        }
//    }
//}
//用这种方法就 AC 了
int prime[700010], k;
bool Isprime[maxn];
void Prime()
{
    k = 0;
    memset(Isprime, true, sizeof(Isprime));
    Isprime[1] = false;
    for(int i = 2 ; i < maxn ; i++)
    {
        if(Isprime[i])
        {
            prime[k++] = i;
            for(int j = 2 ; i * j < maxn ;j++)
                Isprime[i * j] = false;
        }
    }
}

int main()
{
//需要注意:在使用文件流的时候,在提交代码的过程中将 1 该为 0
#if 0
    freopen("in.txt","r",stdin);
#endif //1
    int T;
    LL n;
    Prime();
    scanf("%d",&T);
    for(int kase=1;kase <= T;kase++){
        LL ans = 1;
        scanf("%lld",&n);
        for(int i=0;i<k&&prime[i]*prime[i]<=n;i++){
            int x = 0;
            if(n%prime[i]==0){
                while(n%prime[i]==0){
                    x++;
                    n/=prime[i];
                }
            }
            ans*=(x*2+1);
        }
        if(n>1){
            ans*=3;
        }
        cout << "Case " << kase << ": ";
        cout << ans/2+1 << endl;
    }
    return 0;
}

 

posted @ 2017-05-12 19:50  渣渣技术狗  阅读(61)  评论(0)    收藏  举报