Pairs Forming LCM (LCM+ 唯一分解定理)题解

这篇博客介绍了如何解决寻找满足lcm(a, b) = n条件的整数对(a, b)的问题,其中1 ≤ n ≤ 1014。博主探讨了唯一分解定理在解决此问题中的应用,并提供了计算此类配对数量的公式和优化方法。文章通过一系列样例解释了计算过程,展示了如何快速有效地找到满足条件的配对总数。" 113797899,9648070,PyTorch实战:猫狗分类模型训练与预测,"['深度学习', 'pytorch', '机器学习', '神经网络', '模型训练']

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

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

题意:

在a,b中(a,b<=n)(1 ≤ n ≤ 1014),有多少组(a,b)  (a<b)满足lcm(a,b)==n;

思路:

这里要学个新东西:快问问神奇海螺

首先,我们已经知道了唯一分解定理:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

那么,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)

现在我们就可以求解题目了。

要a,b的LCM是n,所以max(a1,b1)== e1,max(a2,b2)== e2以此类推到n,也就是说每一个ai,bi中至少有一个等于ei,求这种组合方式有多少。按上面的思路第pi组有2*(ei+1)种组合方式,但是还要再减去一种重复的 ai==bi==ei ,所以结果是 2*ei+1 种。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack> 
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
const int N=1e7+5;
const int NN=1e6;
const int MOD=1000; 
using namespace std;
bool prime[N];
ll p[NN];    //这里只能1e6不然就MLE了
int pn;
void get_prime(){
	pn=0;
	memset(prime,false,sizeof(prime));
	prime[0]=prime[1]=true;
	for(ll i=2;i<N;i++){
		if(!prime[i]){
			p[pn++]=i;
			for(ll j=i*i;j<N;j+=i){
				prime[j]=true;
			}
		}
	}
}
ll deal(ll n){
	ll res=1;
	for(ll i=0;i<pn && p[i]*p[i]<=n;i++){
		if(n%p[i]==0){
			int tmp=0;
			while(n%p[i]==0){
				tmp++;
				n/=p[i];
			}
			res*=(2*tmp+1);
		}
	}
	if(n>1) res*=3;	 
	res=res/2+1;
	return res;
}
int main(){
	get_prime();
	int T,num=1;
	ll ans,n;
	scanf("%d",&T);
	while(T--){
		scanf("%lld",&n);
		ans=deal(n);
		printf("Case %d: %lld\n",num++,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值