Test instructions
Enter a number n (n <= 200000) and output the bottom n primes.
Ideas:
There are about 210000 primes in 1-3000000. Direct these primes to the table, then the offline query on OK.
Code:
1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <algorithm>7#include <queue>8#include <stack>9#include <vector>Ten using namespacestd; OnetypedefLong LongLL; A - BOOLvisit[3010000+7]; - intprime[3000000+7]; the intans[220000+7]; - voidInit_prim () { -memset (Visit,true,sizeof(visit)); - intnum =0; + for(inti =2; I <=3000000; ++i) { - if(Visit[i] = =true){ +num++; APrime[num] =i; at } - for(intj =1; ((J <= num) && (i * prime[j] <=3000000)); ++j) { -Visit[i * Prime[j]] =false; - if(i% prime[j] = =0) Break; - } - } in } - to intMain () + { - //freopen ("Input.txt", "R", stdin); thememset (Prime,0,sizeof(prime)); * Init_prim (); $ intCNT =0;Panax Notoginseng for(inti =0; I <=3000000; i++){ - if(Prime[i]) ans[cnt++] =Prime[i]; the } + intT; Ascanf"%d", &t); the while(t--){ + intN; -scanf"%d", &n); $printf"%d\n", Ans[n-1]); $ } - return 0; -}
To find the nth number of primes