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. Shared library can't open object

    将cpp的源文件和.so (shared object)链接成可执行程序之后,无法运行,提示如标题,实际就是找不到共享库. 最终找到了解决办法是: export LD_LIBRARY=./:$LD_L ...

  2. VS2013编译GLUI

    vs自带的OpenGL为1.1版本,太老了. 1,编译glut https://www.opengl.org/resources/libraries/glut/glut37.zip 查看生成路径,可以 ...

  3. const define 定义常量的区别

    1.用const定义常量在编译的时候,提供了类型安全检查,而define 只是简单地进行字符串的替换 2.const定义的常量,会分配相应的内存空间.而define没有分配空间,只是在程序中与处理的时 ...

  4. MFC中获取指针的方法

    1.获取应用程序指针 CMyApp* pApp=(CMyApp*)AfxGetApp(); 2.获取主框架指针 CWinApp 中的公有成员变量 m_pMainWnd 就是主框架的指针 CMainFr ...

  5. 获取当前<script>节点

    /* get current JavaScript dom object. */ var all_js = document.getElementsByTagName("script&quo ...

  6. JS如何获取iframe内html的body值

    default页面: <html> <head> <script type="text/javascript"> window.onload=f ...

  7. APACHE支持.htaccess以及 No input file specified解决方案

    在你的Apache安装文件夹conf里找到httpd.conf文件 搜索LoadModule rewrite_module modules/mod_rewrite.so 如果前面有注释符号#,请去掉. ...

  8. HttpClient使用笔记

    使用版本为4.5.1 常用API: 1.获取网页内容:InputStream in = response.getEntity().getContent() 2.获取状态码:response.getSt ...

  9. Django 下static的配置

    1.添加一个BASE_DIR在setting.py中,如果已存在可不用添加,需引入 import os BASE_DIR = os.path.dirname(os.path.dirname(os.pa ...

  10. PHP - PDO 之 mysql 事务功能

    <?php /* pdo 学习 */ $dsn = 'mysql:host=localhost;dbname=cswl';//构建连接dsn $db = new pdo($dsn,'root', ...