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. 内存保护机制及绕过方法——利用Ret2Libc绕过DEP之ZwSetInformationProcess函数

    1.    DEP内存保护机制 1.1   DEP工作原理 分析缓冲区溢出攻击,其根源在于现代计算机对数据和代码没有明确区分这一先天缺陷,就目前来看重新去设计计算机体系结构基本上是不可能的,我们只能靠 ...

  2. 数据存储-- Core Data的使用(一)

    一.概念 1.Core Data 是数据持久化存储的最佳方式 2.数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型 在Mac OS X 10.5Leopard及以 ...

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

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

  4. Xcode 在读写上提速100倍

  5. key相同合并Map

    public class Demo11 { public static void main(String[] args) { ConcurrentHashMap<Integer, Map< ...

  6. linux命令-xz

    tar.xz文件如何压缩解压xz是绝大数linux默认就带的一个压缩工具,压缩率很高. xz压缩文件方法 默认压缩等级是6.要设置压缩率加入参数 -0 到 -9调节压缩率. xz -z [文件名] 不 ...

  7. 【剑指offer】不用加减乘除做加法,C++实现

    原创博文,转载请注明出处! # 题目 # 思路 第一步:不考虑进位对每一位相加(异或操作) 第二步:考虑进位(位与运算+左移) 第三步:第一步和第二步相加(重复执行前两步) # 代码 #include ...

  8. Hello Pythoner!

    首先,欢迎你来到pyer的博客,希望你能有所收获! 然后,pyer之前学C#(原博客地址:初行-博客园),后来转的Python,目前从事服务端研发工作. 最后,相逢便是缘,如果看过pyer的博客后有什 ...

  9. Linux下定时任务Crontab的使用

    使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命令.例如系统管理员安排一个备份任务使其每天都运行   入门 # crontab –e 这样可以已编辑模式打开个人的cro ...

  10. CF的Architecture,把它搞透!

    Architecture Cloud Controller - Maintains a database with tables for orgs, spaces, apps, services, s ...