AKOJ-1695-找素数

题意:

给定区间L,R。

计算区间中素数个数。

2 <= L,R <= 2147483647, R-L <= 1000000。

思路:

素数区间筛

先筛(2-sqrt(r))。

再用(2-sqrt(r))中的素数筛(l-r)。

代码:

1.自己写的区间筛,将筛2-sqrt(r) 分开了。

#include <iostream>
#include <string>
#include <queue>
#include <memory.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAXN = 1000010;
int isPrimersmall[MAXN];
int isPrimer[MAXN];int main()
{LL l,r;scanf("%lld%lld",&l,&r);isPrimersmall[0] = 1;isPrimersmall[1] = 1;int x = sqrt(r);for (int i = 2;i<=x;i++){if (isPrimersmall[i] == 0){for (int j = i * i; j <= x; j += i)isPrimersmall[j] = 1;}}for (int i = 2;i<=x;i++){if (isPrimersmall[i] == 0){for (int j = (int)((l + (i-1))/ i) * i;j <= r; j += i){if (j != i)isPrimer[j-l] = 1;}}}int sum = 0;for (int i = 0;i<=r-l;i++){if (isPrimer[i] == 0)sum++;}printf("%d\n",sum);return 0;
}

  

2.将筛(2-sqrt(r))和(l-r)放在一起筛。

#include <iostream>
#include <string>
#include <queue>
#include <memory.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int MAXN = 1000010;
int isPrimersmall[MAXN];
int isPrimer[MAXN];int segement_sieve(LL a,LL b)
{for (LL i = 0;i*i <= b;i++)isPrimersmall[i] = 1;for (LL i = 0;i <= b-a;i++)isPrimer[i] = 1;for (LL i = 2;i*i <= b;i++){if (isPrimersmall[i]){for (LL j = i*2;j*j <= b;j+=i)isPrimersmall[j] = 0;for (LL j = max(2LL,(a+i-1)/i) * i;j <= b ;j+=i)isPrimer[j-a] = 0;}}int sum = 0;for (int i = 0;i <= b-a;i++)if (isPrimer[i])sum++;return sum;
}int main()
{LL a,b;scanf("%lld%lld",&a,&b);printf("%d\n",segement_sieve(a,b));return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10266165.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280657.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring 环境与profile(一)——超简用例

什么是profile,为什么需要profile? 在开发时&#xff0c;不同环境&#xff08;开发、联调、预发、正式等&#xff09;所需的配置不同导致&#xff0c;如果每改变一个环境就更改配置不但麻烦&#xff08;修改代码、重新构建&#xff09;而且容易出错。Spring提供了解决方案。 方…

Django04-1: ORM增删改查

ORM 增删改查 一、字段增加 #终端输入 1.model里添加字段&#xff0c; 2.执行迁移命令。 3.终端里输入默认值&#xff0c;继续执行迁移命令。 #允许为空 再nulltrue&#xff0c;终端不需要输入默认值 #设置默认值 defalult‘xxxx‘ 二、字段修改 1.直接修改代码&…

Comcast以纯文本泄露客户Wi-Fi登录信息,立即更改密码

A Comcast Xfinity website was leaking Wi-Fi names and passwords, meaning now is a good time to change your Wi-Fi passcode. Comcast Xfinity网站泄漏了Wi-Fi名称和密码&#xff0c;这意味着现在是更改Wi-Fi密码的好时机。 The site, intended to help new customers se…

SpringBoot详解(一)-快速入门

SpringBoot详解系列文章&#xff1a;SpringBoot详解&#xff08;一&#xff09;-快速入门SpringBoot详解&#xff08;二&#xff09;-Spring Boot的核心SpringBoot详解&#xff08;三&#xff09;-Spring Boot的web开发SpringBoot详解&#xff08;四&#xff09;-优雅地处理日志…

龙芯上跑WTM,为国产化做点贡献

点击上方蓝字关注我哦“信创”&#xff0c;是一项国家战略&#xff0c;即信息技术应用创新产业&#xff0c;它是数据安全、网络安全的基础&#xff0c;也是新基建的重要组成部分。信创从名称上来看本意指向创新&#xff0c;但是自从漂亮国亲手撕碎了“科技没有国界”的谎言之后…

Class与Style绑定

对于数据绑定&#xff0c;一个常见的需求是操作元素的class列表和它的内联样式。因为它们都是attribute&#xff0c;我们可以用v-bind处理它们&#xff1a;只需要计算出表达式最终的字符串。不过&#xff0c;字符串拼接麻烦又易错。因此&#xff0c;在v-bind用于class和style时…

PHP安装之configure的配置参数

1、生成环境安装配置如下 要求安装如下库&#xff1a; imagickgdmysqlmysqlimysqlndphalconPharsoapsocketsxwebxsvczipzlib 具体查看 vim php-config 就可以知道是如何配置的 --prefix/home/php --with-config-file-path/home/php/etc --with-mysql --with-pdo-oci --with-ope…

Django05: 请求生命周期流程图/路由层

请求生命周期流程图 扩展知识&#xff1a; 缓存数据库 路由层 路由匹配 url(r^test/, views.test), 1. 第一个参数是正则匹配。 只要第一个匹配了&#xff0c;就不会执行下面。 输入url会默认加斜杠&#xff0c;django会重定向 a. 一次匹配不行 b. url再加斜杠匹配 可以…

facebook 分享页面_Facebook个人资料,页面和组之间有什么区别?

facebook 分享页面Facebook is used by a lot of different people for a lot of different things, so it’s only natural that Facebook would have different sets of features for each of them. There are three main ways you can use Facebook: with a regular Profile…

zabbix运行脚本监控ggsci报错

/u01/app/oracle/oracle/ogg/ggsci: error while loading shared libraries: libdb-6.1.so: cannot open shared object file: No such file or directory增加脚本环境变量设置PATH$PATH:$HOME/binexport ORACLE_BASE/u01/app/oracleexport ORACLE_HOME$ORACLE_BASE/11/db_1exp…

一句话设计原则

面向对象的可复用设计&#xff08; Object Oriented Design / OOD&#xff09; 1. 开闭原则 (Open Closed Principle) 对扩展开放&#xff0c;对修改关闭 2. 里氏代换原则(LSP) 1.可以使用基类的地方&#xff0c;其子类必然也能使用 2.并且原功能不会受到任何影响 -- 经典案例,…

postman--安装及Interceptor插件

1. 官网安装&#xff08;看网速-我下载的时候一直下载失败&#xff09;打开官网&#xff0c;https://www.getpostman.com选择ios或者win 2. 非官网安装 https://pan.baidu.com/s/1mstsimqO3ZC5m9z8czxVnA 密码&#xff1a;q6yp 安装postman 3.需要安装分享的蓝灯安装包&#xf…

亚马逊标题自动抓取_如何为您的家人提供自动Amazon礼品卡津贴

亚马逊标题自动抓取When your kids move away to go to school, they’ll probably phone home every once in a while to ask for money. If they shop a lot on Amazon (and they probably do), you can expedite that process by setting up an automatically recurring dep…

Django04-2: ORM关系表\字段补充

一、表与表关系 一对多 多对多 一对一 图书表 出版社 作者表 作者详情表 出版社 和 图书表 关系 一对多 外键字段在多的一方 book 图书表 和 作者表 关系 多对多 需要创建第三张表 作者表 和 作者详情表 关系 一对一 #创建表关系 先将基表创建 再添加外键字段 一对多…

我 与 TDesignBlazor 的故事

前言作者打拼了 .NET 十多年&#xff0c;属于全栈应用类型的工程师&#xff0c;特别是对于前端的技术情有独钟&#xff0c;从纯js到jquery&#xff0c;从bootstrap到自己写css&#xff0c;从web到winform&#xff0c;还写过一段时间的knockout.js&#xff0c;以至于公司里的前端…

实验数据

1.整段deng音频200多秒 2.加xx(1000:1480)之后 转载于:https://www.cnblogs.com/20179302yzl/p/10270632.html

25个好用的Shell脚本常用命令分享

1.列出所有目录使用量&#xff0c;并按大小排序。复制代码 代码如下:ls|xargs du -h|sort -rn #不递归下级目录使用du -sh2.查看文件排除以#开关和空白行&#xff0c;适合查看配置文件。复制代码 代码如下:egrep -v "^#|^$" filenamesed /#.*$/d; /^ *$/d3.删除空格…

mysql中查询一个字段属于哪一个数据库中的哪一个表的方式

mysql中查询一个字段具体是属于哪一个数据库的那一张表&#xff1a;用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个库中的哪一张表 select table_schema,table_name from information_schema.columns where col…

macos剪切_如何使用macOS的内置“ Kill and Yank”作为替代剪切和粘贴

macos剪切Everyone knows about cutting and pasting by now. But did you know that your Mac sort of has a second clipboard known as kill and yank? 现在&#xff0c;每个人都知道剪切和粘贴。 但是您是否知道Mac上还有第二个剪贴板&#xff0c;称为“ kill and yank”&…

ExtJS 折线图趟过的坑

问题&#xff1a; 1、根据条件检索后绘制折线图&#xff0c;之前的坐标没有清除如图 解决方案&#xff1a; 在绘制之前&#xff0c;清空坐票&#xff1a; leftLine.surface.removeAll(); leftLine.redraw(false); 完整代码如下 storeBar.load({params: { SDate: bTime, EDate: …