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 lixri.

Constraints

  • 1≤Q≤105
  • 1≤liri≤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≤iQ) should contain the response to the i-th query.


Sample Input 1

Copy
1
3 7

Sample Output 1

Copy
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

Copy
4
13 13
7 11
7 11
2017 2017

Sample Output 2

Copy
1
0
0
1

Note that 2017 is also similar to 2017.


Sample Input 3

Copy
6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

Copy
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【数论/素数/前缀和】的更多相关文章

  1. AtCoder Beginner Contest 084(AB)

    A - New Year 题目链接:https://abc084.contest.atcoder.jp/tasks/abc084_a Time limit : 2sec / Memory limit  ...

  2. AtCoder Beginner Contest 084 C - Special Trains

    Special Trains Problem Statement A railroad running from west to east in Atcoder Kingdom is now comp ...

  3. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  4. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  5. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  6. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

  7. AtCoder Beginner Contest 068 ABCD题

    A - ABCxxx Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement This contes ...

  8. AtCoder Beginner Contest 154 题解

    人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...

  9. AtCoder Beginner Contest 161

    比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...

随机推荐

  1. python django 路由系统

    URL配置                        基本格式: from django.conf.urls import url urlpatterns = [ url(正则表达式, views ...

  2. TypeError: cannot perform reduce with flexible type

    想要解决这个错误,最好先明白numpy数据类型的dtype转换 生成一个浮点数组 a=np.random.random(4) 输出 a array([0.0945377,0.52199916,0.62 ...

  3. windows下虚拟环境中配置MySQL-python错误问题

    下载mysql 下载mysql-python 这两步基本没有问题怪就怪的 MySQL-python-1.2.3.win-amd64-py2.7 文件只能安装到python27  路径下 然后在虚拟环境 ...

  4. tinyipa make

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

  5. hnust Snowman

    问题 D: Snowman 时间限制: 1 Sec  内存限制: 128 MB提交: 203  解决: 94[提交][状态][讨论版] 题目描述 前言:这是某比赛的热身题,本题主要考察英文水平,只要看 ...

  6. 基于Jquery的商城商品图片的放大镜效果(非组件)

    在开发商城的时候,往往会用到图片的放大功能,这里把自己在近期项目中使用的放大镜特效做一下总结(非插件). 放大镜效果 常用的js组件jquery.imagezoom,jquery.jqzoom,jqu ...

  7. nyoj 题目7 街区最短路径问题

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...

  8. Linux系统基础优化总结

    请称呼我搬运工,哈哈 优化综合 https://www.cnblogs.com/yinshoucheng-golden/p/6149556.html (1)不用root管理,以普通用户的名义通过sud ...

  9. button的默认type居然是submit

    今天使用了html中的button标签,用js写了一点代码来完成onclick实践,当我点下它的时候,它不仅执行了我写的function,还把表单给提交了,一查它的button居然是sumbit. 然 ...

  10. loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】

    题目链接 loj300 题解 orz litble 膜完题解后,突然有一个简单的想法: 考虑到\(2\)是质数,考虑Lucas定理: \[{n \choose m} = \prod_{i = 1} { ...