-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01644_PrimeNumber_TwoPointers.cpp
More file actions
62 lines (51 loc) · 952 Bytes
/
01644_PrimeNumber_TwoPointers.cpp
File metadata and controls
62 lines (51 loc) · 952 Bytes
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
58
59
60
61
62
#include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
int N;
int prime[2001][2001];
int arr[4000001];
vector<int> v;
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> N;
for (int i = 2; i <= N; i++)
arr[i] = i;
for (int i = 2; i * i <= N; i++)
{
if (arr[i] == 0) continue;
for (int j = i + i; j <= N; j += i)
arr[j] = 0;
}
for (int i = 2; i <= N; i++)
{
if (arr[i] != 0)
v.push_back(i);
}
int start = 0;
int end = 0;
int temp = 2;
int cnt = 0;
while (end < v.size())
{
if (temp == N)
{
cnt++;
temp += v[++end];
}
else if (temp < N)
{
temp += v[++end];
}
else
{
temp -= v[start++];
}
}
cout << cnt;
return 0;
}