GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 605 Accepted Submission (s): 268
Problem Description
The greatest common divisor GCD (a, B) of two positive integers a and B, sometimes written (a, B), is the largest divisor common to a and B, For example, (1, 2) = 1, (12, 18) = 6.
(A, B) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1 <= X <= N and (X, N)> = M.
Input
The first line of input is an integer T (T <= 100) representing the number of test cases. the following T lines each contains two numbers N and M (2 <= N <= 1000000000, 1 <= M <= N), representing a test case.
Output
For each test case, output the answer on a single line.
Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
[Cpp]
/*
Question: Enter the number of cases
Input n m indicates the number of the numbers from 1 to n and the number of n's common appointments greater than m
Ideas:
First, find all the common approx. k of n greater than m, and then obtain the number of phi (Euler's function) of each corresponding n/k, that is, the number of ing between n/k and n/k.
Then, if these numbers are mutually qualitative with n/k and less than n/k, then the number of these numbers and n/k are multiplied by k and then becomes the number of k with n public approx. (k> m)
Adding all phi (n/k) is the answer. Of course, this is a reference to people's whining .............
In addition, I have a small question: how can we ensure that these numbers are not repeated? For example, If k1 k2 is an approximate number of n, then if the two numbers are different from n/k1 n/k2
Then multiply the values by k1 and k2 respectively to a number. How can this problem be solved? Ask the experts to leave a statement to prove why they won't repeat.
*/
# Include <stdio. h>
# Include <math. h>
Int num [40000], cnt2;
Int phi (int x) // is the formula
{
Int I, res = x;
For (I = 2; I <(int) sqrt (x * 1.0) + 1; I ++)
If (x % I = 0)
{
Res = res/I * (I-1 );
While (x % I = 0) x/= I; // ensure that I must be a prime number.
}
If (x> 1) res = res/x * (x-1); // be careful not to overflow here
Return res;
}
Int main ()
{
Int I, Cas;
Scanf ("% d", & Cas );
While (Cas --)
{
Int n, m;
Scanf ("% d", & n, & m );
Cnt2 = 0; int s = 0;
For (I = 1; I * I <n; ++ I) // finds all the approx. n.
If (n % I = 0)
{
// If (I> = m)
// S + = phi (I );
// If (n/I> = m)
// S + = phi (n/I );
If (I> = m)
Num [cnt2 ++] = I;
If (n/I> = m)
Num [cnt2 ++] = n/I;
}
If (I * I = n & n % I = 0 & I> = m) num [cnt2 ++] = I;
For (I = 0; I <cnt2; ++ I)
S + = phi (n/num [I]);
Printf ("% d \ n", s );
}
Return 0;
}