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;
double fac[200005], ln[200005];
inline double Poi(int x, int P) { return x * ln[P] - P - fac[x]; }
inline double Uni(int x, int P) {
  if (x > 2 * P) return -1e20;
  return -ln[2 * P + 1];
}
const int N = 255;
int T;
int a[N];
int main() {
  int T;
  scanf("%d", &(T));
  fac[0] == 0;
  for (int i = 1; i <= 200000; i++) fac[i] = fac[i - 1] + (ln[i] = log(i));
  while (T--) {
    for (int i = 1; i <= 250; i++) scanf("%d", &(a[i]));
    int t = -1, P = -1;
    double maxv = -1e20;
    for (int p = 10; p <= 1000; p++) {
      double ret = 0;
      for (int i = 1; i <= 250; i++) ret += Poi(a[i], p);
      if (ret > maxv) maxv = ret, t = 0, P = p;
      ret = 0;
      for (int i = 1; i <= 250; i++) ret += Uni(a[i], p);
      if (ret > maxv) maxv = ret, t = 1, P = p;
    }
    printf("%d\n", P);
  }
  return 0;
}