H - Pairs Forming LCM(唯一分解定理)

本文介绍了一个高效算法来解决特定数学问题:给定整数n,找出所有成对整数(i, j),使得它们的最小公倍数等于n。通过对n进行质因数分解,并利用数学性质减少搜索范围,该算法能在较短时间内得出答案。

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

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 }

 

转载于:https://www.cnblogs.com/Fy1999/p/8948183.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值