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