Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
, so this is absolutely not the time to invent some galaxy-brain optimization. First, figure out all prime numbers up to .
"Neighboring primes" just means consecutive primes in the sorted prime list: , , , , and so on.
For every consecutive pair , compute If is prime and , then this is one of the special primes Nick wants.
So the problem becomes: count how many values of the form are themselves prime and do not exceed . Then compare that count with .
Use a sieve of Eratosthenes to build a boolean array and a list of all primes up to . Then do one linear pass over consecutive primes:
Print YES if , otherwise NO. That's the whole damn problem.
A prime number counts if it can be written as where and are neighboring primes, i.e. consecutive primes.
So once we know all primes up to , we can just inspect every consecutive pair. No tricks, no DP, no black magic—just a clean brute-force check.
Let the primes up to be For each adjacent pair , compute If both conditions hold:
then is one valid prime for Nick's condition.
We count how many such exist. If the count is at least , answer YES; otherwise NO.
A prime number is valid iff it has exactly the form described in the statement:
Our loop checks every possible consecutive prime pair, so:
Therefore the number we compute is exactly the number of valid primes in .
Use the sieve of Eratosthenes up to (or , same difference here).
That gives us:
isPrime[x].Sieve:
Scanning consecutive prime pairs: where is the number of primes up to .
With , this is hilariously small.
For :
Primes are
Check consecutive pairs:
So we get valid primes: and .
If , answer is YES.
Pretty straightforward. The problem sounds fancy because of the name, but underneath it's just “generate primes and check adjacent sums.” Classic Codeforces smoke-and-mirrors.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() { setIO();
int n, k;
cin >> n >> k;
vector<bool> isPrime(n + 1, true);
if (n >= 0) isPrime[0] = false;
if (n >= 1) isPrime[1] = false;
for (int i = 2; i * i <= n; ++i) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
vector<int> primes;
for (int i = 2; i <= n; ++i) {
if (isPrime[i]) primes.push_back(i);
}
int cnt = 0;
for (int i = 0; i + 1 < (int)primes.size(); ++i) {
int s = primes[i] + primes[i + 1] + 1;
if (s <= n && isPrime[s]) {
++cnt;
}
}
cout << (cnt >= k ? "YES" : "NO");
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() { setIO();
int n, k;
cin >> n >> k;
vector<bool> isPrime(n + 1, true);
if (n >= 0) isPrime[0] = false;
if (n >= 1) isPrime[1] = false;
for (int i = 2; i * i <= n; ++i) {
if (isPrime[i]) {
for (int j = i * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
vector<int> primes;
for (int i = 2; i <= n; ++i) {
if (isPrime[i]) primes.push_back(i);
}
int cnt = 0;
for (int i = 0; i + 1 < (int)primes.size(); ++i) {
int s = primes[i] + primes[i + 1] + 1;
if (s <= n && isPrime[s]) {
++cnt;
}
}
cout << (cnt >= k ? "YES" : "NO");
}