-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotalprime_subarray.cpp
More file actions
57 lines (55 loc) · 1.1 KB
/
totalprime_subarray.cpp
File metadata and controls
57 lines (55 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int N = 10000002;
vector<bool> prime(N,true);
vector<int> nump(N); //total prime subarray
void SieveOfEratosthenes()
{
prime[0] = prime[1] = false;
for (int p = 2; p * p <= N; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= N; i += p)
prime[i] = false;
}
}
int c = 0;
for (int p = 0; p <= N; p++){
if (prime[p]){
c++;
}
nump[p] = c;
}
}
void solve(){
int n;cin>>n;
if(n==2){
cout<<"1";
}
else if(n==3){
cout<<"2";
}
else{
int l = n/2;
int k = nump[n] - nump[l];
int ans = k+1;
cout<<ans;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r", stdin);
freopen("output.txt","w", stdout);
#endif
fast;
int t;
SieveOfEratosthenes();
cin>>t;
while(t--){
solve();cout<<"\n";
}
return 0;
}