AtCoder Beginner Contest 084 D - 2017-like Number【数论/素数/前缀和】
D - 2017-like Number
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
We say that a odd number N is similar to 2017 when both N and (N+1)⁄2 are prime.
You are given Q queries.
In the i-th query, given two odd numbers li and ri, find the number of odd numbers x similar to 2017 such that li≤x≤ri.
Constraints
- 1≤Q≤105
- 1≤li≤ri≤105
- li and ri are odd.
- All input values are integers.
Input
Input is given from Standard Input in the following format:
Q
l1 r1
:
lQ rQ
Output
Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.
Sample Input 1
1
3 7
Sample Output 1
2
- 3 is similar to 2017, since both 3 and (3+1)⁄2=2 are prime.
- 5 is similar to 2017, since both 5 and (5+1)⁄2=3 are prime.
- 7 is not similar to 2017, since (7+1)⁄2=4 is not prime, although 7 is prime.
Thus, the response to the first query should be 2.
Sample Input 2
4
13 13
7 11
7 11
2017 2017
Sample Output 2
1
0
0
1
Note that 2017 is also similar to 2017.
Sample Input 3
6
1 53
13 91
37 55
19 51
73 91
13 49
Sample Output 3
4
4
1
1
1
2
【题意】:当N和(N + 1)/ 2都是素数时,奇数N与2017相似。 你有Q个查询。 在第i个查询中,给定两个奇数Li和Ri,找到与2017相似的奇数x的数目,使得li≤x≤ri。
【分析】:筛素数用埃筛,查询用前缀和。
【代码】:
#include<bits/stdc++.h>
using namespace std;
bool f [];
int c [];
int N,L,R ;
int main ()
{
for (int i =; i<=; i++)
if (!f[i])
for (int j = i+i ;j<=; j+=i )
f[j]= true ; for (int i =; i<=; i+=)
if (!f[i] && !f[(i+)/])
c[i]++; for (int i =; i <=; i ++)
c[i]+=c[i-]; scanf ("%d",&N);
while (N--)
{
scanf ("%d%d" ,&L ,&R );
printf ("%d\n" , c[R] - c[L-]);
}
}
前缀和
AtCoder Beginner Contest 084 D - 2017-like Number【数论/素数/前缀和】的更多相关文章
- AtCoder Beginner Contest 084(AB)
A - New Year 题目链接:https://abc084.contest.atcoder.jp/tasks/abc084_a Time limit : 2sec / Memory limit ...
- AtCoder Beginner Contest 084 C - Special Trains
Special Trains Problem Statement A railroad running from west to east in Atcoder Kingdom is now comp ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
- AtCoder Beginner Contest 068 ABCD题
A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 161
比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...
随机推荐
- Hi3518EV300编译U-Boot和内核报错:loadlocale.c:130: _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed. Aborted (core dumped)
下载Hi3518EV300的SDK后编译内核和U-boot,发现爆出如下错误: scripts/kconfig/conf --silentoldconfig Kconfig Aborted (core ...
- HDOJ 2120 Ice_cream's world I
Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...
- (WPF&Silverlight)silverlight自定义控件
2个半小时弄懂了自定义控件是怎么回事儿. 在silverlight中创建一个UserControl,把上面sliderbar的外观和功能都封装在里面. 以自定义控件mapslider控件为例: 1.首 ...
- 15,scrapy中selenium的应用
引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生如果直接用scrapy对其url发请求,是获取不到那部分动态加载出来的数据值,但是通过观察会发现,通过浏览器 ...
- python基础——18(面向对象2+异常处理)
一.组合 自定义类的对象作为另一个类的属性. class Teacher: def __init__(self,name,age): self.name = name self.age = age t ...
- 【SHELL】Linux下安装Oracle Client
一.新建Oracle脚本存储目录并上传文件 [root@A04-Test-172]# mkdir -p /tmp/instance_oracle #新建存储目录 [root@A04-Test-172 ...
- OpenStack之各组件介绍
OpenStack简介 OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于:帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也为大云. ...
- 【SCOI 2010】传送带
为了方便,我们不妨设$\rm P \lt Q,R$ 我们发现,有$\rm E$点在$\rm AB$上,$\rm F$点在$\rm CD$上,最优解一定是$\rm AE\rightarrow EF\ri ...
- Python常见数据类型及操作
基础数据类型 什么是数据类型? 我们人类可以很容易的分清数字与字符的区别,但计算机并不能,计算机虽然很强大,但从某种角度上看又很傻,除非你明确的告诉它,1是数字,“汉”是文字,否则它是分不清1和‘汉’ ...
- 用户注册,登录API 接口
Controer: <?php /** * @name UserController * @author pangee * @desc 用户控制器 */ class UserController ...