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;
const long double e = exp(1.0);
int t;
int a[257];
long double q[2007][2507];
long double poiss(int p) {
  long double res = 0;
  for (int i = 1; i <= 250; ++i) {
    res += log(q[p][a[i]]);
  }
  return res;
}
long double uni(int p) {
  long double res = 0;
  for (int i = 1; i <= 250; ++i) {
    if (a[i] > 2 * p) return -1e9;
    res += log((long double)1 / (2 * p + 1));
  }
  return res;
}
void solve() {
  for (int i = 1; i <= 250; ++i) {
    cin >> a[i];
  }
  int ans = -1, best = -1e9;
  for (int i = 10; i <= 1000; ++i) {
    long double u = uni(i);
    if (u > best) {
      best = u;
      ans = i;
    }
    long double p = poiss(i);
    if (p > best) {
      best = p;
      ans = i;
    }
  }
  cout << ans << '\n';
}
long double brut(int p, int k) {
  long double res = 1;
  int cp = p, ck = k;
  while (cp || ck) {
    if (cp && ck) {
      if (res > 1) {
        if ((long double)p / ck > (long double)1 / e) {
          res *= p;
          res /= ck--;
        } else {
          res /= e;
          --cp;
        }
      } else {
        if ((long double)p / ck < (long double)1 / e) {
          res *= p;
          res /= ck--;
        } else {
          res /= e;
          --cp;
        }
      }
    } else if (cp) {
      res /= e;
      --cp;
    } else if (ck) {
      res *= p;
      res /= ck--;
    }
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  for (int p = 1; p <= 1000; ++p) {
    q[p][p] = brut(p, p);
    for (int i = p + 1; i <= 2500; ++i) {
      q[p][i] = q[p][i - 1] * p / i;
    }
    for (int i = p - 1; i >= 0; --i) {
      q[p][i] = q[p][i + 1] * (i + 1) / p;
    }
  }
  cin >> t;
  while (t--) solve();
}