题意:
素数判断,预处理了一下,还加了一个优化,不然会超时。优化是zmc教的。
E - Fermat’s Chirstmas Theorem
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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.
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
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
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <queue> #include <map> using namespace std; int vis[1000505];//标记是否为素数,状态数组 int prime[1000000]; int n,m,cnt; int main() { int i,j; int x,y; int a; memset(vis,1,sizeof(vis)); vis[0]=vis[1]=0; for(i=3;i<1000005;i++) { if(i%2==0) vis[i] = 0; } for(i=3;i<1000;i++) { if(vis[i]) { for(int j=i*2; j<=1000000; j=i+j) vis[j]=0; } } int t=0; for(i=2;i<1000000;i++) { if(vis[i]) { prime[t] = i; t++; } } while(scanf("%d%d",&m,&n)!=EOF) { int i,j; int x=0; int y=0; if(m==-1&&n==-1) break; printf("%d %d ",m,n); for(i=0;i<t;i++) { if(prime[i]<=n&&prime[i]>=m) { x++; if((prime[i]-1)%4==0) y++; } } if(n>=2&&m<=2) y++; printf("%d %d\n",x,y); } return 0; }