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 ...
随机推荐
- 应注意的Flex&Bison潜规则
1.Flex的二义性模式 语法分析器匹配输入时匹配尽可能多的字符串 如果两个模式都可以匹配的话,匹配在程序中更早出的模式. 针对这一点的理解,在语法分析文件当中,token的识别,应从特殊到一般的过程 ...
- opencv 手写选择题阅卷 (一)表格设计与识别
(一)答题表格设计与识别 实际设计好的表格如下图 为了图像精确,表格和四角的标记都是由程序生成的,文字和数据是后期排版软件添加上去的. 图中四角的四个黑方块主要用来定位表格,然后就可以切割出每个单元格 ...
- C/C++ union
叙述原因: union data{ int a;double b;}; 对于union,实际中用的并不多,之前也知道怎样计算union的单元(在字对齐的基础上取最大成员所占的内存大小),比如 unio ...
- grails的插件
今天来歪理邪说一下grails的插件. 有个问题让本人困惑了一段时间,插件是属于grails的,还是属于某个工程的?为什么会有这个问题呢,这涉及到grails插件的安装方式. grails的插件像是一 ...
- C#,PHP对应加密函数
require_once "JunDes.php"; $jDes=new JunDes(); echo $jDes->encode('98765'); //echo $jDe ...
- c++,C# 转换
//C++中的DLL函数原型为 //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsi ...
- html实现层叠加
<div id="canvasesdiv" style="position:relative; width:400px; height:300px"> ...
- Python 基础篇:介绍
1. Python 发展 1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器.Python这个名字,来自Guido所挚爱的电视剧Monty Python's Flying Cir ...
- Ping的过程详解
先看拓朴图: 我在这里讲拼的两情况,一种是同一网段内,一种是跨网段的ping …. 首先,如果主机A,要去拼主机B,那么主机A,就要封装二层报文,他会先查自己的MAC地址表,如果没有B的MAC地址,就 ...
- Python urllib2多进程共享cookies
如果想多个进程共享同一个cookies,不用每个进程都重新登录,可以就cookies保存到一个文件,然后多个进程直接共享一个锁来实现 1.一个进程登录完成后,把cookies保存到一个文件里面 sel ...