## 1) Abridged problem statement

A secret positive integer \(K\) is chosen. Only ports in the interval \([K+1, 2K]\) may pass the firewall, and among them, a port is allowed **iff** its number has **exactly 3 set bits** in binary. You test all ports and observe that exactly \(M\) ports were allowed.

For each given \(M\), determine whether \(K\) is **uniquely determined** by \(M\). Output `"YES"` if \(M\) corresponds to exactly one possible \(K\), otherwise `"NO"`.

---

## 2) Detailed editorial (solution idea and proof)

Define:

\[
g(K) = \#\{p \in [K+1, 2K] : \text{popcount}(p)=3\}
\]

We are given \(M=g(K)\) for some unknown \(K\), and must decide whether this \(K\) is uniquely determined.

### Step 1: How does \(g(K)\) change when \(K\) increases by 1?

Compare the two intervals:

- For \(K\): \([K+1, 2K]\)
- For \(K+1\): \([K+2, 2K+2]\)

Transition:
- We **lose** port \(K+1\)
- We **gain** ports \(2K+1\) and \(2K+2\)

So:
\[
g(K+1)-g(K) =
[\text{pc}(2K+1)=3] + [\text{pc}(2K+2)=3] - [\text{pc}(K+1)=3]
\]

But \(2K+2 = 2(K+1)\). Multiplying by 2 in binary is shifting left, which does **not** change popcount:
\[
\text{pc}(2K+2) = \text{pc}(K+1)
\]
So those two terms cancel:
\[
g(K+1)-g(K) = [\text{pc}(2K+1)=3]
\]

Now note \(2K+1\) in binary is \(K\) shifted left, plus 1 at the end, so:
\[
\text{pc}(2K+1) = \text{pc}(K) + 1
\]
Hence:
\[
g(K+1)-g(K) = [\text{pc}(K)=2]
\]

**Conclusion:** \(g(K)\) increases by 1 exactly at those \(K\) whose binary representation has exactly 2 set bits.

---

### Step 2: Telescoping to a counting formula

We can compute \(g(1)\): interval \([2,2]\) has no 3-bit numbers, so \(g(1)=0\).

Using the step relation:
\[
g(K) = \sum_{i=1}^{K-1} (g(i+1)-g(i))
     = \#\{j \in [1, K-1] : \text{pc}(j)=2\}
\]

So:

> \(M\) equals the number of integers \(<K\) that have exactly two set bits.

Numbers with exactly two set bits are of the form \(2^a + 2^b\) with \(a>b\ge 0\).

---

### Step 3: When is \(K\) uniquely determined by \(M\)?

Since \(g(K)\) is nondecreasing and increases only at special points, it forms a **staircase**: many consecutive \(K\) values can share the same \(M\).

A value \(M\) uniquely determines \(K\) **iff** the “stair step” at height \(M\) has **width 1**, i.e., there is exactly one \(K\) such that \(g(K)=M\).

A standard way to see this for a staircase function:
- \(g\) increases by 1 when \(\text{pc}(K-1)=2\) (because increment from \(K-1\to K\)).
- If both \(K-1\) and \(K\) have popcount 2, then \(g\) jumps at \(K\) and again at \(K+1\), making the plateau at value \(g(K)\) have length 1 (unique).

So we need **two consecutive integers** both with popcount 2.

---

### Step 4: Characterize consecutive integers with popcount 2

Let \(x\) have two set bits. For \(x+1\) to also have two set bits, adding 1 must:
- turn a trailing 0 into 1 without carrying through a 1-bit (otherwise popcount changes a lot)

This happens precisely when:
\[
x = 2^n + 1,\quad x+1 = 2^n + 2 \quad (n\ge 2)
\]
Examples:
- \(2^2+1=5\) (101), \(6\) (110)
- \(2^3+1=9\) (1001), \(10\) (1010)

These are exactly the consecutive popcount-2 pairs.

Thus the unique-determination heights occur at:
\[
K = 2^n + 2
\]

---

### Step 5: Compute the corresponding \(M\)

We need:
\[
M = g(2^n+2) = \#\{j \in [1, 2^n+1] : \text{pc}(j)=2\}
\]

Count popcount-2 numbers \(\le 2^n+1\):

- Among numbers \(<2^n\): choose any 2 set bits among the \(n\) low bit positions:
  \[
  \binom{n}{2}
  \]
- Plus the number \(2^n+1\) itself (bits at positions \(n\) and \(0\)), which also has popcount 2:
  \[
  +1
  \]

So:
\[
M = \binom{n}{2} + 1
\]
Therefore:
\[
M-1 = \binom{n}{2} = \frac{n(n-1)}{2}
\]

So:

> Answer is `"YES"` iff \(M\ge 2\) and \(M-1\) is a triangular number.

We just need to test whether there exists integer \(n\ge 2\) such that:
\[
n(n-1)/2 = M-1
\]
The C++ solution checks triangularity via solving a quadratic.

---

## 3) Provided C++ solution with detailed line-by-line comments

```cpp
#include <bits/stdc++.h>              // Includes almost all standard C++ headers
using namespace std;

// Overload output for pair<T1, T2> (not used in this solution, but part of template)
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

// Overload input for pair<T1, T2> (not used here)
template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

// Overload input for vector<T> (not used here)
template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {                 // read each element
        in >> x;
    }
    return in;
};

// Overload output for vector<T> (not used here)
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {                  // print each element
        out << x << ' ';
    }
    return out;
};

int64_t m;                            // M for current test case

void read() { cin >> m; }             // Read M

void solve() {
    // Mathematical result:
    // Answer is YES iff M >= 2 and (M-1) is a triangular number:
    // (M-1) = k(k+1)/2 for some integer k>=1
    // (their k corresponds to n-1; exact naming doesn't matter)

    if(m < 2) {                       // M=0 or 1 can never uniquely determine K
        cout << "NO\n";
        return;
    }

    int64_t t = m - 1;                // We need to check if t is triangular

    // Approximate solution of k(k+1)/2 = t:
    // k^2 + k - 2t = 0
    // k = (-1 + sqrt(1+8t)) / 2
    int64_t k = (int64_t)((-1.0 + sqrt(1.0 + 8.0 * t)) / 2.0);

    // Due to floating point rounding, adjust k upward until k(k+1)/2 >= t
    while(k * (k + 1) / 2 < t) {
        k++;
    }

    // And adjust downward if we overshot
    while(k > 0 && k * (k + 1) / 2 > t) {
        k--;
    }

    // If exact triangular match, YES, else NO
    cout << (k > 0 && k * (k + 1) / 2 == t ? "YES\n" : "NO\n");
}

int main() {
    ios_base::sync_with_stdio(false); // Fast I/O
    cin.tie(nullptr);                 // Untie cin/cout for speed

    int T = 1;
    cin >> T;                         // Number of test cases
    for(int test = 1; test <= T; test++) {
        read();                       // Read M
        solve();                      // Output YES/NO
    }

    return 0;
}
```

---

## 4) Python solution (same logic, with detailed comments)

```python
import math
import sys

def is_triangular(t: int) -> bool:
    """
    Return True iff t is a triangular number: t = k(k+1)/2 for some integer k>=0.
    We'll use integer arithmetic via discriminant checking.
    """
    if t < 0:
        return False

    # Solve k^2 + k - 2t = 0 => discriminant D = 1 + 8t
    D = 1 + 8 * t

    # D must be a perfect square
    s = int(math.isqrt(D))
    if s * s != D:
        return False

    # k = (-1 + s) / 2 must be an integer and >= 0
    if (s - 1) % 2 != 0:
        return False
    k = (s - 1) // 2
    return k >= 0 and k * (k + 1) // 2 == t


def solve_case(M: int) -> str:
    # From the derived condition:
    # unique K <=> M >= 2 and (M - 1) is triangular
    if M < 2:
        return "NO"
    return "YES" if is_triangular(M - 1) else "NO"


def main() -> None:
    data = sys.stdin.read().strip().split()
    if not data:
        return
    T = int(data[0])
    out = []
    for i in range(1, 1 + T):
        M = int(data[i])
        out.append(solve_case(M))
    sys.stdout.write("\n".join(out))

if __name__ == "__main__":
    main()
```

---

## 5) Compressed editorial

Let \(g(K)\) be the count of numbers in \([K+1,2K]\) with popcount \(=3\). When \(K\to K+1\), we lose \(K+1\) and gain \(2K+1,2K+2\). Since \(\text{pc}(2K+2)=\text{pc}(K+1)\), they cancel, so
\[
g(K+1)-g(K)=[\text{pc}(2K+1)=3].
\]
But \(\text{pc}(2K+1)=\text{pc}(K)+1\), hence
\[
g(K+1)-g(K)=[\text{pc}(K)=2].
\]
With \(g(1)=0\), telescoping yields
\[
g(K)=\#\{j\in[1,K-1]: \text{pc}(j)=2\}.
\]
Thus \(g\) is a staircase increasing at \(j\) with two set bits. \(M\) uniquely determines \(K\) iff the plateau at height \(M\) has length 1, which happens exactly when two consecutive integers have popcount 2. The only such pairs are \((2^n+1,2^n+2)\) for \(n\ge2\), giving the unique \(K=2^n+2\). For this \(K\),
\[
M=g(K)=\binom{n}{2}+1,
\]
so \(M-1\) must be triangular. Therefore output YES iff \(M\ge2\) and \(M-1\) is a triangular number.