hdoj 2588GCD(欧拉函数)

来源:互联网 发布:存储卡数据修复 编辑:程序博客网 时间:2024/05/29 21:29

GCD

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


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
31 110 210000 72
 

Sample Output
16260
 
分析:假设x<=n,n=p*d,x=q*d.假设n与x的最大公约数为d,则能够推出p与q肯定是互质的,因为x<=n所以要求的就是p的欧拉函数值了,那么我们就转化成求满足:n=p*d,并且d>=m的p的欧拉函数值之和。
<span style="font-size:18px;">#include<cstdio>#include<cstring> #include<cmath>#include<cstdlib>#include<algorithm>using namespace std;int Euler(int n){    int ret=n,i;    for(i=2;i<=sqrt(n);i++)     if(n%i==0)     {      ret=ret/i*(i-1);      while(n%i==0)        n=n/i;         }     if(n>1)       ret=ret/n*(n-1);       return ret;}int main(){  int t;  scanf("%d",&t);  while(t--)  {      int n,m,i;      scanf("%d%d",&n,&m);    __int64 sum=0;      for(i=1;i<=sqrt(n);i++)        if(n%i==0)        {          if(i>=m)         sum+=Euler(n/i);         if(i*i!=n&&n/i>=m)//1.i*i!=n是判断前面是否加过 2.注意这儿的n/i>=m不能丢         sum+=Euler(i);      }    printf("%I64d\n",sum);  }  return 0;}</span>

0 0
原创粉丝点击