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(素数二次筛选)的更多相关文章

  1. poj 2689 (素数二次筛选)

    Sample Input 2 17 14 17 Sample Output 2,3 are closest, 7,11 are most distant. There are no adjacent ...

  2. cf Double Happiness(判断是否为素数且为4k+1型)

    2790. Double Happiness   time limit per test 3 seconds memory limit per test 128 megabytes input sta ...

  3. MySQL-分组查询(GROUP BY)及二次筛选(HAVING)

    为了测试GROUP BY 语句,我们创建两张表,并往表中添加数据 -- 创建部门表 CREATE TABLE IF NOT EXISTS department( id TINYINT UNSIGNED ...

  4. MYSQL 二次筛选,统计,最大值,最小值,分组,靠拢

    HAVING 筛选后再 筛选 SELECT CLASS,SUM(TOTAL_SCORES) FROM student_score GROUP BY CLASS HAVING SUM(TOTAL_SCO ...

  5. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  6. POJ 2689 Prime Distance (素数+两次筛选)

    题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...

  7. [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为任意正整数). 思路 ...

  8. jQuery-1.9.1源码分析系列(十二) 筛选操作

    在前面分析的时候也分析了部分筛选操作(详见),我们接着分析,把主要的几个分析一下. jQuery.fn.find( selector ) find接受一个参数表达式selector:选择器(字符串). ...

  9. laravel 对查询结果的二次筛选

    假设有表Scores 里面有 id,math,english等字段,现在要求按总分(数据库没有这个字段)来排序或者筛选,用having()方法就可以很方便解决这个问题. $scores = Score ...

随机推荐

  1. 洛谷 P1890 gcd区间

    P1890 gcd区间 题目提供者 洛谷OnlineJudge 标签 数论(数学相关) 难度 普及/提高- 题目描述 给定一行n个正整数a[1]..a[n]. m次询问,每次询问给定一个区间[L,R] ...

  2. (UVALive 7261)Xiongnu's Land 二分

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  3. 完美高仿精仿京东商城手机客户端android版源码

    完美高仿精仿京东商城手机客户端android版源码,是从安卓教程网那边转载过来的,这款应用源码非常不错的,也是一个非常优秀的应用源码的,希望能够帮到学习的朋友. _js_op> <igno ...

  4. C#对象转JSON字符串和JSON字符串转对象

    namespace Net.String.ConsoleApplication { using System; using System.Data; using System.Collections; ...

  5. GDI+ 绘图闪烁解决方法

    闲着没事,准备做一个类似于TeeChart的自定义控件,结果第一步的绘图就把我给难倒了,虽然早就知道GDI绘图的闪烁问题很坑,但是却没有想到如此之坑,折腾了两天,才找到解决方法. 首先在窗体加载的时候 ...

  6. Jquery 控件

    1. Jeditable 2. Select2 3. superfish 4. Jquery file upload https://blueimp.github.io/jQuery-File-Upl ...

  7. slider jq小插件

    html代码 <div class="r_list r_1" style="display:block;"> <div class=" ...

  8. php,javscript调用百地图度API实现标记

    最近一个项目需要用到地图的定位和标记功能,本来考虑使用google map API .但是在国内这个速度确实很慢,有时候加载到一半就出现错了,不过可以通过google agent 来解决在国内的访问速 ...

  9. 修复ecshop商品重量BUG小数位增至五位

    如果ECSHOP商品重量录入为1.499千克,数据库存储值为1.499:如果录入1.499克,存储值为1.显然数据保存有误差,虽然在快递运输中,此误差极小可以忽略不计,但从严谨的角度看,这是不合理的. ...

  10. Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming

    Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming 在Spark2.x中,Spark Streaming获得了比较全面的升级,称为St ...