Sdut Fermat's Chirstmas theorem (prime screening)

Source: Internet
Author: User
Fermat's Chirstmas Theorem Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ Description in a letter dated December 25,164 0; 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, neverwrote it down. it wasn't until 100 years later that no one other than Euler proved this theorem. to revoke strate, each of the following primes can be expressed as the sum of two squares: 5 = 2 2 + 1213 = 32 + 2217 = 42 + 1241 = 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.
Enter 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 between des a dummy test case with both L = u =? 1. output 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] (random Sive,) and Y is the total number of Primes (also within [L, u]) that can be expressed as a sum of squares. sample Input
10 2011 19100 1000-1 -1
Sample output
10 20 4 211 19 4 2100 1000 143 69
Sure enough, it's a tough question. If the meaning of the question is very clear, we can use the prime number to screen the brute force. There is a pitfall, for example, the range is 1-2. At this time, 1 is also qualified, because 1 = 4*0 + 1 and 1 = 0*0 + 1*1 (although 1 is not a prime number, why is there such data ?)
<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 (prime screening)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.