LightOJ - 1236 Pairs Forming LCM 合数分解

本文探讨了如何高效计算使得最小公倍数等于给定数值n的所有整数对(i,j)的数量,通过质因数分解的方法减少计算复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LightOJ - 1236 Pairs Forming LCM 合数分解

一、题目

LightOJ - 1236 Pairs Forming 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

二、题目大意

求lcm[i,j]=n,i 和 j 的对数,其中i,j在[1,n]之间且i<=j,n<=1e14
三、解题思路
将n进行合数分解,得到素数表达式n=p^1^e1* p2^e2* p3^e3* p4^e4…pr^er。则对i=p1^e1’* p2^e2’* p3^e3’* p4^e4’…pr^er’,j=p1^e1”* p2^e2”* p3^e3”p4^e4”…pr^er”,lcm(i,j)=p1^max(e1’,e1”) p2^max(e2’,e2”) * …pr^max(er’,er”) ,则只需满足max(e1’,e1”)=e1, max(e2’,e2”)=e2,…, max(er’,er”)=e2 即可。若不考虑i,j大小关系,则对每个位置有2(2 ei+1)-1种选法, ans=ni==0(2(2ei+1)1) ,考虑i,j大小关系,i==j时,i==j==n,只有一种情况,其他情况i < j, i > j会被统计两次,一次答案为 (ans1)2+1 即: (ans+1)2
四、代码实现

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<list>
    #include<algorithm>
    #include<vector>
    #include<string>
    #include<fstream>
    //#pragma comment(linker,"/STACK:1024000000,1024000000")
    using namespace std;
    const int N=1e5+5;
    const int MAXN=1e7+5;
    int prime[MAXN/5];
    bool vis[MAXN];
    int cnt=0;
    void getPrime()
    {
        for(int i=2;i<MAXN;i++)
        {
            if(!vis[i]) prime[++cnt]=i;
            for(int j=1;j<=cnt;j++)
            {
                if(i*prime[j]>=MAXN) break;
                vis[i*prime[j]]=1;
                if(i%prime[j]==0) break;
            }
        }
        prime[0]=cnt;
    }
    long long factor[200][2];
    int fatCnt;
    int getFactors(long long x)
    {
    fatCnt=0;
    long long tmp=x;
    for(int i=1;i<=cnt&&prime[i]<=tmp/prime[i];i++)
    {
    factor[fatCnt][1]=0;
    if(tmp%prime[i]==0)
    {
    factor[fatCnt][0]=prime[i];
    while(tmp%prime[i]==0)
    {
    factor[fatCnt][1]++;
    tmp/=prime[i];
    }
    fatCnt++;
    }
    }
    if(tmp!=1)
    {
    factor[fatCnt][0]=tmp;
    factor[fatCnt++][1]=1;
    }
    return fatCnt;
    }
    long long n;
    int main()
    {
        getPrime();
    //    cout<<prime[cnt]<<endl;
        int t,tt=0;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%lld",&n);
            getFactors(n);
            long long ans=1;
            for(int i=0;i<fatCnt;i++)
            {
                ans*=2*(factor[i][1]+1)-1;
            }
            printf("Case %d: %lld\n",++tt,(ans+1)/2);
        }
       return 0;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值