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. dfs 的全排列

    #include <iostream> #include <algorithm> #include <cstdio> #include <string> ...

  2. Linux档案与文件系统的压缩与打包

    总结: 压缩指令为透过一些运算方法去将原本的档案进行压缩,以减少档案所占用的磁盘容量.压缩前与压缩后的档案所占用的磁盘容量比值,就可以被称为是“压缩比” 压缩的好处是可以减少磁盘容量的浪费,在www网 ...

  3. PostgreSql基础命令及问题总结

    本章内容: 1.基本命令 基本命令 1.psql -U cdnetworks_beian -d cdnetworks_beian         #-U指定用户,-d指定数据库 2.\l        ...

  4. loj2091 「ZJOI2016」小星星

    ref 总的来说,就是 容斥转化为点对应到点集问题. 树形 dp 解决转化后的问题. #include <iostream> #include <cstring> #inclu ...

  5. Java并发之(1):volatile关键字(TIJ21-21.3.3 21.3.4)

    Java并发Java服务器端编程的一项必备技能. ** 1 简介    volatile是java中的一个保留关键字,它在英语中的含义是易变的,不稳定的.volatile像final.static等其 ...

  6. 引用其他头文件时出现这种错误,莫名其妙,error C2065: “ColorMatrix”: 未声明的标识符

    今天做项目时,直接拷贝了另一个工程里的头文件和源文件,然后运行时就出现这种问题,莫名其妙,在原程序里运行一点问题就没有,但是在新工程里就是error. >e:\c++\button_fly2\b ...

  7. springMvc的400问题

    主要是参数类型对不上导致的 本文主要记录一些作者在使用spring mvc过程中遇到的一些以及解决办法,以备日后查询或者供其他网友阅读,每个问题的解决办法肯定不止一种,如果你也遇到过类似问题,并且有独 ...

  8. Unity 查找

    GameObject.Find().Transform.Find查找游戏对象 1.前置条件 Unity中常用到查找对象,非隐藏的.隐藏的,各种方法性能有高有低,使用又有各种条件限制. 在此对查找的性能 ...

  9. 声卡(Sound Card)基本概念

    声卡 (Sound Card)是实现声音的模拟/数字信号相互转换.信号处理的一种硬件. 声卡的基本功能是把来自话筒.磁带.光盘的原始声音信号加以转换(模数转换或者数模转换),输出到耳机.扬声器.扩音机 ...

  10. 【bzoj4589】Hard Nim FWT

    题目描述 Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下: 1. Claris和NanoApe两个人轮流拿石子,Claris先拿. 2. 每次只能从一堆中取若干个,可将一堆全取走, ...