#include <bits/stdc++.h>
const double inf = 1e9, e = 2.718281828459;
using namespace std;
const int N = 305, M = 100005, mod = 1e9 + 7;
template <class T>
inline void gmin(T &x, const T &y) {
  if (x > y) x = y;
}
template <class T>
inline void gmax(T &x, const T &y) {
  if (x < y) x = y;
}
inline void ch(int &x, int y) { x = (x + y) % mod; }
inline void read(int &x) {
  x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
int a[N], cnt[2005];
double p[2005], ans;
double calc1(int P) {
  long double l = 1.0;
  double ans = 0, sum = 0;
  for (int i = 1; i <= P; i++) l /= e;
  int si = 0;
  for (int i = 0; i <= P * 2; i++) {
    ans += ((l - (double)cnt[i] / 250.0) * (l - (double)cnt[i] / 250.0));
    l = l * P / (i + 1);
    if (ans > ::ans) return ans;
  }
  if (si > 1) return inf;
  return ans;
}
double calc2(int P) {
  for (int i = 1; i <= 250; i++)
    if (a[i] > P * 2) return inf;
  double ans = 0.0;
  for (int i = 0; i <= P * 2; i++) {
    ans += ((1.0 / P / 2 - (double)cnt[i] / 250.0) *
            (1.0 / P / 2 - (double)cnt[i] / 250.0));
    if (ans > ::ans) return ans;
  }
  return ans;
}
double b[2005], c[2005];
struct node {
  double x;
  int y;
} d[2005];
bool cmp(node a, node b) { return a.x < b.x; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ans = inf;
    int res = 0;
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= 250; i++) scanf("%d", &a[i]), cnt[a[i]]++;
    for (int P = 10; P <= 1000; P++) {
      b[P] = calc1(P);
      c[P] = calc2(P);
      if (b[P] < ans) ans = b[P], res = P;
      if (c[P] < ans) ans = c[P], res = P;
    }
    printf("%d\n", res);
  }
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations.

Can you help Heidi estimate each village's population?

Input

Same as the easy version.

Output

Output one line per village, in the same order as provided in the input, containing your (integer) population estimate.

Your answer is considered correct if it is an integer that falls into the interval <image>, where P is the real population of the village, used to create the distribution (either Poisson or uniform) from which the marmots drew their answers.

### Response

```cpp
#include <bits/stdc++.h>
const double inf = 1e9, e = 2.718281828459;
using namespace std;
const int N = 305, M = 100005, mod = 1e9 + 7;
template <class T>
inline void gmin(T &x, const T &y) {
  if (x > y) x = y;
}
template <class T>
inline void gmax(T &x, const T &y) {
  if (x < y) x = y;
}
inline void ch(int &x, int y) { x = (x + y) % mod; }
inline void read(int &x) {
  x = 0;
  char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
}
int a[N], cnt[2005];
double p[2005], ans;
double calc1(int P) {
  long double l = 1.0;
  double ans = 0, sum = 0;
  for (int i = 1; i <= P; i++) l /= e;
  int si = 0;
  for (int i = 0; i <= P * 2; i++) {
    ans += ((l - (double)cnt[i] / 250.0) * (l - (double)cnt[i] / 250.0));
    l = l * P / (i + 1);
    if (ans > ::ans) return ans;
  }
  if (si > 1) return inf;
  return ans;
}
double calc2(int P) {
  for (int i = 1; i <= 250; i++)
    if (a[i] > P * 2) return inf;
  double ans = 0.0;
  for (int i = 0; i <= P * 2; i++) {
    ans += ((1.0 / P / 2 - (double)cnt[i] / 250.0) *
            (1.0 / P / 2 - (double)cnt[i] / 250.0));
    if (ans > ::ans) return ans;
  }
  return ans;
}
double b[2005], c[2005];
struct node {
  double x;
  int y;
} d[2005];
bool cmp(node a, node b) { return a.x < b.x; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    ans = inf;
    int res = 0;
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= 250; i++) scanf("%d", &a[i]), cnt[a[i]]++;
    for (int P = 10; P <= 1000; P++) {
      b[P] = calc1(P);
      c[P] = calc2(P);
      if (b[P] < ans) ans = b[P], res = P;
      if (c[P] < ans) ans = c[P], res = P;
    }
    printf("%d\n", res);
  }
  return 0;
}

```