## 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 of the 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 by solving a quadratic.

---

## 3) C++ Solution

```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

int64_t m;

void read() { cin >> m; }

void solve() {
    // Let g(K) = #{ports in [K+1, 2K] with exactly 3 set bits}. The problem
    // gives us M = g(K) for some unknown K, and asks if K is uniquely
    // determined. We will prove that the answer is YES iff M >= 2 and (M-1) is
    // a triangular number. The key idea is to consider how g(K) changes as K
    // increases by 1. When K -> K+1, the interval [K+1, 2K] becomes [K+2,
    // 2K+2]. We lose port K + 1 and gain ports 2K + 1 and 2K + 2. So g(K + 1) -
    // g(K) = [popcount(2K+1)==3] + [popcount(2K+2)==3] - [popcount(K+1)==3].
    //
    // Note that 2K+2 = 2*(K+1), so popcount(2K+2) = popcount(K+1).
    // These cancel, giving g(K+1) - g(K) = [popcount(2K+1) == 3].
    // Since 2K+1 is odd, popcount(2K+1) = popcount(K) + 1.
    // So g(K+1) - g(K) = [popcount(K) == 2].
    //
    // Since g(1) = 0 (interval [2,2] has no 3-bit numbers), telescoping
    // gives:
    //
    //   g(K) = #{j in [1, K-1] : popcount(j) == 2}.
    //
    // This is a staircase function: g increases by 1 at each K where
    // popcount(K-1) = 2, and is constant between such points. So M = g(K)
    // determines K uniquely iff the step has width 1, i.e. both K-1 and K
    // have popcount 2 (g jumps at K, and also jumped at K-1).
    //
    // Two consecutive integers both have popcount 2 only around powers
    // of 2: the pairs are (2^n+1, 2^n+2) for n >= 2. At such a point,
    // g(2^n+2) = #{j in [1, 2^n+1] with popcount 2} = C(n,2) + 1
    // (choose 2 of the n low bits, plus 2^n+1 itself). We don't care about the
    // actual K - only whether some K exists. So just check: is M = C(n,2) + 1
    // for some n >= 2? Equivalently, is (M-1) a triangular number?

    if(m < 2) {
        cout << "NO\n";
        return;
    }

    int64_t t = m - 1;
    int64_t k = (int64_t)((-1.0 + sqrt(1.0 + 8.0 * t)) / 2.0);
    while(k * (k + 1) / 2 < t) {
        k++;
    }
    while(k > 0 && k * (k + 1) / 2 > t) {
        k--;
    }
    cout << (k > 0 && k * (k + 1) / 2 == t ? "YES\n" : "NO\n");
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        solve();
    }

    return 0;
}
```

Note: here `k` plays the role of `n-1`, so `(M-1) = k(k+1)/2` is the same triangular-number condition as `(M-1) = n(n-1)/2`.

---

## 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.
