hdu_2588_GCD

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.

InputThe 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.OutputFor each test case,output the answer on a single line.Sample Input

3
1 1
10 2
10000 72

Sample Output

1
6
260

这是一道不错的题目,很有启发性。

假设x<=n,n=p*d,x=q*d.假设n与x的最大公约数为d,则能够推出p与q肯定是互质的,因为x<=n所以要求的就是p的欧拉函数值了,那么我们就转化成求满足:n=p*d,并且d>=m的p的欧拉函数值之和了。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define N 1000010
using namespace std;
typedef long long ll;
int prime[N];
bool vis[N];
int pn=0;
ll a[N];
long long p(long long  n){ //返回euler(n)
     long long  res=n,a=n;
     for(long long  i=2;i*i<=a;i++){
         if(a%i==0){
             res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
             while(a%i==0) a/=i;
         }
     }
     if(a>1) res=res/a*(a-1);
     return res;
}
int main()
{
    for (int i = 2; i < N; i++) {
        if (vis[i]) continue;
        prime[pn++] = i;
        for (int j = i; j < N; j += i)
            vis[j] = 1;
    }
    ll i,j,n,m;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll ans=0;
        scanf("%lld%lld",&n,&m);
        for(i=1;i*i<=n;i++)
        {
            if(n%i==0)
            {
                if(i>=m&&i*i!=n)
                    ans+=p(n/i);
                if(n/i>=m)
                    ans+=p(i);
            }
        }
        cout<<ans<<endl;

    }

}

  

posted @ 2017-11-26 21:27  会飞的雅蠛蝶  阅读(142)  评论(0编辑  收藏  举报