2013春季SD高校ACM周赛9(SDUT) -E
2013-03-17 16:33
295 查看
Fermat’s Chirstmas Theorem
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.
The last line of the input file includes a dummy test case with both L = U = −1.
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.
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, Fermatdidn’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, 000The last line of the input file includes a dummy test case with both L = U = −1.
输出
L U x ywhere 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
#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; }
相关文章推荐
- 2013春季SD高校ACM周赛9(SDUT) -A
- 2013春季SD高校ACM周赛9(SDUT) -b
- 【2013春季SD高校ACM周赛8(SDUT)】各水题题解
- 2013春季SD高校ACM周赛8(SDUT)
- 【2013寒假SD高校ACM周赛4(SDJZU & SDUT)】 超级玛丽
- 2013寒假SD高校ACM周赛3(SDUT)
- 2013.3.6 SDUT 2013春季ACM集训_12级周赛2 A题 ZOJ 2965
- 【读题坑爹】 2013寒假SD高校ACM周赛总结
- 【打表找规律】2013寒假SD高校ACM周赛5——I’ve Got Your Back(gammon)
- 【再看细节】SDUT春季高校周赛9 Problem E——A Tale from the Dark Side of The Moon
- 水题啊水题,可我偏偏没做出来!!!(SDUT 2013春季ACM集训_12级周赛3 A题 )
- 2013寒假SD高校ACM周赛3—Steps
- 2013 CSDN & 西南大学 高校俱乐部 春季巡讲
- [ACM]魔方涂色(SD高校ACM周赛3(SDUT))
- 【差一点就坑了】SDUT春季高校周赛8(SDUT2038) Problem F——数字游戏
- CocoaChina2013春季开发者大会:中移动游戏基地副总经理端木文琳-2013游戏大爆发 行业大洗牌
- 2013 山东科技大学CSDN高校俱乐部春季巡讲
- Cocos2d-x 2013春季新功能发布和发展规划
- 2013春季巡讲讲稿—从大学生到CTO—肖江—西南大学CSDN高校俱乐部