HUST 1214 Cubic-free numbers II(区间n=x^3*k的n的个数、枚举x容斥)

题目链接;
HUST 1214 Cubic-free numbers II
题意:
求区间 n[L,R] ,且 n 不可以表示成n=x3kx1的形式的 n 的个数。
数据范围:大概107
分析:
其实这题和HDU 2204 Eddy’s 爱好
差不多。
HDU那道题我们是枚举指数,然后容斥,因为考虑到数据范围。
同样的道理这里我们枚举底数,但是这样子依然会重复,例如 2364 831 就被计算了两次,同时我们可以发现多计算的一定是底数的倍数,而且我们需要倒着处理
这样子可以得到区间 [1,r] 中的解,那么区间 [L,R] 中的解减一下就好了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int MAX_N = 20000100;

ll num[MAX_N];

ll solve(ll n)
{
    if(n <= 0) return 0;
    ll top = (ll)(pow(n * 1.0, 1.0 / 3.0) + eps);
    ll ans = 0;
    for(ll i = top; i >= 2; --i) {
        num[i] = n / (i * i * i);
        for(ll j = 2; j * i <= top; ++j) {
            num[i] -= num[j * i];
        }
        ans += num[i];
    }
    return n - ans;
}

int main()
{
    int T;
    ll L, R;
    scanf("%d", &T);
    while(T--) {
        scanf("%lld%lld", &L, &R);
        printf("%lld\n", solve(R - 1) - solve(L - 1));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值