GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 986 Accepted Submission(s): 459
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.
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
Source
Recommend
题意:
输入G,L求有多少对数(x, y, z)满足gcd(x,y,z) = G lcm(x,y,z) = L
和上一个题类似
L/G质因数分解为p1^r1 * p2^r2 ... pn^rn
x/G分解为p1^a1 * p2^a2 ... pn^an
y/G z/G同理 为 ...pi^bi... ...pi^ci...
x/G y/G z/G 这三个数肯定互质(GCD性质) 所以 min(ai, bi, ci) ==0
而lcm(x/G, y/G, z/G) = L/G 所以 max(ai, bi, ci) == ri
所以根据组合数学可得对于pi^ri这一项有
一个选择ri 一个选择0 另外一个随便选择然后全排列就是 6*(ri+1) 然后减去重复的6种(C(3,2)为0 +C(3,2)为ri)
即 6*(ri+1) - 6 种方案数
然后因为每一项相互独立,乘法原理相乘即可
#include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <string> #include <map> #include <cmath> #include <queue> #include <set> using namespace std; //#define WIN #ifdef WIN typedef __int64 LL; #define iform "%I64d" #define oform "%I64d\n" #define oform1 "%I64d" #else typedef long long LL; #define iform "%lld" #define oform "%lld\n" #define oform1 "%lld" #endif #define S64I(a) scanf(iform, &(a)) #define P64I(a) printf(oform, (a)) #define P64I1(a) printf(oform1, (a)) #define REP(i, n) for(int (i)=0; (i)<n; (i)++) #define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++) const int INF = 0x3f3f3f3f; const double eps = 10e-9; const double PI = (4.0*atan(1.0)); const int maxn = 70000 + 20; int vis[maxn], prim[maxn]; void sieve(int n) { int m = (int) sqrt(n+0.5); memset(vis, 0, sizeof(vis)); for(int i=2; i<=m; i++) if(!vis[i]) { for(int j=i*i; j<=n; j+=i) { vis[j] = 1; } } } int getPrimeTable(int n) { sieve(n); int c = 0; for(int i=2; i<=n; i++) if(!vis[i]) prim[c++] = i; return c; } int main() { int T; int primNum = getPrimeTable(70000); scanf("%d", &T); while(T--) { int G, L; scanf("%d%d", &G, &L); if(L % G) { puts("0"); continue; } LL ans = 1; int x = L / G; int m = (int) sqrt(x + 0.5); for(int i=0; i<primNum && prim[i] <= m && x > 1; i++) { int t = 0; while(x % prim[i] == 0) { t++; x /= prim[i]; } if(t) { ans *= 6 * (t + 1) - 6; } } if(x > 1) ans *= 6; P64I(ans); } return 0; }