標籤:des style blog http color io os ar java
GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1092 Accepted Submission(s): 512
Problem DescriptionGiven 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.
InputFirst 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.
OutputFor each test case, print one line with the number of solutions satisfying the conditions above.
Sample Input2 6 72 7 33
Sample Output72 0
Source2013 ACM-ICPC吉林通化全國邀請賽——題目重現
題意:求滿足 gcd(x,y,z) = g , lcm(x,y,z) = l 的 組數
首先 l%g != 0 肯定是不行的,
我們對 l 和 g 經行素數分解
g = p1^a1 * p2 ^ a2 * p3 ^ a3 ...
l = p1^b1 * p2 ^ b2 * p3 ^ b3 ...
因為 l %g == 0 ;所以 ai <= bi 對於p1 素數,對於 x,y,z ,需要有一個數的素數分解 中 p1的次數為 ai ;一個數的素數分解 中 p1的次數為 bi 如果 ai == bi ,那麼 沒有選擇ai < bi 的選擇就是 C(1,3)*C(1,2)*(bi-a1+1) 公式就是 6*(bi-ai+1) 意思就是,三個裡面選一個取 ai,然後兩個裡面選一個選 bi ,第三個的任意選
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<set>#include<stack>#include<map>#include<ctime>#include<bitset>#define LL long long#define mod 1000000007#define maxn 110#define pi acos(-1.0)#define eps 1e-8#define INF 0x3f3f3f3fusing namespace std;bool check(LL i ){ for(int j = 2 ; j*j <= i ;j++)if(i%j == 0) return false; return true;}int main(){ int j,i,l,g; int T,ans1,u,v; LL ans; cin >> T ; while(T--) { scanf("%d%d",&g,&l ) ; if(l%g !=0)puts("0") ; else { l /= g ; ans=1; for( LL i = 2 ; i*i <= l ;i++)if(l%i==0&&check(i)) { v = 0; while(l%i==0) { v++; l /= i ; } ans *= v*6 ; } if(l !=1) ans *= 6 ; printf("%I64d\n",ans); } } return 0 ;}View Code
hdu 4497 GCD and LCM