2790. Double Happiness

 
time limit per test

3 seconds

memory limit per test

128 megabytes

input

standard input

output

standard output

On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as:

t=a2+b2,where a,b are arbitrary positive integers.

Now, the boys decided to find out how many days of the interval [l,r] (lr) are suitable for pair programming. They decided that the day i(lir) is suitable for pair programming if and only if the number i is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days.

Input

The first line of the input contains integer numbers l,r (1≤l,r≤3·108).

Output

In the only line print the number of days on the segment [l,r], which are lucky for Peter and Bob at the same time.

Examples
Input
3 5
Output
1
Input
6 66
Output
7

题目分析:
两位同学的幸运数字一个是为素数,一个是可以表示为两整数平方和的数字。
这里只是想补充一个数论的知识:一个4k+1型的素数,一定可以分解为两个整数的平方和。
知道了这个知识后,一定程度上简化此题。最后一个要解决的问题就是,如何快速的判断一个较大数字是否为素数。
这个地方需要,了解“埃拉托斯特尼筛法”,以下简称埃氏筛
所谓“埃氏筛”就是,要得到n以内的所有素数,必须把不大于n的所有素数的整数倍剔除,剩下的数字就是素数。
另外r,l的取值范围最大取到3e8,如果建立一个容量是3e8的bool型数组,必定要炸内存,所以要设法精简下。
· 所有的素数都是奇数,所有可以找到一种对应关系,3/2=1,5/2=2,7/2=3,嗯,这样就可以把数组的长度折半。
b[n>>1]
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
const long maxn = 3e8 + ;
vector<bool> b(maxn >> , );//集体赋为0
int main()
{
long l;
long r;
long i;
long j;
for (i = ; i*i <= maxn; i += )//埃氏筛
if (!b[i >> ])//0 is 素数
for (j = i * i; j <= maxn; j += (i << )) {
b[j >> ] = ;//1 is 非素数
} while (cin >> l >> r) {
long ans = ;
if (l <= && r >= ) ans++;
while (l % != )l++;
while (r % != )r--;
for (i = l; i <= r; i += )
if (i >= l && !b[i >> ])
ans++;
cout << ans << endl;
}
return ;
}

cf Double Happiness(判断是否为素数且为4k+1型)的更多相关文章

  1. CodeForces114E——Double Happiness(素数二次筛选)

    Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers ...

  2. python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中

    刚刚学习python的菜鸟,这道题包括:文件的读写,python的参数调用,异常的使用,函数的使用 创建一个文本文件inti_prime.txt 执行命令:python Prime.py init_p ...

  3. (step7.2.2)hdu 2161(Primes——判断是否是素数)

    题目大意:输入一个n,判断您是否是素数.. 解题思路:简单数论 代码如下: /* * 2161_1.cpp * * Created on: 2013年8月31日 * Author: Administr ...

  4. Java经典案例之-判断质数(素数)

    /** * 描述:任意输入两个数n,m(n<m)判断n-m之间有多少个素数,并输出所有素数. * 分析:素数即质数,除1和本身之外,不能被其他自然数整除的数. * 判断素数的方法为:用一个数分别 ...

  5. 从n个数中随机选出k个数,并判断和是不是素数

    洛谷p1036 #include<iostream> #include<math.h> using namespace std; ],n,k;//依照题目所设 bool isp ...

  6. [Codeforces113C]Double Happiness(数论)

    题意 给定闭区间[l,r] [l,r] [l,r],找出区间内满足t=a2+b2 t=a^{2}+b^{2} t=a2+b2的所有素数t t t的个数( a,b a,b a,b为任意正整数). 思路 ...

  7. python_输入一个数,判断是否是素数

    while True: n=int(input('n=')) for i in range(2,n): if n%i==0: print("n is not 素数") break ...

  8. python应用-判断回文素数

    from math import sqrt number=int(input('请输入一个整数:')) def is_prime(num): for rea in range(2,int(sqrt(n ...

  9. Python小代码_10_判断是否为素数

    import math n = int(input('Input an integer:')) m = int(math.sqrt(n) + 1) for i in range(2, m): if n ...

随机推荐

  1. 使用IScroll5实现滚动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 009PHP基础知识——运算符(二) 逻辑运算符

    <?php /**运算符(二) 逻辑运算符: */ //1.逻辑与 and或&& 左右两边表达式均成立TRUE ,返回真值: /*$uname='admin'; $upwd='l ...

  3. 005-对象——对象的 final const

    <?php /** * */ /*class shouji { public $pinpai; final function chongdian() { //final 最终的 return $ ...

  4. Mysql加载配置默认路径

    查看命令 mysqld --verbose --help|grep "Default options" -n1 输出结果 11-12:Default options are rea ...

  5. ElasticSearch6.0 高级应用之 多字段聚合Aggregation(二)

    ElasticSearch6.0 多字段聚合网上完整的资料很少 ,所以作者经过查阅资料,编写了聚合高级使用例子 例子是根据电商搜索实际场景模拟出来的 希望给大家带来帮助! 下面我们开始吧! 1. 创建 ...

  6. LeetCode OJ:Peeking Iterator(peeking 迭代器)

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  7. Gradle2.0用户指南翻译——第二章. 概述

    翻译项目请关注Github上的地址:https://github.com/msdx/gradledoc本文翻译所在分支:https://github.com/msdx/gradledoc/tree/2 ...

  8. 使用Sinopia搭建私有npm仓库

    使用Sinopia搭建私有npm仓库 在用npm装包的时候,每次都要下载一大堆,慢且不说,npm还老被墙,所以就想到在公司内部搭建npm仓库镜像.大概看了几个,觉得Sinopia最简单也好用,所以就使 ...

  9. CSDN博客积分规则

    1.博客积分规则 博客积分是CSDN对用户努力的认可和奖励,也是衡量博客水平的重要标准.博客等级也将由博客积分唯一决定.积分规则具体如下: 每发布一篇原创或者翻译文章:可获得10分: 每发布一篇转载文 ...

  10. ubuntu创建Centos7镜像&&配置运行环境

    1. 下载centos7镜像 sudo docker pull centos:7 2. 启动centos7容器并挂载本地目录 sudo docker -it -v /home/software:/ho ...