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. Java中String两种不同创建方式的区别及intern的用法

    一, Java有两种创建字符串的方式, String str1 = "abc"; String str2 = new String("abc"); 用双引号创建 ...

  2. C++编译错误:multiple types in one declaration

    这是在使用QT的时候看到的.这种情况往往是结构体或者是class最后少加了一个分好,加上即可,这个bug找了我好久,mark一下.

  3. New Concept English three(20)

    26w/m 36 In 1908 Lord Northcliffe offered a prize of £1000 to the first man who would fly across the ...

  4. Linux之LVM设备的管理

    LVM可以理解为可扩展的设备:在设备空间不足的时候,保证其在原始数据不变的情况下增大设备的存储大小.那么,要达到这种效果,我们得把可用设备先比变为物理卷,再把物理卷处理为物理卷组,最后成为LVM逻辑卷 ...

  5. Buildroot构建指南——工具链

    Linux系统的交叉编译工具链用来将源代码变成bin文件或者库文件的一个软件.一般大家默认工具链等于gcc或者arm-linux-gcc,但是实际上,gcc只是工具链的编译器部分,不是全部,制作一个工 ...

  6. LitJson 不支持 float 类型数据

    使用指引: 在 Unity 中使用 Best HTTP 插件的 LitJson 发送一些 Json格式数据给服务器,使用方式很简单: string jsonDataPost = JsonMapper. ...

  7. anjular2以及微信小程序的一点比较

    1条件渲染: 小程序:用 wx:if="{{condition}}" 来判断是否需要渲染该代码块. <view wx:if="{{condition}}" ...

  8. C# 读写opc ua服务器,浏览所有节点,读写节点,读历史数据,调用方法,订阅,批量订阅操作

    OPC UA简介 OPC是应用于工业通信的,在windows环境的下一种通讯技术,原有的通信技术难以满足日益复杂的环境,在可扩展性,安全性,跨平台性方面的不足日益明显,所以OPC基金会在几年前提出了面 ...

  9. Windows2008操作系统 IIS7 IIS7.5 进程池经常死最终解决方案

    作为一个服务器维护人员,经常遇到不可解决的问题,这问题一直存在,一直困扰我多时,经常凌晨1-2点起床,就为了重启一下进程池 错误应用程序名称: w3wp.exe,版本: 7.5.7600.16385, ...

  10. c++学习笔记(5)

    1.两个相邻的仅由空格,制表符或者换行符分开的字符串字面值,可连接成一个新的字符串 cout<<"a multi-line " "string literal ...