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. UVA:11297-Census(二维线段树)

    Census Time Limit: 8 sec Description This year, there have been many problems with population calcul ...

  2. PAT Basic 1085

    1085 PAT单位排行 每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数 N(≤10​5​​),即考生人数.随后 N 行, ...

  3. C#开发模式——dll多级引用的问题

    C#解决方案里有两种引用方式,项目引用和dll物理文件引用. 一.项目引用 严格引用,项目文件需包含在解决方案里,好处是便于调试,可直接进入代码.缺点是耦合度太高(必须全部编译通过才能run起来),项 ...

  4. 产生指定时间区间序列、按指定单位变化时间 python实现

    示例1:给定起始日期和结束日期,如何得到中间的时间序列 import datetime def dateRange(beginDate, endDate): dates = [] dt = datet ...

  5. 2018安恒杯11月月赛 MISC

    题目放评论了 Numeric password 这次隐写没有按照套路出牌,很强. 记录一下 看来自主学习的能力很有待提高. 打开Numeric password.txt 中华文化博大精深,近日在教小外 ...

  6. python - 接口自动化测试 - TestRecharge - 充值接口测试用例

    # -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_recharge.py @ide: PyChar ...

  7. Linux之ubuntu系统操作学习笔记

    1,swp分区:当内存不够时用swp分区顶替内存 2,语言环境检查  locale –a:可以明白系统支持什么语言 3,安装软件: apt-cache search(软件):搜索软件 apt-cach ...

  8. Python 操作 SQLite 数据库

    写在之前 SQLite 是一个小型的关系型数据库,它最大的特点在于不需要单独的服务.零配置.我们在之前讲过的两个数据库,不管是 MySQL 还是 MongoDB,都需要我们安装.安装之后,然后运行起来 ...

  9. 关于C++ STL标准库中map 的多元素应用

    map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ...

  10. 获取表的字段例如 col1,col2,col3

    create function [dbo].[f_getcolsByName](@tableName varchar(50)) returns varchar(1000)asbegin declare ...