"codevs1012" greatest common divisor and least common multiple

Source: Internet
Author: User
Tags greatest common divisor

Title Description Description

Enter two positive integer x0,y0 (2<=x0<100000,2<=y0<=1000000) to find the number of p,q that meet the following conditions

Condition: 1.p,q is a positive integer

2. Require p,q to x0 for greatest common divisor, y0 as least common multiple.

Trial: The number of all possible two positive integers that satisfy the condition.

Enter a description input Description

Two positive integers x0,y0

Outputs description Output Description

The number of all possible two positive integers that satisfy the condition

Sample input to sample

3 60

Sample output Sample Outputs

4

Analysis:

P and Q's Greatest common divisor (GCD) is x, least common multiple (LCM) is Y

So P*q=x*y

Set P=x*i,q=x*j,i and J coprime

Then p*q= (x*i) * (x*j) =x*y, then there is i*j=y/x

We can enumerate I, starting with I=1, until i*i>y/x

If I is a factor of y/x

Then j= (y/x)/I

Again to determine whether I and J coprime

Because each of the two numbers in the smaller is I, the larger the number is J,i is the small root (y/x), J is greater than the radical (y/x) so does not repeat the calculation, that count to one time, the answer will accumulate 2.

Code:
#include <iostream>using namespace std;int gcd (int x,int y) {    return (X%Y==0?Y:GCD (y,x%y));} int main () {    int x,y,ans=0;    cin>>x>>y;    if (y%x) {        cout<<0;        return 0;    }    y=y/x;    for (int i=1; i*i<=y; i++)    {        if (Y%I==0&&GCD (i,y/i) ==1)            ans+=2;    }    Cout<<ans<<endl;}

  

"codevs1012" greatest common divisor and least common multiple

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.