2013春季SD高校ACM周赛9(SDUT) -E

来源:互联网 发布:贝佳斯原理 知乎 编辑:程序博客网 时间:2024/04/30 06:09
                                                                                                            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 2011 19100 1000-1 -1

示例输出

10 20 4 211 19 4 2100 1000 143 69
 
 
#include<iostream>#include<cstring>#include<cstdio>#include<ctime>#include<algorithm>using namespace std;int n=1000003;      bool visit[10100000];int prime[10000000];void init_prim()//素数打表 这个比较高效 复杂度线性? {memset(visit, true, sizeof(visit));int num = 0;for (int i = 2; i <= n; ++i){if (visit[i] == true){num++;prime[num]=i;}for (int j = 1; ((j <= num) && (i * prime[j] <= n));  ++j){visit[i * prime[j]] = false;//if (i % prime[j] == 0) break; //点睛之笔}}}int main(){memset(prime, 0, sizeof(prime));    int count1;int count2;int a,b;init_prim();while(scanf("%d%d",&a,&b)){if(a==-1&&b==-1)break;count1=0;count2=0;for(int i=0;i<=100000;i++){if(prime[i]&&prime[i]>=a&&prime[i]<=b)//加上prime[i]就对~ 不加就错~ {count1++;if((prime[i]-1)%4==0)     count2++;     if(prime[i]==2)     count2++;     }}cout<<a<<" "<<b<<" "<<count1<<" "<<count2<<endl;}return 0;}        

原创粉丝点击