hdu 2588 GCD---欧拉函数

题意:

求有多少 x ( 1 <= x <= n ), 使得 gcd ( x, n ) >= m。

其中(2<=N<=1000000000, 1<=M<=N)。


解析:

设 x = q * d, n = p * d ,且 gcd ( x, n) = d 。

则 q 和 p 必然是互素的,题目要求 d >= m,所以只要找出当 d >= m时:

p = (n / d ),小于p,且与p互素的数有多少个就行了,这就相当于求x有多少个了。

这个数就是欧拉函数...

然后求得时候肯定不是一遍爆过去,而是像上次多校赛那题一样。

现在换一种写法,以后这样写开根号:

for (int i = 1; i * i <= n; i++)

这样就把复杂度降下来了,并且找到了所有p (●'◡'●)


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#include <climits>
#include <cassert>
#define LL long long

using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = 4 * atan(1.0);
const double ee = exp(1.0);

int euler_phi(int n)
{
    int m = sqrt(n + 0.5);
    int res = n;
    for (int i = 2; i <= m; i++)
    {
        if (n % i == 0)
        {
            res = res / i * (i - 1);
            while (n % i == 0)
                n /= i;
        }
    }
    if (1 < n)
        res = res / n * (n - 1);
    return res;
}

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    int ncase;
    scanf("%d", &ncase);
    while (ncase--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        int ans = 0;
        for (int i = 1; i * i <= n; i++)
        {
            if (n % i == 0)
            {
                if (i >= m)
                    ans += euler_phi(n / i);
                if (n / i != i && n / i >= m)
                    ans += euler_phi(i);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值