• HDU 2588 GCD (欧拉函数)


    Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

     Status

    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
     

    Source

    ECJTU 2009 Spring Contest
    题意:给定n和m,m小于n,求x在1到n之间,满足gcd(x,n)>=m。问这样的x有多少个,实际上x是从m到n之间的。
    题解:考察对欧拉函数本质的理解,欧拉函数指的是对于一个数来说小于等于该数的数中与其互质的数的个数。找出n的所有大于m的因子x,设phi(i)为i的欧拉函数值,那么答案就是phi(n/x)的和。比如第二组样例n=10,m=2,x=2,5,10,注意x=1是不满足条件的。答案就是phi(10/2)+phi(10/5)+phi(10/10)=4+1+1=6。phi(10/2)=4代表的是2*1=2 , 2*2=4 , 2*3=6 , 2*4=8 这四个数;phi(10/5)=1代表的是5*1=5;phi(10/10)=1代表的是10*1=10。那么为什么是这样呢?因为我们知道该因子x已经满足条件,比如2满足条件,我们可以让这个数变大为x*y,但一定要在10/2=5的范围之内,而且gcd(n,x*y)=x,因为x不能超过n,也就是把2乘以一个小于5的数y,而且这个y一定要与5是互素的。因为我们一定要保证gcd(n=2*5,2*y)=2,否则就出现重复了。举个例子,比如n=12,3是满足条件的,12/3=4,现在让3变大,2与4是不互素的我们让3*2=6,gcd(12,6)=6!=3,这就出现重复了就不对了,6不是现在算的时候。而欧拉函数值恰好是求一个数小于等于该数中与其互质的数的个数,所以用欧拉函数做就可以了,注意n的范围较大要用sqrt(n)算,因为我们在算小的这一侧因子的时候大的因子也会跟着出来,理解了欧拉函数的本质本题也就迎刃而解了。如描述有错误欢迎指正。
    #include <iostream>
    #include <cmath>
    using namespace std;
    int enlur(int n) //求欧拉函数
    {
        int ans=n;
        for(int i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                ans=ans/i*(i-1);
                while(n%i==0)
                    n/=i;
            }
        }
        if(n>1)
            ans=ans/n*(n-1);
        return ans;
    }
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            int n,m,ans=0;
            cin>>n>>m;
            int tmp=sqrt(n);
            for(int i=1;i<=tmp;i++)
            {
                if(n%i==0)
                {
                    if(i>=m)
                        ans+=enlur(n/i);
                    if(n/i>=m)
                        ans+=enlur(i);
                }
            }
            if(tmp*tmp==n&&tmp>=m) //注意得是tmp>=n
                ans-=enlur(tmp);
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    初识jQuery(2)
    初识jQuery(1)
    document和javaScript内置对象
    location
    history
    window对象
    注释、语法约定、函数和作用域
    运算符和typeof
    string
    数据恢复
  • 原文地址:https://www.cnblogs.com/Ritchie/p/5312463.html
Copyright © 2020-2023  润新知