原题 https://www.luogu.org/problemnew/show/P1865

本来get到了一个很好的判断素数的方法

O(玄学常数)https://www.luogu.org/blog/nopartyfoucaodong/solution-p3383 (我的luogu博客 嘻嘻)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int n,m;
bool su(int a){
if(a==) return ;
if(a==||a==) return ;
if(a%!=&&a%!=) return ;
int temp=sqrt(a);
for(int i=;i<=temp;i+=)
{
if(a%i==||a%(i+)==) return ;
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x;
scanf("%d",&x);
if(su(x)) printf("Yes"),cout<<endl; //是质数
else printf("No"),cout<<endl;
x=; }
return ;
}

然而这个方法在一些时候是有弊病的。比如本题区间质数查询时,有些数需要重复的判断多次。用这个方法就会T掉部分点。

一位@Enderturtle大佬给出了另一种方法。

所以说,对于不同的方法,还要注意看它们的优点,适宜什么情况啊(叹气

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int tot[];
bool book[];
void prime(int f)
{
tot[]=;
book[]=true;
for(int i=;i<=f;i++)
{
if(book[i]==false)
{
tot[i]=tot[i-]+;
for(int j=*i;j<=f;j=j+i)
{
book[j]=true;
}
}
else tot[i]=tot[i-];
}
}
int main()
{
scanf("%d%d",&n,&m);
prime(m);
for(int i=;i<=n;i++)
{
int l=,r=;
scanf("%d%d",&l,&r);
if(l<||l>m||r<||r>m)
{
printf("Crossing the line\n");
continue;
}
else
{
if(book[l]==false)
{
printf("%d\n",tot[r]-tot[l]+);
continue;
}
printf("%d\n",tot[r]-tot[l]);
} }
return ;
}

所以我们换个角度从判断素数,变成判断合数;

合数显然是可以分解质因数的,既然如此,也就说明,质数的倍数(倍数>1)都是合数,所以就有了线性筛(不懂线性筛的同学可以看洛谷[【模板】线性筛素数](https://www.luogu.org/problemnew/show/3383 "【模板】线性筛素数"))

本质上就是从2开始把它除本身的倍数全部删去,以此类推(这时你可能要问那到这个数的时候怎么判断它是不是质数,事实上,如果自然数N在N-1的时候没有把它标记掉就肯定是质数(具体证明可百度))

区间和可以用前缀和处理;

f[r]-f[l],如果l是质数还要加1

区间质数查询 luoguP1865的更多相关文章

  1. 洛谷 P1865 A % B Problem(求区间质数个数)

    题目背景 题目名称是吸引你点进来的 实际上该题还是很水的 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对 ...

  2. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  4. POJ 3468 A Simple Problem with Integers(线段树模板之区间增减更新 区间求和查询)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 140120 ...

  5. 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]

    题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...

  6. POJ 题目3667 Hotel(线段树,区间更新查询,求连续区间)

    Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13805   Accepted: 5996 Descriptio ...

  7. Accessoft-日期区间段查询示例,开始日期至截止日期区段查询

    Accessoft-日期区间段查询示例,开始日期至截止日期区段查询 实现功能效果如下: 示例查询开始日期为2017年3月15日到2017年3月16日的内容: sql查询语句如下: SELECT Inf ...

  8. codevs 1299 线段树 区间更新查询

    1299 切水果  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 简单的说,一共N个水果排成 ...

  9. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

随机推荐

  1. 九度OJ 1103:二次方程计算器 (解方程)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2804 解决:633 题目描述: 设计一个二次方程计算器 输入: 每个案例是关于x的一个二次方程表达式,为了简单,每个系数都是整数形式. 输 ...

  2. c/c++预处理命令#pragma

    1 #pragma pack(push, 1)和#pragma pack(pop) #pragma pack用于指定数据在内存中的对齐方式.如果不指定对齐方式的话,默认为自然对齐. 1.1 #prag ...

  3. Redis persistence demystified

    https://redis.io/topics/persistence http://oldblog.antirez.com/post/redis-persistence-demystified.ht ...

  4. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  5. 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1【转】

    本文转载自:http://www.cppblog.com/mmdengwo/archive/2011/04/14/144253.aspx #include <stdio.h>#includ ...

  6. javascript ajax和jquery ajax

    一 进行ajax步骤: 1 获取dom值 2发送ajax请求 3返回成功进行前端逻辑处理 二 原生javascript的ajax <!DOCTYPE html> <html> ...

  7. Java 使用POI操作EXCEL及测试框架搭建、测试开发的一些想法

    无论是UI自动化测试还是接口自动化测试都需要进行数据驱动,一般很常见的一种方式就是用excel来管理数据,那么就涉及到一些代码对EXCEL的操作,之前我们介绍过用CSV来处理EXCEL,但是它的功能还 ...

  8. python学习笔记:第二天(基本数据类型)

    Python3 基本数据类型 1.标准数据类型 Python3中有六个标准的数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictio ...

  9. CodeForces669E:Little Artem and Time Machine(CDQ分治)(或者用map+树状数组优美地解决)

    Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of cour ...

  10. 「UVA10298」 Power Strings(KMP

    题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 abcd aaaa ababab . 输出样例#1: 复制 1 4 3 题解 Luogu的题解 这里是对目前 ...