1012 Greatest common divisor and least common multiple issues

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

Output description Output Description

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

Sample input Sample Input

3 60

Sample output Sample Output

4

Idea: It's a violent enumeration. But notice the time problem:

Two points: One is the number of enumeration, because greatest common divisor is x0, that is, the number of enumerations must be a multiple of x0, so you can add x0 each time, so as to reduce unnecessary judgment.

The 2nd is: Calculate the greatest common divisor between two numbers here is the use of the method of division, the first use is from the x0 forward enumeration, so that the sure timeout.

Of course, there must be a better way to continue learning.

Code:

#include <stdio.h>
int is_maxmin (int n,int m,int x,int y)
{
int i,r,a,b;
A=x;
B=y;
r=b%a;
Seeking greatest common divisor by the method of dividing
while (r!=0)
{
B=a;
c=0;
r=b%a;
}
I=a;
int Min;
min=x*y/i;
if (min==m&&i==n)
{
return 1;
}
return 0;

}
int main ()
{
int n,m,i,j,count=0;
scanf ("%d%d", &n,&m);
for (I=n;i<=m;i=i+n)
{
for (J=n;j<=m;j=j+n)
{
if (Is_maxmin (N,M,I,J))
{
count++;
}
}
}
printf ("%d\n", count);
return 0;
}

1012 Greatest common divisor and least common multiple issues

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.