Count primes

LeetCode 204. Count Primes 及判断一个数是否是质数的方法 LeetCode 204. Count Primes 题意:给出一个非负整数n,求小于n的质数的个数。 阅读详情

题目链接:点击打开链接

题目意思就是求1到n之间的素数个数 n<=1e11。

思路就是: 不会啊, 先存两份模板.

顺便存存高手代码。暂时没看懂,不过好短,感觉很厉害。

#include <bits/stdtr1c++.h>

#define MAXN 100
#define MAXM 10001
#define MAXP 40000
#define MAX 400000
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define isprime(x) (( (x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2))

using namespace std;

namespace pcf
{
long long dp[MAXN][MAXM];
unsigned int ar[(MAX >> 6) + 5] = {0};
int len = 0, primes[MAXP], counter[MAX];

void Sieve()
{
    setbit(ar, 0), setbit(ar, 1);
    for (int i = 3; (i * i) < MAX; i++, i++)
    {
        if (!chkbit(ar, i))
        {
            int k = i << 1;
            for (int j = (i * i); j < MAX; j += k) setbit(ar, j);
        }
    }

    for (int i = 1; i < MAX; i++)
    {
        counter[i] = counter[i - 1];
        if (isprime(i)) primes[len++] = i, counter[i]++;
    }
}

void init()
{
    Sieve();
    for (int n = 0; n < MAXN; n++)
    {
        for (int m = 0; m < MAXM; m++)
        {
            if (!n) dp[n][m] = m;
            else dp[n][m] = dp[n - 1][m] - dp[n - 1][m / primes[n - 1]];
        }
    }
}

long long phi(long long m, int n)
{
    if (n == 0) return m;
    if (primes[n - 1] >= m) return 1;
    if (m < MAXM && n < MAXN) return dp[n][m];
    return phi(m, n - 1) - phi(m / primes[n - 1], n - 1);
}

long long Lehmer(long long m)
{
    if (m < MAX) return counter[m];

    long long w, res = 0;
    int i, a, s, c, x, y;
    s = sqrt(0.9 + m), y = c = cbrt(0.9 + m);
    a = counter[y], res = phi(m, a) + a - 1;
    for (i = a; primes[i] <= s; i++) res = res - Lehmer(m / primes[i]) + Lehmer(primes[i]) - 1;
    return res;
}
}

long long solve(long long n)
{
    int i, j, k, l;
    long long x, y, res = 0;

    for (i = 0; i < pcf::len; i++)
    {
        x = pcf::primes[i], y = n / x;
        if ((x * x) > n) break;
        res += (pcf::Lehmer(y) - pcf::Lehmer(x));
    }

    for (i = 0; i < pcf::len; i++)
    {
        x = pcf::primes[i];
        if ((x * x * x) > n) break;
        res++;
    }

    return res;
}

int main()
{
    pcf::init();
    long long n, res;

    while (scanf("%lld", &n) != EOF)
    {
        //res = solve(n);
        printf("%lld\n",pcf::Lehmer(n));
        //printf("%lld\n", res);
    }
    return 0;
}


模板二代码   

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<time.h>

using namespace std;
typedef long long LL;
const int INF=2e9+1e8;
const int MOD=1e9+7;
const int MAXSIZE=1e6+5;
const double eps=0.0000000001;
void fre()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
}
#define memst(a,b) memset(a,b,sizeof(a))
#define fr(i,a,n) for(int i=a;i<n;i++)


LL f[320000],g[320000],n;

void init()
{
    LL i,j,m;
    for(m=1; m*m<=n; ++m) f[m]=n/m-1;
    for(i=1; i<=m; ++i)   g[i]=i-1;
    for(i=2; i<=m; ++i)
    {
        if(g[i]==g[i-1])continue;
        for(j=1; j<=min(m-1,n/i/i); ++j)
        {
            if(i*j<m)f[j]-=f[i*j]-g[i-1];
            else f[j]-=g[n/i/j]-g[i-1];
        }
        for(j=m; j>=i*i; --j)g[j]-=g[j/i]-g[i-1];
    }
}
int main()
{
    while(scanf("%I64d",&n)&&n)
    {
        init();
        cout<<f[1]<<endl;
    }
    return 0;

} 


--> 点击打开链接题解
学习Python,小白如何搭建Python环境? 学习Python,首先需要搭建好Python环境,主要分为【运行环境搭建】、【开发环境搭建】以及【Python环境配置】三个环节,接下来我将对Python环境搭建这三个部分进行介绍。 阅读详情

相关推荐

Hopfield神经网络(HNN)详解

Hopfield神经网络是一种比较特殊的网络,它不像一般的神经网络那样有输入层和输出层,并且通过训练来改变神经网络中的参数,最终实现预测、识别等功能。Hopfield网络只有一群神经元节点,所有节点之间相互连接,形式如下: 虽然有些文章会画出像深度神经网络那样有输入输出的图,并且输出后又返回输入,但本质上就是多个节点之间的全连接,也就是上面的图形。Hopfield神经网络主要有两个应用:一是起到类似储存器的作用,也就是我们把多个序列或图片输入这个网络,最终这个网络会以神经元之间连接权重的形式储存这

weixin_60737527的博客 4万+

204. Count Primes

原文题目: 204. Count Primes 读题: 题目很简单,就是求小于n的质数的个数,注意是小于n,不等于n,也就是说n=2的时候,返回0,n=3时才是返回1 一下列出两种方法,一种方法超时,另一种方法逐步优化到46ms //超时 class Solution { public: int countPrimes(int n) { int count = 0;

xqn2017的博客 500

nsight-system教程

参考nsight-compute使用教程-优快云博客。

youzjuer的博客 1894

[leetcode] 204. Count Primes

Description Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 分析 题目的意思是:给你一个...

Learning from the mistakes 222

ZLMediaKit源码分析(二)推流创建

ZLMediaKit推流结构体创建是在RtspSession::onRecv()之后。如何触发onRecv()请参考上一篇文章这里不在赘述。

cliffordl的专栏 5075

MIKE水动力笔记4_制作网格

建立水动力模型前的重要一步就是制作网格,本文所讲内容就是如何在MIKE中制作网格。

晏长街的博客 2万+

[LeetCode]204. Count Primes

[LeetCode]204. Count Primes题目描述思路类似动态规划,每次计算当前素数时,将不可能是素数的结果标记,之后遍历碰到这些结果直接跳过代码#include <iostream> #include <vector>using namespace std;class Solution { public: int countPrimes(int n) { if

Lcharon的博客 373

手把手教你学Simulink--移动机器人多轮协同场景实例:基于Simulink的双轮差速PMSM轮毂电机协同驱动仿真

本文介绍了基于Simulink的双轮差速PMSM轮毂电机协同驱动仿真系统。针对移动机器人存在的电机参数不一致、负载分配不均等问题,提出主从同步+力矩补偿的协同控制策略。通过搭建包含运动学模型、PMSM电机模型和矢量控制器的仿真系统,实现了速度同步误差<0.005m/s、轨迹跟踪误差<0.8cm、能耗降低12.5%的优化效果。文章详细阐述了建模步骤、核心算法实现和性能验证,为移动机器人动力系统设计提供了实用参考方案。

MHD0815的博客 133

LeetCode - 204. Count Primes

题目 LeetCode - 204. Count Primes 题目链接 https://leetcode.com/problems/count-primes/ 参考博客 https://www.cnblogs.com/grandyang/p/4462810.html 解题思路 自行设计的算法时间复杂度是n∗n\sqrt{n} * nn​∗n,但是运行超时,参考博客的算法复杂度分析知时间复杂度相同...

virgilshi的博客 215

Qt QCustomPlot 图形库详解及使用

QCustomPlot 是一个开源的 Qt C++ 库,专门用于创建数据图表。易于集成到 Qt 项目中。支持多种图表类型,如折线图、散点图、条形图等。提供丰富的交互功能,如缩放、平移、选择和拖动。高度可定制,支持图形的各种样式设置。// 添加第一条曲线i < 101;++i) {// 设置曲线颜色// 添加第二条曲线i < 101;++i) {// 设置曲线颜色// 设置散点样式// 添加图例。

MusicScore的博客 764

AI Agent Harness Engineering 开发效率工具:Prompt工程、调试与监控平台

随着AI Agent技术从原型探索走向企业级落地,开发者普遍面临Prompt迭代无版本管控、Agent执行黑盒无法定位错误、上线后无监控导致故障频发、多团队协作效率低下四大核心痛点,据2024年LLMOps行业报告显示,没有配套工具链的Agent开发团队平均迭代周期是有工具链团队的6倍,线上故障率高达23%,平均资源浪费超过60%。

小白菜的博客 426

Count Primes

埃拉托色尼筛法求素数、找出所有小于n的素数。

不积跬步无以至千里 375

【WRF-Urban】WUDAPT详解:An Urban Weather, Climate, and Environmental Modeling Infrastructure

WUDAPT详解:An Urban Weather, Climate, and Environmental Modeling Infrastructure

WW、forever的博客 497

Leetcode 204.Count Primes

Leetcode 204. Count Primes Count the number of prime numbers less than a non-negative number, n. 知识:任意一个合数都可以进行质因数分解,即任意一个合数都是一个小于自己的素数的倍数。 一、试除法 进行打表,一个数n如果是比它小的任意一个素数的倍数,则它是合数。 复杂度O(...

yangchz的博客 395

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

[Leetcode] Count Primes

Description: Count the number of prime numbers less than a non-negative number, n Hint: The number n could be in the order of 100,000 to 5,000,000. click to show more hints. Credits:Special thanks...

weixin_34257076的博客 220

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

hdu 5901 Count primes (meisell-Lehmer)

Count primes Time Limit: 12000/6000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2625Accepted Submission(s): 1337 Problem Description Easy question! Ca...

GetcharZp的博客 180

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

[LeetCode] 204. Count Primes

题:https://leetcode.com/problems/count-primes/description/ 题目 Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 Explanation: There are 4 prime numbers l...

u013383813的博客 108

204 Count Primes

Count the number of prime numbers less than a non-negative number, n.Example:Input: 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. 给出要筛数值的范围n,找出{\displaystyle {...

dongbeier的博客 209

LeetCode Count Primes

原题链接在这里:https://leetcode.com/problems/count-primes/ 这道题我先用了 HashSet,从2开始判断他是否为prime,若是就加入HashSet,对于接下来的数n,判断它是否会被HashSet现有的数字整除,若不能,n也是prime,加入HashSet;若能,n就不是prime。时间复杂度为O(n+k^2),k为指数。但是这种方法也超时了。

Dylan_Java_NYC的博客 615

[LeetCode] Count Primes

Description: Count the number of prime numbers less than a non-negative number, n解题思路采用Eratosthenes筛选法,依次分别去掉2的倍数,3的倍数,5的倍数,……,最后剩下的即为素数。实现代码class Solution { public: int countPrimes(int n) {

楚兴 1913
上一篇: CMD help
下一篇: 后缀数组-入门学习
Code--Dream
博客等级 码龄11年 53粉丝 100原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值