loj #6235. 区间素数个数 min_12.5筛

\(\color{#0066ff}{ 题目描述 }\)

\(1\sim n\) 之间素数个数。

\(\color{#0066ff}{输入格式}\)

一行一个数 n 。

\(\color{#0066ff}{输出格式}\)

一行一个数,表示答案。

\(\color{#0066ff}{输入样例}\)

10

\(\color{#0066ff}{输出样例}\)

4

\(\color{#0066ff}{数据范围与提示}\)

对于 \(100\%\) 的数据,\(2 \leq n \leq 10^{11}\)

\(\color{#0066ff}{ 题解 }\)

这TM是min_12.5筛吧?????
预处理出g数组就可以直接出答案了
#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 2e6 + 10;
LL g[maxn], a[maxn];
LL n, sqt, m;
int getid(LL x) { return x <= sqt? x : m - n / x + 1; }
int main() {
    n = in();
    sqt = sqrt(n);
    for(LL i = 1; i <= n; i = a[m] + 1)
        a[++m] = n / (n / i), g[m] = a[m] - 1;
    for(int i = 2; i <= sqt; i++) {
        if(g[i] != g[i - 1]) {
            LL sqr = a[i] * a[i];
            for(int j = m; a[j] >= sqr; j--)
                g[j] -= g[getid(a[j] / i)] - g[i - 1];
        }
    }
    printf("%lld\n", g[m]);
    return 0;
}

转载于:https://www.cnblogs.com/olinr/p/10283224.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值