1. Prime number Statistics
(Pcount.pas/.c/.cpp)
"Problem description"
Xiao Tan's teacher is familiar with GE to decorate a problem for the students, asked to count the number of primes within a given interval. "Isn't it very simple?" "Little Tan couldn't help saying. Familiar with Golen said: "When you see the topic will know." "And then turned away.
Sure enough, little Tan was frightened by the great interval, and now it's time for you to save her.
Input
The input file name is pcount.in.
Enter a row of two positive integers a and B to indicate that the given interval is [B, a].
Output
The output file name is Pcount.out.
Outputs an integer that represents the number of primes in the interval.
"Input and Output sample"
Pcount.in |
Pcount.out |
1 17 |
7 |
"Data Range"
For 30% of the data, there are n
For 60% of the data, there are n
For 100% of the data, there is n
Problem Solving Report:
It was frightening to see such a big data. The previous day to learn the number theory, this time test on two questions, although I only did two questions. First talk about this problem, the problem of their own time to think, anyway, it is not all points, simply set the array smaller, get a sixty or seventy points. So began the violent screening method , the 1~m and 1~n of the prime numbers are screened out, and then subtract, just to note that N is the case of Prime, ans--; so he got 70.
Positive solution: Such a large array must not, so need subscript (array) translation , the n~m and greater than sqrt (m) translation to the subscript 0~m-n. Then sweep over the interval and add ans. But pay attention to special circumstances: n==1 time, ans--;
It is important to note that J is defined as a long long in the loop, because when M is large, I * i+ I exceeds the range of int, this time it becomes negative, then infinite loop, and the last set of data is timed out.
The code is attached:
1#include <iostream>2#include <cstdio>3#include <cmath>4 using namespacestd;5 Long LongN,m,ans;6 Const intmaxn=1000005;7 BOOLVIS1[MAXN],VIS2[MAXN];8 intMain ()9 {TenFreopen ("pcount.in","R", stdin); OneFreopen ("Pcount.out","W", stdout); ACin>>n>>m; - Long LongMID=SQRT (m) +1; - for(Long LongI=2; i<=mid;i++) the { - if(!Vis1[i]) - { - for(Long LongJ=i*i;j<=mid;j+=i)//Long Long +vis1[j]=1; - for(Long Longj=n/i*i;j<=m;j+=i) + if(j>i&&j>=n) vis2[j-n]=1; A } at } - for(intI=0; i+n<=m;i++) - if(!vis2[i]) ans++; - if(n==1) ans--; -cout<<ans; - return 0; in}
"Number theory + Skill" Magic Noip simulation test the second trial T1 prime number statistics