2790. Double Happiness

 
time limit per test

3 seconds

memory limit per test

128 megabytes

input

standard input

output

standard output

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] (lr) are suitable for pair programming. They decided that the day i(lir) 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·108).

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.

Examples
Input
3 5
Output
1
Input
6 66
Output
7

题目分析:
两位同学的幸运数字一个是为素数,一个是可以表示为两整数平方和的数字。
这里只是想补充一个数论的知识:一个4k+1型的素数,一定可以分解为两个整数的平方和。
知道了这个知识后,一定程度上简化此题。最后一个要解决的问题就是,如何快速的判断一个较大数字是否为素数。
这个地方需要,了解“埃拉托斯特尼筛法”,以下简称埃氏筛
所谓“埃氏筛”就是,要得到n以内的所有素数,必须把不大于n的所有素数的整数倍剔除,剩下的数字就是素数。
另外r,l的取值范围最大取到3e8,如果建立一个容量是3e8的bool型数组,必定要炸内存,所以要设法精简下。
· 所有的素数都是奇数,所有可以找到一种对应关系,3/2=1,5/2=2,7/2=3,嗯,这样就可以把数组的长度折半。
b[n>>1]
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
const long maxn = 3e8 + ;
vector<bool> b(maxn >> , );//集体赋为0
int main()
{
long l;
long r;
long i;
long j;
for (i = ; i*i <= maxn; i += )//埃氏筛
if (!b[i >> ])//0 is 素数
for (j = i * i; j <= maxn; j += (i << )) {
b[j >> ] = ;//1 is 非素数
} while (cin >> l >> r) {
long ans = ;
if (l <= && r >= ) ans++;
while (l % != )l++;
while (r % != )r--;
for (i = l; i <= r; i += )
if (i >= l && !b[i >> ])
ans++;
cout << ans << endl;
}
return ;
}

cf Double Happiness(判断是否为素数且为4k+1型)的更多相关文章

  1. CodeForces114E——Double Happiness(素数二次筛选)

    Double Happiness On the math lesson a teacher asked each pupil to come up with his own lucky numbers ...

  2. python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中

    刚刚学习python的菜鸟,这道题包括:文件的读写,python的参数调用,异常的使用,函数的使用 创建一个文本文件inti_prime.txt 执行命令:python Prime.py init_p ...

  3. (step7.2.2)hdu 2161(Primes——判断是否是素数)

    题目大意:输入一个n,判断您是否是素数.. 解题思路:简单数论 代码如下: /* * 2161_1.cpp * * Created on: 2013年8月31日 * Author: Administr ...

  4. Java经典案例之-判断质数(素数)

    /** * 描述:任意输入两个数n,m(n<m)判断n-m之间有多少个素数,并输出所有素数. * 分析:素数即质数,除1和本身之外,不能被其他自然数整除的数. * 判断素数的方法为:用一个数分别 ...

  5. 从n个数中随机选出k个数,并判断和是不是素数

    洛谷p1036 #include<iostream> #include<math.h> using namespace std; ],n,k;//依照题目所设 bool isp ...

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

  7. python_输入一个数,判断是否是素数

    while True: n=int(input('n=')) for i in range(2,n): if n%i==0: print("n is not 素数") break ...

  8. python应用-判断回文素数

    from math import sqrt number=int(input('请输入一个整数:')) def is_prime(num): for rea in range(2,int(sqrt(n ...

  9. Python小代码_10_判断是否为素数

    import math n = int(input('Input an integer:')) m = int(math.sqrt(n) + 1) for i in range(2, m): if n ...

随机推荐

  1. hdu 6053 TrickGCD(筛法+容斥)

    TrickGCD Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. 利用CSS变量实现悬浮效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Java线程的五种状态详解

    状态转换图 1.new状态:通过new关键字创建了Thread或其子类的对象 2.Runnable状态:即就绪状态.可从三种状态到达,new状态的Thread对象调用start()方法,Running ...

  4. 决定整理一下canvas的基础学习

    好久没有用过canvas,都要忘完了.还是决定复习一下以前的笔记,以及整理一下笔记,以后好查阅

  5. Django中通过定时任务触发页面静态化的方式

    安装 pip install django-crontab 添加应用 INSTALLED_APPS = [ ... 'django_crontab', # 定时任务 ... ] 设置任务的定时时间 在 ...

  6. 初次使用VCS仿真软件

    由于刚开始接触VCS,对于VCS不是太了解,在网上找了很多的资料终于遇到了一个相对比较初级的入门资料,这个资料是以一个简单的4位加法器为例来介绍vcs的用法的,比较好入门,这个文章的地址如下: htt ...

  7. MVC4中视图获取控制器中返回的json格式数据

    再开发MVC项目时,有时只需要从控制器中返回一个处理的结果,这时返回Json格式的数据非常的方便,在Controller中,提供了几种返回类型和方法,如: Content() 返回文本类型的Conte ...

  8. 【PL/SQL编程】块结构

    [DECLARE] --声明部分,可选 BEGIN --执行部分,必须 [EXCEPTION] --异常处理部分,可选 END

  9. ASP.NET后台怎么输出方法中间调试信息?

    后台方法,不止是aspx.cs,而是页面调用的一些其它方法.想调试这些方法,我以前winform都是MessageBox.Show一些中间结果,现在我也想用这种方式.但想想,网页会触发 Message ...

  10. Android架构设计之MVP分析

    转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280