1. 程式人生 > >HDU 4497 GCD and LCM

HDU 4497 GCD and LCM

print ... strong ret HERE sin you class ble

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3103 Accepted Submission(s): 1356


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.

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
//Pro: hdu 4497 GCD and LCM

//先寫點東西: 
//如果有非負整數a,b, 將他們分解質因數,得到:
//a=p1^k1 * p2^k2 * p3^k3 *...* pn^kn
//b=p1^q1 * p2^q2 * p3^q3 *...* pn^qn (p>0, q>=0, k>=0, k,q不同時為0)
//顯然地, 
//gcd(a,b)=p1^(min(k1,p1)) * p2(min(k2,p2)) *...* pn^(min(kn,qn))
//lcm(a,b)=p1^(max(k1,p1)) * p2(max(k2,p2)) *...* pn^(max(kn,qn))
//=> a*b=p1^(k1+q1) * p2^(k2+q2) * p3^(k3+q3) *...* pn^(kn+qn)=gcd(a,b) * lcm(a,b) //題意: //三個未知數x,y,z,它們的gcd為G,lcm為L,G和L已知,求(x,y,z)三元組的個數 //由上邊那個東東可以知道,將x,y,z,G,L質因數分解之後 //他們的相同的因子Pi的次數 ki_G<=ki_x,ki_y,ki_z<=ki_L, //且min(ki_x,ki_y,ki_z)==ki_G && max(x,y,z)==ki_L //所以x,y,z裏有兩個數的某一質因子的個數是固定的,分別等於ki_L或ki_G, //剩下的第三個數的該質因子的個數可以在[ki_G,ki_L]中任選 //(ki_g<ki_a<ki_l)的排列一共有6種,如果ki_a==ki_g或者ki_a==ki_l,各有三種排列,共2*3=6種 //那麽(x,y,z)的個數一共有6*(l-g-1)+2*3=6*(l-g)種 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> using namespace std; const int N=1e7+5; int T; int l,g,a; bool nprime[N]; int prime[N],cnt; void init() { nprime[1]=1; for(int i=2;i<N;++i) { if(!nprime[i]) prime[++cnt]=i; for(int j=1,d;j<=cnt&&(d=i*prime[j])<N;++j) { nprime[d]=1; if(i%prime[j]==0) break; } } } int ans; int main() { init(); scanf("%d",&T); while(T--) { scanf("%d%d",&g,&l); if(l%g) { puts("0"); continue; } l/=g; //把相同的因子約一下 ans=1; for(int i=1,k;i<=cnt;++i) { if(l%prime[i]) continue; k=0; while(l%prime[i]==0) { ++k; l/=prime[i]; } ans*=6*k; } printf("%d\n",ans); } return 0; }

HDU 4497 GCD and LCM