Title Description
If a prime number is a prime in the mass list, it is called prime in prime. For example: 3 5 are prime numbers for the 2nd and 3rd respectively, so they are prime numbers in prime numbers. Now give a number n, and find out how many prime numbers are in the smallest prime numbers of >=n (which can be considered by the prime sieve method).
input
Inputs a number N (n <= 10^6)
output >=n The smallest prime numbers in the primes.
Input Example
31 output Example
This problem at that time, feel 10^6=100000 and then always WA, can't find WA point, handed over two hair after looking at the topic information to change over, a change data range on .... surprised.
We need to know what the number of prime numbers are, and then we need to find out which of the numbers in prime numbers their permutations are prime numbers.
So first you need an array to save what number is prime, the second table in turn into prime numbers, the third table to deposit the prime number of the second table his position is not a prime, when the final output if vis2[i]>=n&&vis3[i]==0 can output this result ( Note the equals sign here!!)
But there's really no need for a lot of groups here, just put the code here,
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include &
lt;iostream> #include <algorithm> #include <queue> #include <map> #include <stack>
#include <set> using namespace std;
typedef long Long LL;
typedef unsigned long long ull;
const int max_n=100000;
int vis[1001010];
int vis2[1001010];
int vis3[1001010];
int main () {Ios::sync_with_stdio (false);
memset (vis,0,sizeof (VIS));
memset (vis2,0,sizeof (VIS2));
memset (vis3,0,sizeof (VIS3));
Vis[1]=1;
ll Qq=1;
for (ll-i=2;i<1001010;i++) {if (!vis[i]) for (LL J=i*2;j<=1001010;j=j+i)
Vis[j]=1;
if (vis[i]==0) vis2[qq++]=i;
} for (ll i=2;i<qq;i++) {for (int w=i*2;w<=qq;w=w+i) vis3[w]=1;
} int n; Vis3[1]=1;
while (cin>>n) {int flag=0; for (int i=1;i<=qq&&flag==0;i++) if (vis2[i]>=n&&vis3[i]==0) {flag
= 1;
cout<<vis2[i]<<endl;
}} return 0;
}
This code takes 46MS memory for 13.6m
So how to optimize it, first the VIS array is to mark a number is not a prime, so it can be replaced by the use of smaller memory bool type
Second VIS3 the existence of the array is really necessary, he just is the location of the query is not a prime number, can be used vis this table instead, can be deleted, and 1000000 of the multiple initialization may also lead to increased time
The optimization code is as follows:
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <i
ostream> #include <algorithm> #include <queue> #include <map> #include <stack>
#include <set> using namespace std;
typedef long Long LL;
typedef unsigned long long ull;
const int max_n=100000;
BOOL vis[1001010];
int vis2[1001010];
int n;
int main () {Ios::sync_with_stdio (false);
memset (vis,0,sizeof (VIS));
memset (vis2,0,sizeof (VIS2));
Vis[1]=1;
ll Qq=1;
for (ll-i=2;i<1001010;i++) {if (!vis[i]) for (LL J=i*2;j<=1001010;j=j+i)
Vis[j]=1;
if (vis[i]==0) vis2[qq++]=i;
} while (cin>>n) {int flag=0; for (int i=1;i<=qq&&flag==0;i++) if (vis2[i]>=n&&vis[i]==0) {flag= 1;
cout<<vis2[i]<<endl;
}} return 0; }
This code time is 31ms, takes up the memory to 6.7m, if still wants to optimize, may need to sacrifice the time to reduce the memory, for example does not hit the prime table, in order to judge this number is not prime, if is to deposit Vis2 array, And then you can test yourself a few primes in 1000000, and then optimize the size of the array, a small brother in the same way, the memory consumption dropped to 2m, but time is 472ms, so it is worth the candle, here is no longer paste code