hdu 4135


[A,B] 内与 n 互质的数的个数。

1AB10151n109


n109 ,所以对 n 的质因子枚举子集,容斥原理计算。


因为 235711131719232931>109

所以 n 的不同质因子最多 11 个, 211 的枚举是可以承受的。


时间复杂度: O(n+2p)    ( p n 的质因子个数 )


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}
#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)

const int size = 31624, add[2] = {-1, 1};
const double eps = 1e-4;

int n;
long long A, B, ans;

int prime[size], tot;

void prework()
{
    int sz = sqrt(n) + eps, tp = n;

    tot = 0;

    REP(i, 2, sz)
    {
        if(!(tp % i))
        {
            while(!(tp % i)) tp /= i;

            prime[tot++] = i;

            sz = std::min(sz, tp);
        }
    }

    if(tp != 1) prime[tot ++] = tp;
}

long long solve(long long S)
{
    long long ret = S;

    REP(i, 0, (1<<tot) - 1)
    {
        int poi = 1, cnt = 0;

        REP(j, 0, tot - 1)
            if((i >> j)&1)
            {
                poi *= prime[j];
                cnt++;
            }

        ret += (S/poi) * add[cnt&1];
    }

    return S - ret;
}

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

    read(T);

    REP(CaseNum, 1, T)
    {
        printf("Case #%d: ", CaseNum);

        read(A), read(B), read(n);

        prework();

        ans = solve(B) - solve(A - 1);

        write(ans), puts("");
    }

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值