2588 欧拉函数的应用

D = GCD(X,N)>=M,所以就是N = P*D , X = Q*D,所以P和Q一定互质,所以可以利用欧拉函数求解.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;
LL euler ( LL x )
{
    LL res = x;
    LL temp = x;
    for ( int i = 2 ; i*i<= temp ; i++ )
    {
        if ( x%i == 0 )
            res -= res/i;
        while ( x%i==0 ) x/=i;
    }
    if ( x > 1 ) res -= res/x;
    return res;    
}

int t;
LL m,n;

int main ()
{
    scanf ( "%d" , &t );
    while ( t-- )
    {
        LL sum = 0;
        scanf ( "%lld%lld" , &n , &m );
        for ( int i = 1 ; i*i <= n ; i++ )
        {
            if ( n %i ) continue;
            if ( i >= m ) sum += euler( n/i );
            if ( (n/i)>=m && i != (n/i) )
                sum += euler( i );
        }
        cout << sum << endl;
    }
}


你可能感兴趣的:(C++,数学,欧拉函数)