Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- BufferedReader br;
- PrintWriter out;
- StringTokenizer st;
- boolean eof;
- double p;
- double[][] trueDistr;
- static final double MAX_ERR = 2e-3;
- double distrDistance(double[] a, double[] b) {
- double ret = 0;
- for (int i = 0; i < a.length; i++) {
- ret += Math.abs(a[i] - b[i]);
- }
- System.err.println(ret);
- return ret;
- }
- double getErr(double[] a, int n) {
- double mult = Math.sqrt(n * p * (1 - p));
- double add = n * p;
- double[] hist = new double[n + 1];
- double[] rErr = new double[a.length];
- for (int i = 0; i < a.length; i++) {
- double x = a[i];
- x = x * mult + add;
- double roundX = Math.round(x);
- rErr[i] = roundX - x;
- rErr[i] *= Math.sqrt(n * p * (1 - p));
- x = roundX;
- if (x < 0)
- x = 0;
- if (x > n)
- x = n;
- hist[(int) x]++;
- }
- Arrays.sort(rErr);
- double error = 0;
- for (int i = 0; i < a.length; i++) {
- double need = -0.5 + 1.0 * i / (a.length - 1);
- error += Math.abs(need - a[i]);
- }
- System.err.println("!" + error);
- for (int i = 0; i <= n; i++) {
- hist[i] /= a.length;
- }
- return distrDistance(hist, trueDistr[n]);
- }
- int guess(double[] a, int actual) {
- int ret = 0;
- for (int n = 1; n < MAX_N; n++) {
- double err = getErr(a, n);
- if (n == actual) {
- System.err.println("!!!!!!!!!!!!!!!!!");
- }
- if (err < MAX_ERR) {
- ret = n;
- }
- }
- return ret;
- }
- static final int MAX_N = 101;
- static final Random rng = new Random();
- static final int SAMPLE_SIZE = 10000;
- double genOneNumber(int n) {
- double ret = 0;
- for (int i = 0; i < n; i++) {
- if (rng.nextDouble() < p) {
- ret++;
- }
- }
- ret += rng.nextDouble() - 0.5;
- return (ret - n * p) / Math.sqrt(n * p * (1 - p));
- }
- double[] genSample(int n, int sampleSize) {
- double[] ret = new double[sampleSize];
- for (int i = 0; i < sampleSize; i++) {
- ret[i] = genOneNumber(n);
- }
- return ret;
- }
- Main() throws IOException {
- // br = new BufferedReader(new InputStreamReader(System.in));
- // out = new PrintWriter(System.out);
- br = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- // int t = nextInt();
- // p = nextDouble();
- p = 0.5;
- trueDistr = new double[MAX_N][];
- trueDistr[0] = new double[] { 1 };
- for (int i = 1; i < MAX_N; i++) {
- trueDistr[i] = new double[i + 1];
- for (int j = 0; j <= i; j++) {
- trueDistr[i][j] = (j == 0 ? 0 : trueDistr[i - 1][j - 1] * p)
- + (j == i ? 0 : trueDistr[i - 1][j] * (1 - p));
- }
- }
- // while (t-- > 0) {
- // int n = nextInt();
- // double[] a = new double[n];
- // for (int i = 0; i < n; i++) {
- // a[i] = nextDouble();
- // }
- // out.println(guess(a));
- // }
- int iters = 1;
- for (int i = 0; i < iters; i++) {
- int n = rng.nextInt(MAX_N - 1) + 1;
- double[] sample = genSample(n, SAMPLE_SIZE);
- int guess = guess(sample, n);
- System.err.println("guess " + guess + " actual " + n);
- }
- out.close();
- }
- public static void main(String[] args) throws IOException {
- new Main();
- }
- String nextToken() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (Exception e) {
- eof = true;
- return null;
- }
- }
- return st.nextToken();
- }
- String nextString() {
- try {
- return br.readLine();
- } catch (IOException e) {
- eof = true;
- return null;
- }
- }
- int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement