HDU4135

题目大意:求在[A,B]区间中,与n互质的的数的个数。( 1AB1015 1n108

简要题解:
f(x) 表示 1 ~n中与n不互质的的数的个数,我们可以将n质因数分解,然后容斥即可

AC code:

#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int, int> pii;

inline LL read()
{
    LL x = 0; int v = 1, c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) if(c == '-') v = -1;
    for(;c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    return x * v;
}

template<typename T>
inline void write(T x, int ch = 10)
{
    int s[20], t = 0;
    if(x < 0) { x = -x; putchar('-'); }
    do s[t++] = x % 10; while(x /= 10);
    while(t--) putchar('0' + s[t]);
    putchar(ch);
}

const int delta[] = {-1, 1};

int n;
LL A, B;
vector<int> p;

void decompose(int x)
{
    p.clear();
    int m = sqrt(x + 0.5);
    for(int i = 2; i <= m; ++i)
    {
        if(x % i == 0)
        {
            p.PB(i);
            while(x % i == 0)
            {
                x /= i;
            }
        }
    }
    if(x > 1) p.PB(x);
}

LL solve(LL x)
{
    LL ans = 0;
    for(int i = 1; i < (1 << p.size()); ++i)
    {
        int res = 1, cnt = 0;
        for(int j = 0; j < p.size(); ++j)
        {
            if(i & (1 << j))
            {
                res *= p[j];
                cnt++;
            }
        }
        ans += delta[cnt&1] * x / res;
    }
    return x - ans;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("hdu4135.in", "r", stdin);
    freopen("hdu4135.out", "w", stdout);
#endif

    int T = read(), kase = 0;
    while(T--)
    {
        A = read(); B = read(); n = read(); decompose(n);
        printf("Case #%d: ", ++kase); write(solve(B) - solve(A-1));
    }

#ifndef ONLINE_JUDGE
    fprintf(stderr, "Run Time: %dms\n", clock());
    fclose(stdin), fclose(stdout);
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值