HDU 4497 GCD and LCM (数学,质数分解)

时间:2023-03-09 03:34:09
HDU 4497 GCD and LCM (数学,质数分解)

题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对。

析:数学题,当时就想错了,就没找出规律,思路是这样的。

首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n=L/G),即可,那么可以进行质因数分解,假设:

n = p1^t1*p2^t2*p3^t3;那么x, y, z,除以G后一定是这样的。

x = p1^i1*p2^i2*p3^i3;

y = p1^j1*p2^j2*p3^j3;

z = p1^k1*p2^k2*p3^k3;

那么我们可以知道,i1, j1, k1中最少一个t1,最多2个,0也是,考虑最大公因数,所以就会有下面三种方案。

0 0 t1         排列有三种

0 t1 t1        排列有三种

0 t1 1-t1-1  排列有(t1-1)*6种

所以加起来有t1*6种,那么只要找质因数的指数就好。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 56 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int main(){
int T; cin >> T;
while(T--){
int g, l;
cin >> g >> l;
if(l % g) printf("0\n");
else {
int gl = l / g;
int n = sqrt(gl+0.5);
int ans = 1;
for(int i = 2; i <= n && gl > 1; ++i){
if(gl % i == 0){
int cnt = 0;
while(gl % i == 0){
gl /= i;
++cnt;
}
ans *= cnt * 6;
}
}
if(gl != 1) ans *= 6;
cout << ans << endl;
}
}
return 0;
}