杭电2588-GCD

来源:互联网 发布:金融互助网站源码 编辑:程序博客网 时间:2024/06/05 02:14

GCD

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


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
/*我们可以在sqrt(n)的时间内找到n的所有约数,而这些约数和n的最大公约数就是这个约数本身我们可以寻找x>=m且x是n的约数,最后答案就是每一个大于m的约数xans=sigma(eular(n/x))可以大概的说一下为什么这样处理因为对于x的倍数和n取最大公约数,则其值完全可能大于x。那么可以证明出,必然在m<=k*x<=n之间一定存在eular(n/x)个数与n的最大公约数是x*/#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<set>#include<map>#include<string>#include<cstring>#include<iomanip>const int MAX=100000;__int64 divsor[MAX];__int64 top;using namespace std;void Div(__int64 n){    __int64 i;    for(i=1;i*i<n;i++)    {        if(n%i==0)        {            divsor[top++]=i;            divsor[top++]=n/i;        }    }    if(n%i==0)    divsor[top++]=i;    sort(divsor,divsor+top);}__int64 eular(__int64 n){    __int64 i;    __int64 ans;    ans=n;    for(i=2;i*i<=n;i++)    {        if(n%i==0)        {            ans-=ans/i;            while(n%i==0)            n/=i;        }    }    if(n>1)    ans-=ans/n;    cout<<ans<<endl;    return ans;}int main(){    int t;    __int64 n,m,i,sum;    cin>>t;    while(t--)    {        cin>>n>>m;        top=0;        sum=0;        Div(n);        for(i=0;i<top;i++)        {            if(divsor[i]>=m)            {                //sum+=1;                sum+=eular(n/divsor[i]);            }        }        cout<<sum<<endl;    }    return 0;}


原创粉丝点击