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. JSON的使用

    最近在项目中大量的使用了JSON, 发现JSON和XML的功能相近,都是一种数据传输格式.只是与XML相比JSON显得更加轻量级,使用也更加容易. JSON依赖的第三方jar包: commons-be ...

  2. 创建ID3D11Device可能会遇到的问题,不能使用具体的IDXGIAdapter

    要使用具体硬件的显示适配器创建D3D11必须把driverTypes设为D3D_DRIVER_TYPE_UNKNOWN 如下 // 创建D3D11设备 HRESULT hr = D3D11Create ...

  3. mapper配置

    一:查询 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC &q ...

  4. setTimeout/setInterval执行时机

    setTimeout()和setInterval()经常被用来处理延时和定时任务.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式,而setInterval()则可以在每隔指定的 ...

  5. linux关闭服务的方法

    本文介绍下,在linux下关闭服务的方法,主要学习chkconfig的用法,有需要的朋友参考下. 先来看一个在linux关闭服务的例子,例如,要关闭sendmail服务,则可以按如下操作. 例1, 复 ...

  6. 【DELPHI】线程相关

    //准备让线程调用的测试函数 procedure Draw(aCanvas: TCanvas; X,Y: Integer; aCount: Integer = 100000); var i: Inte ...

  7. CorelDRAW 插件的安装和使用

    CorelDRAW 是一款在中国非常受欢迎的图形软件,开放的界面和编程技术,能够对它进行二次开发制作插件,插件大抵有三种gms.cpg.exe格式,下面介绍一下这三种插件的安装和使用方法. 一.gms ...

  8. 转 在无法通过yum下载非标准包时,怎么办

    在CentOS下,我们可以通过yum来下载或更新rpm包,但是标准的源(repository)里只提供一部分的rpm包,虽然大部分情况下,这些包是够用的.但是有时候还是需要下载其他的一些非标准的包,如 ...

  9. 生成XML文件,通过实体生成XML文件

    实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...

  10. if...else..的错误用法

    1.最近在写js代码完成一个前段DOM操作的函数时,自己错误的使用了if..else..控制体.为什么是错误的呢?看看我的 代码你就明白了: document.getElementsByClassNa ...