题目描述

区间质数个数

输入输出格式

输入格式:

一行两个整数 询问次数n,范围m

接下来n行,每行两个整数 l,r 表示区间

输出格式:

对于每次询问输出个数 t,如l或r∉[1,m]输出 Crossing the line

输入输出样例

输入样例#1: 复制

2 5
1 3
2 6
输出样例#1: 复制

2
Crossing the line

说明

【数据范围和约定】

对于20%的数据 1<=n<=10 1<=m<=10

对于100%的数据 1<=n<=1000 1<=m<=1000000 -10^9<=l<=r<=10^9 1<=t<=1000000

【分析】:不用前缀和就TLE阿QAQ

【代码】:

#include <bits/stdc++.h>

using namespace std;
int const MAX = ;
int const INF = 0x3fffffff;
int n, m;
int a[];
int isP(int n)
{
if(n == ) return ;
for(int i=; i<=sqrt(n); i++){
if(n % i == ){
return ;
}
}
return ;
} int main()
{
cin >> n >> m;
a[] = ;
int l, r, ans = ; for(int i=; i<=m; i++){
if( i%!= || i==){
a[i] = a[i-] + isP(i);
}
else{
a[i] = a[i-];
}
}
//a[i]=isP(i)+a[i-1]是a[i]=a[i]前所有素数的个数,如果i是素数 a[i]要加一,否则不加(判素数的函数回的是1或0)
while(n--){
ans = ;
cin >> l >> r;
if(l<||r>m){
printf("Crossing the line\n");
}
else{
printf("%d\n",a[r] - a[l-]);
} }
}

暴力筛

#include <bits/stdc++.h>

using namespace std;
int const MAX = ;
int const INF = 0x3fffffff;
int n, m, l, r;
int a[MAX];
int sum[MAX];
int main()
{
cin >> n >> m;
a[] = ; for(int i=; i<=sqrt(m); i++){
if(!a[i]){
for(int j=i+i; j<=m; j+=i){
a[j] = ;
}
}
} for(int i=; i<=m; i++){
sum[i] = sum[i-] + (a[i]^);//异或 :a[i] = 0 ——> +1 / a[i] = ——> +0
} for(int i=; i<=n; i++){
cin >> l >> r;
if(l< || r>m) puts("Crossing the line");
else{
printf("%d\n",sum[r]-sum[l-]);
}
}
}

埃筛

洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]的更多相关文章

  1. 洛谷P1865 A % B Problem

    1.洛谷P1865 A % B Problem 题目背景 题目名称是吸引你点进来的 实际上该题还是很水的 题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行 ...

  2. 【数论线性筛】洛谷P1865 A%B problem

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

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

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

  4. 洛谷 P1865 A % B Problem

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

  5. 洛谷 P4902 乘积 (约数筛,前缀和(积))

    洛谷P4902乘积 题意简述: 给 $ t $ 组 $ (a,b) $ 求: $ \prod_{i=A}^{B}\prod_{j=1}^{i}(\frac{i}{j})^{\lfloor \frac{ ...

  6. 【洛谷 p3383】模板-线性筛素数(数论)

    题目:给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内).(N<=10000000,M<=100000) 解法:1.欧拉筛O(n),数组近乎100KB:2.( ...

  7. 洛谷1001 A+B Problem

    洛谷1001 A+B Problem 本题地址:http://www.luogu.org/problem/show?pid=1001 题目描述 输入两个整数a,b,输出它们的和(|a|,|b|< ...

  8. 洛谷1303 A*B Problem 解题报告

    洛谷1303 A*B Problem 本题地址:http://www.luogu.org/problem/show?pid=1303 题目描述 求两数的积. 输入输出格式 输入格式: 两个数 输出格式 ...

  9. 洛谷1601 A+B Problem(高精) 解题报告

    洛谷1601 A+B Problem(高精) 本题地址:http://www.luogu.org/problem/show?pid=1601 题目背景 无 题目描述 高精度加法,x相当于a+b pro ...

随机推荐

  1. POJ 3414 BFS 输出过程

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17456   Accepted: 7407   Special J ...

  2. A1055 The World's Richest(25 分)

    A1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based ...

  3. V4L2学习(四)VIVI分析

    vivi 相对于后面要分析的 usb 摄像头驱动程序,它没有真正的硬件相关层的操作,也就是说抛开了复杂的 usb 层的相关知识,便于理解 V4L2 驱动框架,侧重于驱动和应用的交互. 前面我们提到,V ...

  4. TCP/IP网络编程之I/O流分离

    分离I/O流 “分离I/O流”是一种常用表达,有I/O工具可以区分二者.无论使用何种办法,都可以认为分离I/O流.我们之前通过两种方法分离I/O流,第一种是TCP/IP网络编程之进程间通信中的“TCP ...

  5. IE下Date.parse出现NaN有关问题解决

    IE不支持"2000-01-01"这种格式的,但是谷歌浏览器支持,改成"2000/01/01"就可以了. 下面的方法两种浏览器就就都支持了 Date.parse ...

  6. Asp.net自定义控件开发任我行(6)-嵌入资源下

    摘要 上一章,我们讲了嵌入.css文件,这一章,我们来讲一下嵌入.js文件,也顺带一个嵌入Image文件 内容 我们前面的几章,一运行,下拉框就显示出来了,但是DropDwonList的下拉框是被隐藏 ...

  7. C#开发微信公众平台教程

    http://www.cnblogs.com/xishuai/p/3625859.html http://www.cnblogs.com/wuhuacong/p/3614175.html http:/ ...

  8. 【Maximal Rectangle】cpp

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones ...

  9. tinyipa make

    参考:http://tinycorelinux.net/ Ironic Python Agent repo还提供了一组脚本,用于在imagebuild / tinyipa文件夹下构建一个基于Linux ...

  10. startActivityForResult用法

    使用场景:A界面(activity) 可跳转到一个(假设为 B)或多个子Activity,要求B处理完工作之后返回A 界面,或许同时返回一些数据交给A继续处理.如 由登陆界面A跳转到注册界面B,注册成 ...