CodeForces114E——Double Happiness(素数二次筛选)
Double Happiness
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] (l ≤ r) are suitable for pair programming. They decided that the day i (l ≤ i ≤ r) 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*10^8).
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.
Sample test(s)
input
3 5
output
1
input
6 66
output
7
题目大意:
给定区间[L,R],求区间内满足条件的数的个数:
条件1)z是素数
条件2)z=x^2+y^2 x和y为任意的正整数
解题思路:
其实就是求满足费马定理的数的个数。
费马定理:一个奇素数z可以表示成z=x^2+y^2的形式,当且仅当z可以表示成4*t+1的时候。(偶素数2=1^2+1^2)
LR的范围是1到3*10^8用普通的筛选法求素数表,时间空间都会超。使用两次筛选来求素数。
3*10^8的平方根小于17500,用17500以内的素数可以筛出范围内的所有素数。在通过判断是否满足z=t%4+1来累加。
又由于z的范围过大,但对于唯一确定的z来说,t也唯一确定,故可以用t作为数组下标即(z-1)/4。数组大小就会小4倍左右。
具体细节看代码备注。
Code:
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <bitset>
#define MAXN 17500
using namespace std;
bitset<MAXN>ISP; //类似于bool型,可以存01
bitset</+>result;
int prime[MAXN];
int is_prime()//先筛选1-17500的素数
{
int p=;
ISP[]=ISP[]=;
for (int i=; i<=MAXN; i++)
if (ISP[i]==)
{
prime[p++]=i;//用prime素组将素数存起来留到后面筛选用。。
for (int j=i+i; j<=MAXN; j+=i)
ISP[j]=;
}
return p-;
}
int cnt(int L,int R)
{
int p=is_prime();
for (int j=; j<=p; j++)
{
int x=L/prime[j];
if (x*prime[j]<L) x++;
if (x==) x++;
for (int k=x*prime[j]; k<=R; k+=prime[j]) //通过素数表再来筛选[L,R]中的素数
if (k%==) //标记符合条件2且不符合条件1的数
result[(k-)/]=;
}
int cnt=;
for (int i=L;i<=R;i+=)
if (!result[(i-)/]) cnt++;
return cnt;
}
int main()
{
is_prime();
int L,R;
int ok=;
cin>>L>>R;
if (L<=&&R>=) ok=; //2作为偶素数且符合条件 单独判断
while (L%!=||L==) //将区间边界缩小到符合 %4==1
L++;
while (R%!=)
R--;
cout<<cnt(L,R)+ok;
return ;
}
CodeForces114E——Double Happiness(素数二次筛选)的更多相关文章
- poj 2689 (素数二次筛选)
Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...
- cf Double Happiness(判断是否为素数且为4k+1型)
2790. Double Happiness time limit per test 3 seconds memory limit per test 128 megabytes input sta ...
- MySQL-分组查询(GROUP BY)及二次筛选(HAVING)
为了测试GROUP BY 语句,我们创建两张表,并往表中添加数据 -- 创建部门表 CREATE TABLE IF NOT EXISTS department( id TINYINT UNSIGNED ...
- MYSQL 二次筛选,统计,最大值,最小值,分组,靠拢
HAVING 筛选后再 筛选 SELECT CLASS,SUM(TOTAL_SCORES) FROM student_score GROUP BY CLASS HAVING SUM(TOTAL_SCO ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
- [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为任意正整数). 思路 ...
- jQuery-1.9.1源码分析系列(十二) 筛选操作
在前面分析的时候也分析了部分筛选操作(详见),我们接着分析,把主要的几个分析一下. jQuery.fn.find( selector ) find接受一个参数表达式selector:选择器(字符串). ...
- laravel 对查询结果的二次筛选
假设有表Scores 里面有 id,math,english等字段,现在要求按总分(数据库没有这个字段)来排序或者筛选,用having()方法就可以很方便解决这个问题. $scores = Score ...
随机推荐
- UVaLive6834 Shopping
题意:一条直线上有n个点,标号从1到n,起点为0,终点为N+1,给定m个限制关系(ci,di),访问ci点前必须先访问di点,每两个点之间是单位距离,求在限制条件下,从起点到终点访问完所有的点的最短距 ...
- Windows下Android模拟环境的搭建
- 抓取dump
1,在程序奔溃前部署.adplus.exe -crash -pn explorer.exe -o d: -crash:当进程挂掉的时候抓取dump,只能抓取到进程报错时的信息,如果进程不报错,就无法抓 ...
- ZigBee协议学习之网络层
ZigBee的体系结构中,底层采用IEEE 802.15.4的物理层和媒介层,再次基础上,ZigBee联盟建立了自己的网络层(NWL)和应用层框架. ZigBee网络层的主要功能包括设备的连接和断开. ...
- Fuck Flyme Theme
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/fuck-flyme-theme/ 说明 本插件仅用于魅蓝Note, MX3, MX4, MX4 PRO它机型 ...
- ios版弹珠游戏源码
这个是我们比较喜欢玩的一直小游戏的,ios版弹珠游戏源码,该游戏源码来着IOS教程网其他网友提供上传的,大家可以了解一下吧. nore_js_op> <ignore_js_op&g ...
- Eclipse经验总结
1.在Project Explorer中显示src.resource目录:通过Project->properties->java Build Path->Source设置 2.解决J ...
- html5学习笔记——2016/4
HTML5新增的结构元素: section article aside header hgroup footer nav figure HTML ...
- MongoDB之【增加用户认证、增加用户、删除用户、修改用户密码、读写权限、只读权限】
说明:增加用户是针对数据库进行操作 1.进入到数据库 use dbname 2.针对当前数据库添加用户 权限是针对当前数据 1.添加并验证用户 > use admin > db.addUs ...
- Java语法糖
1.创建数组:String[] s = new String[]{"1","2","3"};//这种方法可以在不指定数组元素数量的情况下生成 ...