Hdu 4497 GCD and LCM decomposition of number theory primes

Source: Internet
Author: User
Tags greatest common divisor

GCD and LCMTime limit:2000/1000 MS (java/others) Memory limit:65535/65535 K (java/others)
Total submission (s): 1339 Accepted Submission (s): 607


Problem Descriptiongiven-positive integers G and L, could you tell me how many solutions of (x, Y, z) there is, Satis Fying 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 different solutions.
Inputfirst line comes an integer t (t <=), telling the number of test cases.
The next T lines, each contains, positive 32-bit signed integers, G and L.
It's guaranteed that each answer would fit in a 32-bit signed integer.
Outputfor each test case, print one line with the number of solutions satisfying the conditions above.
Sample Input

Sample Output
72 0


Links: http://acm.hdu.edu.cn/showproblem.php?pid=4497


Test instructions: Each case gives you two numbers G and L. Then find out how many different (x, Y, z) gcd are G, and the LCM is L.


Procedure: Decomposition of prime g,l.


First l%g! =0 that must be output 0.

First case 6 72

After decomposition 6 has a 2, a 3.

72 after decomposition there are 3 2, 2 3.


Then for the prime number 2, 6 of a 2 is the lower limit, and 72 of 3 2 is the upper limit. X, Y, z must have a number to decompose a prime 2, must have another number to decompose 3 2, and then the remaining number of the 2 must be divided between "1,3".

Then it can be introduced for the number of prime 2 to X, Y, Z assigned primes 2 Total has 6+ (3-1-1) *6 species Division. The preceding 6, which represents the third number, is equal to the upper or lower bounds such as 2 2 8 or 2 8 8.  Since there are two numbers equal, their permutation combinations are 3 kinds.   So it's 2*3=6 species.   Then the following is the case of three numbers are different, 3-1-1 =1 1 to 3 is a number, that is, 2, that is, the allocation of two prime number 2. Then 2 4 8 are arranged in combination, three are different, and the permutations are grouped into 6.

So there are 12 kinds of allocation methods for prime number 2.

Then allocate 3, a total of 6.


Multiply the number of allocation methods for each prime 12*6=72. Is the total number of programs.


Obviously for this problem, can only use decomposition l/g, and then for L each prime number of the lower limit is all 0.


#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h># Include <string> #include <iostream> #include <algorithm>using namespace std; #include <stack># Include <queue> #include <vector> #include <deque> #include <set> #include <map> #define LL __int64__int64 gcd (__int64 A,__int64 b) {if (b==0) return A;return gcd (b,a%b);} __int64 LCM (__int64 A,__int64 b) {return a/gcd (b) *b;} #define N 10000000//prime[0,primenum) ll prime[1000000], Primenu  M  BOOL ISPRIME[N+10];     <=max_prime prime void Prime (ll max_prime) {primenum = 0;      Isprime[0] = isprime[1] = 0;      ISPRIME[2] = 1;      prime[primenum++] = 2;      for (ll i = 3; I <= max_prime; i++) isprime[i] = i&1; for (ll i = 3; I <= max_prime; i+=2) {if (Isprime[i]) prime[primenum++] = i;              for (ll j = 0; J < Primenum; J + +) {if (prime[j] * i > Max_prime) break;              Isprime[prime[j]*i] = 0;          if (i%prime[j] = = 0) break;  }}} ll fen_g[1000000];ll fen_l[1000000];ll vis[1000000];ll wei[1000000];int Main () {PRIME (100000); __int64 t,g,l; scanf ("%i64d", &t), while (t--) {scanf ("%i64d%i64d", &g,&l), if (l%g!=0) printf ("0\n"), else {memset (fen_g,0 , sizeof fen_g); memset (fen_l,0,sizeof fen_l); memset (vis,0,sizeof vis); L=l/g;__int64 Tem_g=g;__int64 Tem_l=l;__int64 Ji=0;for (__int64 i=0;i<primenum;i++) {while (tem_l%prime[i]==0) {tem_l/=prime[i];if (Fen_l[i]==0&&vis[i] ==0) {vis[i]=1;wei[ji++]=i;}  fen_l[i]++;//Upper Limit}}if (tem_l!=1) {wei[ji++]=primenum;vis[primenum]=1;fen_l[primenum]++;} ll Ans=1;for (__int64 i=0;i<ji;i++) {__int64 ww=wei[i]; __int64 Cnt=fen_l[wei[i]]; if (cnt==1) {ans*=6;} elseans*=3*2+ (cnt-1) *6; }printf ("%i64d\n", ans);}} return 0;  }





Hdu 4497 GCD and LCM decomposition of number theory primes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.