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 int N = 100001;
double logFact[N];
int main() {
  int maxItr = 250;
  for (int i = 2; i < N; i++) logFact[i] = logFact[i - 1] + log(i);
  int v;
  cin >> v;
  for (int i = 0; i < v; i++) {
    vector<int> vec;
    for (int j = 0; j < maxItr; j++) {
      int x;
      cin >> x;
      vec.push_back(x);
    }
    double maxi = -1E18;
    string ans = "";
    int P = -1;
    for (int k = 10; k <= 1000; k++) {
      double lnP = log(k);
      double ln2P = log(2 * k + 1);
      double poisson = 0;
      double uniform = 0;
      for (int j = 0; j < maxItr; j++)
        if (vec[j] <= 2 * k)
          uniform -= ln2P;
        else
          uniform -= 1E18;
      poisson = -maxItr * k;
      for (int j = 0; j < maxItr; j++)
        poisson += vec[j] * lnP - logFact[vec[j]];
      if (poisson > maxi) {
        maxi = poisson;
        P = k;
        ans = "poisson";
      }
      if (uniform > maxi) {
        maxi = uniform;
        P = k;
        ans = "uniform";
      }
    }
    cout << P << endl;
  }
  return 0;
}