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. Gradle构建Java Web应用:Servlet依赖与Tomcat插件(转)

    Gradle的官方tutorial介绍了构建Java Web应用的基本方法.不过在使用Servlet做上传的时候会碰到问题.这里分享下如何通过Servlet上传文件,以及如何使用Gradle来构建相应 ...

  2. oschina应用工具

    应用工具 22思维导图软件 9博客客户端 15加密/解密/破解 120浏览器 78邮件工具 327文档/文本编辑 31杀毒软件 177终端/远程登录 195IM/聊天/语音工具 74下载工具 64文件 ...

  3. Debian 7 下载

                                                                    Debian 7 DOWNLOAD http://cdimage.deb ...

  4. 查询某库所有表的rows &查看当前sql的注册信息

    查询某库所有表的rows &查看当前sql的注册信息 1 2 3 4 5 6 7 select sobj.name,spar.rows FROM sys.objects sobj  INNER ...

  5. Smarty - 安装+入门小例子

    环境: smarty 1.在http://www.smarty.net/download下载最新smarty包,window选择zips,linux下选择tar.gz.以windows为例,下载后解压 ...

  6. struts+hibernate 请求数据库增删改查(小项目实例)

      StudentAction.java package com.action; import java.util.ArrayList; import java.util.List; import j ...

  7. CentOS6.5 服务器+apache5.3绑定多个域名+SELinux设置

    下面简单的介绍了如何通过设置Apache的http.conf文件,进行多个域名以及其相关的二级域名的绑定(假设我们要绑定的域名是minidx.com和ntt.cc,二级域名是blog.minidx.c ...

  8. lokijs

    http://lokijs.org/#/ 500,000+ 1.1M ops/s. A fast, in-memory document-oriented datastore for node.js, ...

  9. <转>一个最不可思议的MySQL死锁分析

    1 死锁问题背景 1 1.1 一个不可思议的死锁 1 1.1.1 初步分析 3 1.2 如何阅读死锁日志 3 2 死锁原因深入剖析 4 2.1 Delete操作的加锁逻辑 4 2.2 死锁预防策略 5 ...

  10. 11gOCP 1z0-052 :2013-09-11 MGR_ROLE role........................................................A66

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11584537 正确答案A 实验测试 1.创建用户:SKD gyj@OCM> crea ...