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. ognl--数据运转的催化剂

    原文链接:http://struts2.group.iteye.com/group/wiki/1353-ognl-catalyst-for-data-operation-in-struts2 首先让我 ...

  2. 使用DiskGenius对虚拟机磁盘进行压缩

    使用虚拟机的用户是否感觉到您的虚拟磁盘文件越来越大,都快把宝贵的磁盘空间(宿主机物理硬盘)占满了呢? 有人会想到,我直接启动虚拟机,然后把里面没用的数据删除了,不就行了吗?实际测试发现,这样删除后,存 ...

  3. Thinkphp里import的几个使用方法介绍

    以下附上import的几个使用方法介绍 1.使用方法一 import('@.Test.Translate'); @,表示项目根文件夹.假定根文件夹是:App/ 导入类库的路径是:App/Lib/Tes ...

  4. 动态规划——最长公共子序列(LCS)

    /** * @brief longest common subsequence(LCS) * @author An * @data 2013.8.26 **/ #include <iostrea ...

  5. Hibernate 一对一关联映射

    package com.entity; import javax.persistence.Entity; import javax.persistence.OneToOne; @Entity publ ...

  6. 我的Python成长之路---第一天---Python基础(5)---2015年12月26日(雾霾)

    六.流程控制 与C语言不通的事Python的流程控制的代码块不是用{}花括号表示的,而是用强制缩进来,而且缩进必须一致,官方推荐是使用4个空格,不建议使用使用tab(制表符)做缩进,一是不同的系统ta ...

  7. 用css实现列表菜单的效果

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

  8. [Android] Activity 重复使用

    Intent intent = new Intent(A.this, B.class); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | ...

  9. JVM调优总结(五)-分代垃圾回收详述1

    为什么要分代 分代的垃圾回收策略,是基于这样一个事实:不同的对象的生命周期是不一样的.因此,不同生命周期的对象可以采取不同的收集方式,以便提高回收效率. 在Java程序运行的过程中,会产生大量的对象, ...

  10. 基于visual Studio2013解决C语言竞赛题之0707月份输出

     题目 解决代码及点评 /* 编一程序,打入月份号,输出该月的英文月名. 例如,输入"3",则输出"March",要求用指针数组处理. */ #includ ...