-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.cpp
55 lines (49 loc) · 1.16 KB
/
solution.cpp
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
#include <vector>
#include <bitset>
#include <algorithm>
#include <cmath>
using namespace std;
class Solution
{
public:
vector<int> prime;
void sieve(int M)
{
bitset<1001> isPrime;
isPrime.set();
isPrime[0] = isPrime[1] = 0;
for (int p = 2; p * p <= M; ++p)
{
if (isPrime[p])
{
for (int j = p * p; j <= M; j += p)
{
isPrime[j] = 0;
}
}
}
for (int p = 2; p <= M; ++p)
{
if (isPrime[p])
prime.push_back(p);
}
}
bool primeSubOperation(vector<int> &nums)
{
int n = nums.size(), M = *max_element(nums.begin(), nums.end());
sieve(M);
for (int i = n - 2; i >= 0; i--)
{
if (nums[i] >= nums[i + 1])
{
auto it = upper_bound(prime.begin(), prime.end(), nums[i] - nums[i + 1]);
if (it == prime.end())
return false;
nums[i] -= *it;
}
if (nums[i] <= 0)
return false;
}
return true;
}
};