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;
int a[250];
double log_fact[5001], inf;
inline double get_log_fact(int k) { return log_fact[min(k, 5000)]; }
double get_poisson_prob(int p) {
  double sum = 0;
  for (int i = 0; i < 250; ++i) {
    int k = a[i];
    double single_prob = log(p) * k - p - get_log_fact(k);
    sum += single_prob;
  }
  return sum;
}
double get_uniform_prob(int p) {
  for (int i = 0; i < 250; ++i) {
    int k = a[i];
    if (k > 2 * p) {
      return -inf;
    }
  }
  return -log(p * 2 + 1) * 250;
}
int main(void) {
  log_fact[0] = 0;
  inf = 1e100;
  for (int k = 1; k <= 5000; ++k) {
    log_fact[k] = log_fact[k - 1] + log(k);
    inf = inf * inf;
  }
  int cas;
  scanf("%d", &cas);
  while (cas--) {
    for (int i = 0; i < 250; ++i) {
      scanf("%d", a + i);
    }
    double prob = -inf;
    pair<int, bool> ans = pair<int, bool>(-1, false);
    for (int p = 10; p <= 1000; ++p) {
      double prob_poisson = get_poisson_prob(p);
      if (prob_poisson > prob) {
        prob = prob_poisson;
        ans = pair<int, bool>(p, false);
      }
      double prob_uniform = get_uniform_prob(p);
      if (prob_uniform > prob) {
        prob = prob_uniform;
        ans = pair<int, bool>(p, true);
      }
    }
    printf("%d\n", ans.first);
  }
  return 0;
}