Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.Math;
  4.  
  5.  
  6. public class AntiQS {
  7.     FastScanner in;
  8.     PrintWriter out;
  9.  
  10.  
  11.     public double binSearch(double a, int n) {
  12.         double low = 0;
  13.         double high = a;
  14.         while (ans(a, high, n) - ans(a, low, n) > 0.001) {
  15.             double m = (high - low) / 2;
  16.             if (!touch(a, m, n)) {
  17.                 high = m;
  18.             } else {
  19.                 low = m;
  20.             }
  21.         }
  22.         return ans(a, high, n);
  23.     }
  24.  
  25.     public double ans(double a, double b, int n) {
  26.         boolean f = true;
  27.         double[] E = new double[n];
  28.         E[0] = a;
  29.         E[1] = b;
  30.         for (int i = 2; i < n; i++) {
  31.             E[i] = ((E[i - 1] + 1) * 2) - E[i - 2];
  32.             if (E[i] < 0) f = false;
  33.         }
  34.         return E[n - 1];
  35.     }
  36.  
  37.     public boolean touch(double a, double b, int n) {
  38.         boolean f = true;
  39.         double[] E = new double[n];
  40.         E[0] = a;
  41.         E[1] = b;
  42.         if (a <= 0 || b <= 0) {
  43.             return false;
  44.         }
  45.         for (int i = 2; i < n; i++) {
  46.             E[i] = ((E[i - 1] + 1) * 2) - E[i - 2];
  47.             if (E[i] <= 0) f = false;
  48.         }
  49.         return f;
  50.     }
  51.  
  52.     public void solve() throws IOException {
  53.         int n;
  54.         double a;
  55.         a = Double.parseDouble(in.next());
  56.         n = in.nextInt();
  57.         double t = binSearch(a, n);
  58.         out.print(t);
  59.     }
  60.  
  61.     public void run() {
  62.         try {
  63.             in = new FastScanner(new File("style.in"));
  64.             out = new PrintWriter(new File("style.out"));
  65.  
  66.             solve();
  67.  
  68.             out.close();
  69.         } catch (IOException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73.  
  74.     class FastScanner {
  75.         BufferedReader br;
  76.         StringTokenizer st;
  77.  
  78.         FastScanner(File f) {
  79.             try {
  80.                 br = new BufferedReader(new FileReader(f));
  81.             } catch (FileNotFoundException e) {
  82.                 e.printStackTrace();
  83.             }
  84.         }
  85.  
  86.         String next() {
  87.             while (st == null || !st.hasMoreTokens()) {
  88.                 try {
  89.                     st = new StringTokenizer(br.readLine());
  90.                 } catch (IOException e) {
  91.                     e.printStackTrace();
  92.                 }
  93.             }
  94.             return st.nextToken();
  95.         }
  96.  
  97.         int nextInt() {
  98.             return Integer.parseInt(next());
  99.         }
  100.     }
  101.  
  102.     public static void main(String[] arg) {
  103.         new AntiQS().run();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement