HDU 2588 GCD (欧拉函数)

【传送门】:http://acm.hdu.edu.cn/showproblem.php?pid=2588

【题目大意】:给定N,M, 求有多少个x,1=<x<=N, 满足GCD(x,N)>= M

【题解】不妨设GCD(x,N)= k,   x = pk, N = qk. 显然,p,q互质

这样GCD(x,N)>= M 转化为 k>=M , N = qk

对于每一个q而言,它对应的p有多少个呢?显然,p必须是小于等于q的与q互质 的数(1=<x<=N),显然可以通过q的欧拉函数求出

那么我们可以枚举所有N的因子k,对应得出q,把所有的q的欧拉函数求和就是最终答案。

【代码】

#include <queue>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int Euler(int n){
    int ans = n;
    //这里的n在不断收缩  循环次数上界将越来越小 
    for(int i=2; i<=n; i++){
        if(n % i == 0){
            ans = ans - ans/i; 
        }
        //把该素因子除尽 
        while(n % i == 0)    n = n/i;
    }
    return ans;
}

int main(){

    int n,m,t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        int ans = 0 ;
        //枚举n的因子i 
        for(int i=1; i*i<=n; i++){
            if(n % i == 0 ){ //i是n的因子
                // i>=m才满足条件 
                if(i >= m)
                    ans += Euler(n/i);
                //当然公因子k也可以是n/i 
                if(i != n/i && n/i >= m)
                    ans += Euler(i); 
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/czsharecode/p/9607914.html