GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 890 Accepted Submission(s): 396
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.
(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
Source
Recommend
1 //15MS 208K 784 B G++ 2 /* 3 4 题意: 5 求[1,n]内与n公约数大于m的数的个数。 6 7 思路: 8 想了挺长时间,没什么思路。 9 后来得知结果为: 10 对于所有大于m的n的公约数k,求出所有n/k的欧拉数的和即为题解。 11 一个数m的欧拉数即为小于m且与m互质的数的和。 12 至于怎么得出的自己推一下吧。 13 14 */ 15 #include<stdio.h> 16 int euler(int n) 17 { 18 int ret=1; 19 for(int i=2;i*i<=n;i++) 20 if(n%i==0){ 21 n/=i;ret*=i-1; 22 while(n%i==0){ 23 n/=i;ret*=i; 24 } 25 } 26 if(n>1) ret*=(n-1); 27 return ret; 28 } 29 int cul(int n,int m) 30 { 31 int sum=0; 32 for(int i=1;i*i<=n;i++){ 33 if(n%i==0){ 34 if(n/i>=m && i*i!=n) 35 sum+=euler(i); 36 if(i>=m) 37 sum+=euler(n/i); 38 } 39 } 40 return sum; 41 } 42 int main(void) 43 { 44 int t,n,m; 45 scanf("%d",&t); 46 while(t--) 47 { 48 scanf("%d%d",&n,&m); 49 printf("%d ",cul(n,m)); 50 } 51 return 0; 52 }