Advertisement
Guest User

Untitled

a guest
Aug 24th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     BufferedReader br;
  7.     PrintWriter out;
  8.     StringTokenizer st;
  9.     boolean eof;
  10.  
  11.     double p;
  12.  
  13.     double[][] trueDistr;
  14.  
  15.     static final double MAX_ERR = 2e-3;
  16.  
  17.     double distrDistance(double[] a, double[] b) {
  18.         double ret = 0;
  19.         for (int i = 0; i < a.length; i++) {
  20.             ret += Math.abs(a[i] - b[i]);
  21.         }
  22.         System.err.println(ret);
  23.         return ret;
  24.     }
  25.  
  26.     double getErr(double[] a, int n) {
  27.         double mult = Math.sqrt(n * p * (1 - p));
  28.         double add = n * p;
  29.         double[] hist = new double[n + 1];
  30.  
  31.         double[] rErr = new double[a.length];
  32.  
  33.         for (int i = 0; i < a.length; i++) {
  34.  
  35.             double x = a[i];
  36.  
  37.             x = x * mult + add;
  38.             double roundX = Math.round(x);
  39.  
  40.             rErr[i] = roundX - x;
  41.             rErr[i] *= Math.sqrt(n * p * (1 - p));
  42.  
  43.             x = roundX;
  44.  
  45.             if (x < 0)
  46.                 x = 0;
  47.             if (x > n)
  48.                 x = n;
  49.             hist[(int) x]++;
  50.         }
  51.        
  52.         Arrays.sort(rErr);
  53.         double error = 0;
  54.         for (int i = 0; i < a.length; i++) {
  55.             double need = -0.5 + 1.0 * i / (a.length - 1);
  56.             error += Math.abs(need - a[i]);
  57.         }
  58.        
  59.         System.err.println("!" +  error);
  60.  
  61.         for (int i = 0; i <= n; i++) {
  62.             hist[i] /= a.length;
  63.         }
  64.  
  65.         return distrDistance(hist, trueDistr[n]);
  66.     }
  67.  
  68.     int guess(double[] a, int actual) {
  69.         int ret = 0;
  70.         for (int n = 1; n < MAX_N; n++) {
  71.             double err = getErr(a, n);
  72.             if (n == actual) {
  73.                 System.err.println("!!!!!!!!!!!!!!!!!");
  74.             }
  75.             if (err < MAX_ERR) {
  76.                 ret = n;
  77.             }
  78.         }
  79.         return ret;
  80.     }
  81.  
  82.     static final int MAX_N = 101;
  83.     static final Random rng = new Random();
  84.  
  85.     static final int SAMPLE_SIZE = 10000;
  86.  
  87.     double genOneNumber(int n) {
  88.         double ret = 0;
  89.         for (int i = 0; i < n; i++) {
  90.             if (rng.nextDouble() < p) {
  91.                 ret++;
  92.             }
  93.         }
  94.         ret += rng.nextDouble() - 0.5;
  95.         return (ret - n * p) / Math.sqrt(n * p * (1 - p));
  96.     }
  97.  
  98.     double[] genSample(int n, int sampleSize) {
  99.         double[] ret = new double[sampleSize];
  100.         for (int i = 0; i < sampleSize; i++) {
  101.             ret[i] = genOneNumber(n);
  102.         }
  103.         return ret;
  104.     }
  105.  
  106.     Main() throws IOException {
  107.         // br = new BufferedReader(new InputStreamReader(System.in));
  108.         // out = new PrintWriter(System.out);
  109.         br = new BufferedReader(new InputStreamReader(System.in));
  110.         out = new PrintWriter(System.out);
  111.  
  112.         // int t = nextInt();
  113.         // p = nextDouble();
  114.  
  115.         p = 0.5;
  116.  
  117.         trueDistr = new double[MAX_N][];
  118.  
  119.         trueDistr[0] = new double[] { 1 };
  120.  
  121.         for (int i = 1; i < MAX_N; i++) {
  122.             trueDistr[i] = new double[i + 1];
  123.             for (int j = 0; j <= i; j++) {
  124.                 trueDistr[i][j] = (j == 0 ? 0 : trueDistr[i - 1][j - 1] * p)
  125.                         + (j == i ? 0 : trueDistr[i - 1][j] * (1 - p));
  126.             }
  127.         }
  128.  
  129.         // while (t-- > 0) {
  130.         // int n = nextInt();
  131.         // double[] a = new double[n];
  132.         // for (int i = 0; i < n; i++) {
  133.         // a[i] = nextDouble();
  134.         // }
  135.         // out.println(guess(a));
  136.         // }
  137.  
  138.         int iters = 1;
  139.         for (int i = 0; i < iters; i++) {
  140.             int n = rng.nextInt(MAX_N - 1) + 1;
  141.             double[] sample = genSample(n, SAMPLE_SIZE);
  142.  
  143.             int guess = guess(sample, n);
  144.             System.err.println("guess " + guess + " actual " + n);
  145.         }
  146.         out.close();
  147.     }
  148.  
  149.     public static void main(String[] args) throws IOException {
  150.         new Main();
  151.     }
  152.  
  153.     String nextToken() {
  154.         while (st == null || !st.hasMoreTokens()) {
  155.             try {
  156.                 st = new StringTokenizer(br.readLine());
  157.             } catch (Exception e) {
  158.                 eof = true;
  159.                 return null;
  160.             }
  161.         }
  162.         return st.nextToken();
  163.     }
  164.  
  165.     String nextString() {
  166.         try {
  167.             return br.readLine();
  168.         } catch (IOException e) {
  169.             eof = true;
  170.             return null;
  171.         }
  172.     }
  173.  
  174.     int nextInt() throws IOException {
  175.         return Integer.parseInt(nextToken());
  176.     }
  177.  
  178.     long nextLong() throws IOException {
  179.         return Long.parseLong(nextToken());
  180.     }
  181.  
  182.     double nextDouble() throws IOException {
  183.         return Double.parseDouble(nextToken());
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement