<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

113. Nearly prime numbers

time limit per test: 0.25 sec.
memory limit per test: 4096 KB


Nearly prime number is an integer positive number for which it is possible to find such primes P1 and P2 that given number is equal to P1*P2. There is given a sequence on N integer positive numbers, you are to write a program that prints “Yes” if given number is nearly prime and “No” otherwise.


Input

Input file consists of N+1 numbers. First is positive integer N (1£N£10). Next N numbers followed by N. Each number is not greater than 109. All numbers separated by whitespace(s).


Output

Write a line in output file for each number of given sequence. Write “Yes” in it if given number is nearly prime and “No” in other case.


Sample Input

1
6
Sample Output

Yes
Author	: Michael R. Mirzayanov
Resource	: PhTL #1 Training Contests
Date	: Fall 2001

<|response|>
1. Abridged Problem Statement  
Given N (1 ≤ N ≤ 10) positive integers (each ≤ 10^9), determine for each one whether it is the product of exactly two primes (primes need not be distinct). Print “Yes” if it is, otherwise “No.”

2. Key Observations  
- A number X is “nearly prime” iff its prime‐factorization has exactly two prime factors counted with multiplicity.  
  • e.g. 6 = 2·3 → two prime factors → nearly prime  
  • 9 = 3·3 → two prime factors (same prime twice) → nearly prime  
  • 4 = 2·2 → nearly prime  
  • 8 = 2·2·2 → three prime factors → not nearly prime  
  • 1 has zero prime factors → not nearly prime  
  • Any prime p has one prime factor → not nearly prime  
- To count prime factors (with multiplicity) of X ≤ 10^9, trial‐divide by all integers d from 2 up to √X.  
- Maintain a counter c; every time d divides X, do c++ and X /= d. If at any point c > 2, you can stop early.  
- After the loop, if the remaining X > 1, that remaining piece is a prime factor → c++.  
- Finally check if c == 2.

3. Full Solution Approach  
For each input number A:
  1. Let x = A, c = 0.  
  2. For d from 2 to floor(√x):  
       while x % d == 0:  
         c++;  
         x /= d;  
         if c > 2: break out of both loops early.  
     (Optionally skip even d>2 by testing 2 then odd d’s.)  
  3. If x > 1 after the loop, then x is prime → c++.  
  4. If c == 2, print “Yes”; otherwise print “No.”  
Complexity: each A costs O(√A) divisions. With N ≤ 10 and A ≤ 10^9, this runs comfortably within time limits.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

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

    int N;
    cin >> N;
    while (N--) {
        long long A;
        cin >> A;
        long long x = A;
        int count = 0;

        // Trial division from d = 2 up to sqrt(x)
        for (long long d = 2; d * d <= x; ++d) {
            // While d is a factor, divide it out and increment count
            while (x % d == 0) {
                ++count;
                x /= d;
                // Early exit if too many prime factors
                if (count > 2) break;
            }
            if (count > 2) break;
        }

        // If something >1 remains, it's a prime factor
        if (x > 1) {
            ++count;
        }

        // Exactly two prime factors => nearly prime
        if (count == 2) {
            cout << "Yes\n";
        } else {
            cout << "No\n";
        }
    }
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys
import math

def is_nearly_prime(x):
    """
    Return True if x has exactly two prime factors (with multiplicity).
    """
    count = 0
    # trial divide by 2 first
    while x % 2 == 0:
        count += 1
        x //= 2
        if count > 2:
            return False

    # now try odd divisors
    d = 3
    # only need to go up to sqrt(x)
    while d * d <= x:
        while x % d == 0:
            count += 1
            x //= d
            if count > 2:
                return False
        d += 2

    # if remaining x > 1, it's a prime factor
    if x > 1:
        count += 1

    return (count == 2)

def main():
    data = sys.stdin.read().split()
    N = int(data[0])
    nums = map(int, data[1:])

    for x in nums:
        print("Yes" if is_nearly_prime(x) else "No")

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

Explanation of key parts:  
- We count factors by repeated division.  
- Early exit as soon as count exceeds 2.  
- After trial division up to √x, any leftover x>1 must itself be prime, so contributes one more factor.  
- Finally check if total count equals 2.