Problem Description
Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
Input
First line comes an integer T (T <= 12), telling the number of test cases. The next T lines, each contains two positive 32-bit signed integers, G and L. It’s guaranteed that each answer will fit in a 32-bit signed integer.
Output
For each test case, print one line with the number of solutions satisfying the conditions above.
Sample Input
2 6 72 7 33
Sample Output
72 0一.问题分析
这道题很有代表性,通过这道题可以从另一种观点看待gcd和lcm分析:这题明显要用唯一分解定理来做。gcd(x,y,x)=G,lcm(x,y,z)=L;
x=p1^a1*p2^a2……ps^as;
y=p1^b1*p2^b2……ps^bs;
z=p1^c2*p2^c2……ps^cs;
G,L已知,满足条件:
G=p1^min(a1,b1,c1)……ps^min(as,bs,cs)=p1^e1……ps^es
L=p1^max(a1,b1,c1)……ps^max(as,bs,cs)=p1^h1……ps^hs
则满足 ei=min(ai,bi,ci) hi=max(ai,bi,bi),考虑组合数。
考虑有序数对,对于每个(ei,hi) ,让 (x,y,z)固定两个数,让一个数变化有 6种,即 A(3,2)=6,每一种有(hi-ei+1)种。
然后考虑重复情况,每次,只要(x,y,z)中只出现ei,hi的情况重复,总共重复6次。即 6*(hi-ei+1)-6=6*(hi-ei)种。
即 6*(h1-e1)*6(h2-e2)……,由于是幂指数相减,考虑L/G就可。如果不能整除,即没有(x,y,z)满足条件。公式推出,题目就好做了!二.漫长的debug过程
首先我的解决是先构造出来素数表,然后利用素数表来进行。。。。。,然而先不说利用素数表和直接分解质因数那个快,但是这个思路写起来还是很麻烦的。然后还有就是要对数学公式进一步化简之后只用分解L/g就好了,这样就会很简单了啊。。。。#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; const int maxn=1e7+5; int factor[maxn],counter; void init(int n) { counter=0; for(int i=2;i<=sqrt(n)+0.0;i++) { if(n%i==0) { counter++; factor[counter]=0; while(n%i==0) { factor[counter]++; n/=i; } } } if(n!=1) factor[++counter]=1; } int main() { //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin); int T; cin>>T; for(int t=1;t<=T;t++) { int l,g; cin>>g>>l; if(l%g!=0) { cout<<"0"<<endl; continue; } int k=l/g; init(k); long long sum=1; for(int i=1;i<=counter;i++) { sum*=6*factor[i]; } cout<<sum<<endl; } }
求解特定数学问题
本文介绍了一种通过质因数分解解决特定数学问题的方法,该问题要求找出满足给定最大公约数和最小公倍数的所有三元组(x, y, z)的数量。
2712

被折叠的 条评论
为什么被折叠?



