HDU 2588 GCD------欧拉函数变形

GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 812    Accepted Submission(s): 363


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
 1 /*

 2 题意:求1<=X<=N 满足GCD(X,N)>=M.

 3 

 4 思路:if(n%p==0 && p>=m)  那么gcd(n,p)=p,

 5       求所有的满足与n的最大公约数是p的个数,

 6       就是n/p的欧拉值。

 7       因为与n/p互素的值x1,x2,x3....

 8       满足 gcd(n,x1*p)=gcd(n,x2*p)=gcd(n,x3*p)....

 9       

10       枚举所有的即可。

11 */

12 

13 #include<stdio.h>

14 #include<stdlib.h>

15 #include<string.h>

16 

17 

18 int Euler(int n)

19 {

20     int i,temp=n;

21     for(i=2;i*i<=n;i++)

22     {

23         if(n%i==0)

24         {

25             while(n%i==0)

26             n=n/i;

27             temp=temp/i*(i-1);

28         }

29     }

30     if(n!=1)

31     temp=temp/n*(n-1);

32     return temp;

33 }

34 

35 void make_ini(int n,int m)

36 {

37     int i,sum=0;

38     for(i=1;i*i<=n;i++)//!!

39     {

40         if(n%i==0)

41         {

42           if(i>=m)

43           sum=sum+Euler(n/i);

44           if((n/i)!=i && (n/i)>=m)//!!

45           sum=sum+Euler(i);

46         }

47     }

48     printf("%d\n",sum);

49 }

50 

51 int main()

52 {

53     int T,n,m;

54     while(scanf("%d",&T)>0)

55     {

56         while(T--)

57         {

58             scanf("%d%d",&n,&m);

59             make_ini(n,m);

60         }

61     }

62     return 0;

63 }

 

 

你可能感兴趣的:(HDU)