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. Jekyll学习:基本使用方法

    Jekyll是一个简单的博客.静态网站生成工具.利用它可以快速的搭建一个网站.并且完全免费的在 GitHub 上发布网站 — 自定义域名. 一.环境搭建 apt-get install jekyll ...

  2. 【Android UI】色板

    Hex Code Color #FFFFFF   #FFFFCC   #FFFF99   #FFFF66   #FFFF33   #FFFF00   #FFCCFF   #FFCCCC   #FFCC ...

  3. JavaScript和JQuery获取DIV的值

    1.设计源代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  4. linux路由表配置

    一.原理说明 1.路由表(table)从0到255进行编号,每个编号可以对应一个别名,编号和别名的对应关系在linux下放在/etc/iproute2/rt_tables这个文件里,一般0编号的tab ...

  5. 文件操作中的FLAG(O_RDONLY,O_WRONLY )的值

    #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> void main(void) { ...

  6. JFinal教程1——小白的第一个JFinal程序

    为了使小白能够完全的按步骤创建第一个JFinal应用并运行,笔者将以Java界最流行的Eclipse平台为例,搭建出所有基础教程中喜欢的Hello world应用. 1. JFinal简介 2. 小白 ...

  7. Windows VS下搭建cocos2d-x环境搭建

    VS2010以上版本(eg:VS2012/VS2013,这里本人用VS2013) 1.环境.安装包准备 2.python安装 3.cocos2d-x安装包解压安装 4.环境变量配置 5.执行setup ...

  8. BZOJ 1110: [POI2007]砝码Odw( 贪心 )

    ORZjcvb... #include<bits/stdc++.h> using namespace std; ; int N, M, item[maxn], V[maxn]; vecto ...

  9. Cocos2d-X游戏开发之Windows7+VS2010环境搭建(亲测)

    Cocos2d—X游戏引擎,提供Mac系统下的Xcode开发和Windows系统的VS开发,应该是比较常用的2种. 使用Mac以后,就会发现使用Xcode开发实在是福分啊.VS开发步骤繁琐,调试效率低 ...

  10. 黑龙江省第七届大学生程序设计竞赛-Heap

    描述 A heap is a full binary tree; for each node, its key is greater than its two sub-node’s key. Two ...