poj3511--Fermat's Christmas Theorem
2014-08-05 09:02
295 查看
Fermat's Christmas Theorem
Description
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:
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.
Input
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.
Output
For each test case, write the result using the following format:
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.
Sample Input
Sample Output
题意,如果一个素数可以用p = 4*c + 1 表示 那么他就能写成 p = a^2 + b^2 。 问在l到r中有多少素数,和有多少可以写成p = a^2 + b^2 的素数
特例,2 2不能写成p = 4*c + 1 但是 2 = 1^2 + 1^2
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[1100000] , check[1100000] , top ;
void f()
{
top = 0 ;
memset(check,0,sizeof(check));
int i , j ;
for(i = 2 ; i <= 1000000 ; i++)
{
if( !check[i] )
a[top++] = i ;
for(j = 0 ; j < top ; j++)
{
if( i*a[j] > 1000000 )
break;
check[i*a[j]] = 1 ;
if(i%a[j]==0)
break;
}
}
}
int main()
{
int i , j , l ,r , x , y ;
f();
while(scanf("%d %d", &l, &r)!=EOF)
{
if(l == -1 && r == -1)
break;
x = 0 ; y = 0 ;
for(i = 0 ; i < top ; i++)
{
if(a[i] > r)
break;
if( a[i] >= l )
{
x++ ;
if( a[i] == 2 )
y++ ;
else if( (a[i]-1)%4==0 )
y++ ;
}
}
printf("%d %d %d %d\n", l , r , x , y);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8353 | Accepted: 1761 |
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 |
Input
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.
Output
For each test case, write the result using the following format:
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.
Sample Input
10 20 11 19 100 1000 -1 -1
Sample Output
10 20 4 2 11 19 4 2 100 1000 143 69
题意,如果一个素数可以用p = 4*c + 1 表示 那么他就能写成 p = a^2 + b^2 。 问在l到r中有多少素数,和有多少可以写成p = a^2 + b^2 的素数
特例,2 2不能写成p = 4*c + 1 但是 2 = 1^2 + 1^2
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[1100000] , check[1100000] , top ;
void f()
{
top = 0 ;
memset(check,0,sizeof(check));
int i , j ;
for(i = 2 ; i <= 1000000 ; i++)
{
if( !check[i] )
a[top++] = i ;
for(j = 0 ; j < top ; j++)
{
if( i*a[j] > 1000000 )
break;
check[i*a[j]] = 1 ;
if(i%a[j]==0)
break;
}
}
}
int main()
{
int i , j , l ,r , x , y ;
f();
while(scanf("%d %d", &l, &r)!=EOF)
{
if(l == -1 && r == -1)
break;
x = 0 ; y = 0 ;
for(i = 0 ; i < top ; i++)
{
if(a[i] > r)
break;
if( a[i] >= l )
{
x++ ;
if( a[i] == 2 )
y++ ;
else if( (a[i]-1)%4==0 )
y++ ;
}
}
printf("%d %d %d %d\n", l , r , x , y);
}
return 0;
}
相关文章推荐
- POJ 3511 Fermat's Christmas Theorem 可能会
- poj 3511 Fermat's Christmas Theorem 筛素数
- Fermat’s Chirstmas Theorem
- SDUT Fermat’s Chirstmas Theorem(素数筛)
- 素数筛 E - Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem(素数筛)
- Fermat's little theorem(费马小定理)
- POj 3511 Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem
- Fermat’s Chirstmas Theorem (素数打表的)
- 素数判断----E -Fermat’s Chirstmas Theorem
- SDUT Fermat’s Chirstmas Theorem(素数筛)
- An Introduction to Fermat’s Last Theorem
- Fermat’s Chirstmas Theorem
- UVa 12665 - Joking with Fermat's Last Theorem(数学)
- Fermat’s Chirstmas Theorem
- Joking with Fermat's Last Theorem UVA - 12665 (数学,暴力)
- My christmas week holiday agenda...
- 中国剩余定理(Chinese Remainder Theorem)
- 106 - Fermat vs. Pythagoras