GCD & LCM
Time Limit: 2 Seconds Memory Limit: 65536 KB
Given x and y (2 <= x <= 100,000, 2 <= y <= 1,000,000), you are to count the number of p and q such that:
1) p and q are positive integers;
2) GCD (p, q) = x;
3) LCM (p, q) = y.
Input
X and y, one line for each test.
Output
Number of pairs of p and q.
Sample Input
3 60
Sample Output
4
Analysis: Number theory + enumeration. Set p = ax, q = bx; Because GCD (p, q) = x: GCD (ax, bx) = x; can be released, GCD (a, B) = 1; and because p * q/x = y; therefore, you can use these two conditions for enumeration.
The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> using namespace std; int gcd (int a, int B) {return B = 0? A: gcd (B, a % B);} int main () {# ifdef test freopen ("sample.txt", "r", stdin); # endif int x, y; while (scanf ("% d", & x, & y )! = EOF) {if (y % x) {printf ("0 \ n"); continue;} int n = y/x, cct = 0; double num = sqrt (n) + 0.5; for (int I = 1; I <num; I ++) if (! (N % I) & gcd (I, n/I) = 1) + + cct; cct * = 2; // p, q interchangeable, so multiply by 2 if (x = y) printf ("% d \ n", cct-1); else printf ("% d \ n", cct );} return 0 ;}