problem D: The game of Acmer belongs to the number of guessing primesTime limit:1 Sec Memory limit:128 MB
submit:56 solved:15
[Submit] [Status] [Web Board]
Description
ACM Laboratory of the Great gods like to eat together happy especially Lou seniors like to please everyone happy, but Lou seniors please have a habit, we want to play a warm-up game together, guess Prime.
The rules of the game are as follows:
Normal version is this: for example, Lou Seniors agreed to a number, and agreed to a range such as 1~1000, and then everyone began to guess the number within the range, gradually narrowing the range, such as Tang seniors is Lou seniors next position of people, and then Tang seniors guessed 550, if and Lou seniors agreed with the number of Tang seniors accept punishment, Otherwise narrow the scope if the number of contracts is greater than 550, the range becomes 550~1000, otherwise it becomes 1~550. Guess the same, until someone accepts the punishment.
But the version of the Great Gods of the ACM Lab has made a little improvement, that the number of predetermined numbers and the number of guesses that follow must be primes. This is difficult for the cold God, the cold God does not know how many primes altogether. So you can only come to the wit of the study younger brother.
Enter two numbers, A, B (1<=a,b<=10^7) to determine how many primes will be in the range (including a A, b).
The data does not guarantee that a is less than B.
Input
A, b two number (1<=a,b<=10^7) Output
T indicates the number of primes in the range sample Input 1 2 sample Output 4 2 62
Simple prime number screening problem, long time no write ...
The idea of using a table to save results
The code is as follows:
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 10001000
using namespace std;
int ans[maxn+10];
BOOL VIS[MAXN+10];
void Init () {for
(int i=2, i*i<maxn; ++i) {
if (!vis[i]) {for
(int j=i*i; j<maxn; j+=i) {
Vis[j] = t Rue
;
}}} Ans[0] = ans[1] = 0;
for (int i=2; i<maxn; ++i) {
ans[i] = ans[i-1];
if (!vis[i])
++ans[i];
}
}
int main (void) {
init ();
int A, b, X, y;
while (scanf ("%d%d", &a, &b)! = EOF) {
printf ("%d\n", Ans[max (A, B)]-ans[min (A, B)-1]);
}
return 0;
}