Fermat’s Chirstmas Theorem

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描写叙述

In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne that he just proved that an odd prime p is expressible as p = a2 + b2 if and only if p is expressible as p = 4c + 1. As usual, Fermat didn’t include the proof,
and as far as we know, never

wrote it down. It wasn’t until 100 years later that no one other than Euler proved this theorem.
To illustrate, each of the following primes can be expressed as the sum of two squares:
5 = 22 + 12
13 = 32 + 22
17 = 42 + 12
41 = 52 + 42
Whereas the primes 11, 19, 23, and 31 cannot be expressed as a sum of two squares. Write a program to count the number of primes that can be expressed as sum of squares within a given interval.

 
 

输入

Your program will be tested on one or more test cases. Each test case is specified on a separate input line that specifies two integers L, U where L ≤ U < 1, 000, 000

The last line of the input file includes a dummy test case with both L = U = −1.
 

输出

L U x y

where L and U are as specified in the input. x is the total number of primes within the interval [L, U ] (inclusive,) and y is the total number of primes (also within [L, U ]) that can be expressed as a sum of squares.
 

演示样例输入

10 20
11 19
100 1000
-1 -1

演示样例输出

10 20 4 2
11 19 4 2
100 1000 143 69

果然蛋疼的一道题。题意说的非常清楚,就用素数筛暴力就能够了,有一个坑就是比方范围是 1-2 这时1也是符合条件的,由于      1==4*0+1且1==0*0+1*1(尽管1不是素数。但为什么会有有这样的数据?)
<pre name="code" class="html">#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <vector>
#include <string>
#include <map>
#include <queue>
using namespace std;
const int maxn= 1000010;
int num=0;
int vis[maxn],prime[maxn];
void init_prime()
{
memset(vis,1,sizeof(vis));
vis[0]=0;vis[1]=0;
for(int i=0;i<=maxn;i++)
{
if(vis[i])
{
prime[++num]=i;
for(int j=1;j*i<=maxn;j++)
vis[j*i]=0;
}
}
}
int main()
{
int L,U,i;
init_prime();
while(scanf("%d%d",&L,&U)!=EOF)
{
int cnt1=0,cnt2=0;
if(L==-1&&U==-1) break;
for(i=0;i<=num;i++)
{
if(prime[i]&&prime[i]>=L&&prime[i]<=U)
{
if((prime[i]-1)%4==0)
cnt2++;
cnt1++;
}
}
if(L<=2&&U>=2)
cnt2++;
printf("%d %d %d %d\n",L,U,cnt1,cnt2);
}
return 0;
}


SDUT Fermat’s Chirstmas Theorem(素数筛)的更多相关文章

  1. Fermat’s Chirstmas Theorem (素数打表的)

                                                                             Fermat’s Chirstmas Theorem ...

  2. SDUT 3002-素数间隙(素数筛+暴力)

    素数间隙 Time Limit: 1000ms   Memory limit: 262144K  有疑问?点这里^_^ 题目描写叙述 Neko猫是一个非常喜欢玩数字游戏的会说话的肥猫,常常会想到非常多 ...

  3. Help Hanzo (素数筛+区间枚举)

    Help Hanzo 题意:求a~b间素数个数(1 ≤ a ≤ b < 231, b - a ≤ 100000).     (全题在文末) 题解: a~b枚举必定TLE,普通打表MLE,真是头疼 ...

  4. 素数筛 poj 2689

    素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...

  5. BestCoder Round #85 hdu5778 abs(素数筛+暴力)

    abs 题意: 问题描述 给定一个数x,求正整数y,使得满足以下条件: 1.y-x的绝对值最小 2.y的质因数分解式中每个质因数均恰好出现2次. 输入描述 第一行输入一个整数T 每组数据有一行,一个整 ...

  6. poj 3048 Max Factor(素数筛)

    这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...

  7. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  8. Light oj 1197 - Help Hanzo (素数筛技巧)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 给你a和b求a到b之间的素数个数. 先在小区间素数筛,大区间就用类似素数筛的想法 ...

  9. 素数筛&&欧拉筛

    折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...

随机推荐

  1. springMVC中得到request对象,session对象

    RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((S ...

  2. php抓取ajax页面返回图片。

    要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...

  3. svn笔记3

    如果你是从头到尾按章节阅读本书,你一定已经具备了使用Subversion客户端执行大多数不同的版本控制操作足够的知识,你理解了怎样从Subversion版本库取出一个工作拷贝,你已经熟悉了通过svn ...

  4. mong 备份和恢复

    [root@hy-mrz01 bin]# ./mongofiles list -h114.55.5.57 -db pics 20160602152850deeabcb1bd2644afa0c3a9a8 ...

  5. 设计模式(八)装饰器模式Decorator(结构型)

    设计模式(八)装饰器模式Decorator(结构型) 1. 概述 若你从事过面向对象开发,实现给一个类或对象增加行为,使用继承机制,这是所有面向对象语言的一个基本特性.如果已经存在的一个类缺少某些方法 ...

  6. 【行业干货】2013中国零售商排名 - 课程公告板 - 京东内部论坛 - Powered by Discuz!

    [行业干货]2013中国零售商排名 - 课程公告板 - 京东内部论坛 - Powered by Discuz! [行业干货]2013中国零售商排名 [复制链接]     bjpanzhoulan   ...

  7. 配置免安装版JAVA1.7的环境变量

    我用的是免安装版JAVA1.7,假设想获取JDK能够联系问我要. 1.開始配置环境变量,右击[我的电脑]---[属性]-----[高级]---[环境变量],如图: 2.选择[新建系统变量]--弹出&q ...

  8. activity入门

    1.前单文件 <activity android:name="com.example.twoactivity.OtherScreenActivity" android:lab ...

  9. java.util.concurrent-------TimeUnit

    java.util.concurrent并发库是JDK1.5新增的,其作者是Doug Lea ,此人是个BOSS级别的天才人物了.有了他提供的类库,使得我们对多线程并发.锁有了很大的帮助,减少了并发难 ...

  10. first-,second- and third-class value

    In computer science, a programming language is said to have first-class functions if it treats funct ...