hdu-2588-GCD欧拉函数改编

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<=X<=N 满足GCD(X,N)>=M.两者最大公约数大于M

思路:if(n%p==0 && p>=m)  那么gcd(n,p)=p,
      求所有的满足与n的最大公约数是p的个数,
      就是n/p的欧拉值。
      因为与n/p互素的值x1,x2,x3....
      满足 gcd(n,x1*p)=gcd(n,x2*p)=gcd(n,x3*p)....
      枚举所有的即可。
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int Euler(int n)
{
    int i,temp=n;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            while(n%i==0)
            n=n/i;
            temp=temp/i*(i-1);
        }
    }
    if(n!=1)
    temp=temp/n*(n-1);
    return temp;
}
void make_ini(int n,int m)
{
    int i,sum=0;
    for(i=1;i*i<=n;i++)//!!
    {    
        if(n%i==0)//i是n的约数;
        {
          if(i>=m)  
          sum=sum+Euler(n/i);//判断,变形gcd(n/i,x/i)=1利用欧拉函数;
          if((n/i)!=i && (n/i)>=m)//!!这个是对称性
          sum=sum+Euler(i);    //例如n=100;i从1到10假设m=3若4可以的话(4可以),则25也行。但是i只取到sqrt(n)
        }
    }
    printf("%d\n",sum);
}

int main()
{
    int T,n,m;
    while(scanf("%d",&T)>0)
    {
        while(T--)
        {
            scanf("%d%d",&n,&m);
            make_ini(n,m);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/81503588