Problem: 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.

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
long double fac[200002], pexp[1002];
inline long double mypow(long double x, int n) {
  long double res = 1.0;
  while (n > 0) {
    if (n & 1) res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}
int solve() {
  const int n = 250;
  vector<int> vec(n);
  for (long long i = (long long)(0); i < (long long)(n); i++) cin >> vec[i];
  double p = accumulate((vec).begin(), (vec).end(), 0) / (double)n;
  double var = 0;
  for (long long i = (long long)(0); i < (long long)(n); i++)
    var += (vec[i] - p) * (vec[i] - p);
  var /= (double)n;
  double diffp = abs(p - var);
  double diffu = abs(p * p / 3.0 - var);
  if (diffp > diffu) {
    long double maxp = 0, res = -1;
    for (long long p = (long long)(10); p < (long long)(1001); p++) {
      long double np = 1.0;
      for (long long i = (long long)(0); i < (long long)(n); i++) {
        if (vec[i] < 0 || vec[i] > 2 * p)
          np *= 0.0;
        else
          np *= 1.0 / (2.0 * (long double)p + 1);
      }
      if (maxp < np) {
        maxp = np;
        res = p;
      }
    }
    return (int)(res + 0.5);
  } else {
    long double maxp = 0, res = -1;
    for (long long p = (long long)(10); p < (long long)(1001); p++) {
      long double np = 1.0;
      for (long long i = (long long)(0); i < (long long)(n); i++) {
        np *= (long double)mypow((long double)p, vec[i]) * pexp[p] /
              (vec[i] > 200000 ? 1e100 : fac[vec[i]]);
      }
      if (maxp < np) {
        maxp = np;
        res = p;
      }
    }
    return (int)(res + 0.5);
  }
}
int main() {
  fac[0] = fac[1] = 1.0;
  for (long long i = (long long)(1); i < (long long)(200000); i++)
    fac[i + 1] = fac[i] * (i + 1);
  for (long long p = (long long)(10); p < (long long)(1002); p++) {
    pexp[p] = exp(-(long double)p);
  }
  int t;
  cin >> t;
  for (long long i = (long long)(0); i < (long long)(t); i++) {
    printf("%d\n", solve());
  }
  return 0;
}