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];
int cnt[5000];
inline long double sqr(long double x) { return x * x; }
inline int rand1(int P) {
  long double sum = 0;
  long double x = (long double)rand() / RAND_MAX;
  long double frac = 1;
  for (int k = 0; k <= 2 * P; ++k) {
    if (k) {
      frac *= k;
    }
    sum += (pow(P, k) * exp(-P)) / frac;
    if (x <= sum) {
      return k;
    }
  }
  return 2 * P;
}
inline int rand2(int P) {
  long double x = (long double)rand() / RAND_MAX;
  return (int)floor(x * (2 * P + 1));
}
int main(void) {
  int cas, n;
  srand(time(NULL));
  scanf("%d", &cas);
  while (cas--) {
    long double sum = 0;
    for (int i = 0; i < 250; ++i) {
      scanf("%d", a + i);
      sum += a[i];
    }
    sum /= 250;
    long double fangcha = 0;
    for (int i = 0; i < 250; ++i) {
      fangcha += sqr(a[i] - sum);
    }
    fangcha /= 250;
    sort(a, a + 250);
    if (fangcha < sum * 2) {
      int n = round(sum);
      if (n >= 50) {
        printf("%d\n", n);
        continue;
      }
      memset(cnt, 0, sizeof cnt);
      for (int i = 0; i < 250; ++i) {
        if (a[i] <= 100) {
          ++cnt[a[i]];
        }
      }
      int ans;
      long double best = 1e18;
      for (n = 10; n <= 50; ++n) {
        long double frac = 1, sum = 0;
        for (int k = 0; k <= 100; ++k) {
          if (k) {
            frac *= k;
          }
          long double tmp = pow(n, k) / frac / exp(n) * 250;
          sum += abs(tmp - cnt[k]);
        }
        if (sum < best) {
          best = sum;
          ans = n;
        }
      }
      printf("%d\n", ans);
    } else {
      printf("%d\n", (a[249] + 1) / 2);
    }
  }
  return 0;
}