Zoj 1577-GCD & LCM

Source: Internet
Author: User

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 ;}

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.