|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +/* |
| 3 | +CS203_DSAA_template |
| 4 | +
|
| 5 | +Copyright (C) 2023 nanos Certseeds |
| 6 | +*/ |
| 7 | + |
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Scanner; |
| 14 | +import java.util.StringTokenizer; |
| 15 | +import java.util.stream.Collectors; |
| 16 | + |
| 17 | +public final class Main { |
| 18 | + // f'(0) = -2y, 说明肯定是先减 |
| 19 | + // f'(100) = 5*10^14 + ... - 200y(<= 2*10^12) > 0,到最后递增 |
| 20 | + // find where f'(x) equals to 0 |
| 21 | + private static final class funcer { |
| 22 | + public funcer(double y) { |
| 23 | + this.y = y; |
| 24 | + } |
| 25 | + |
| 26 | + private final double y; |
| 27 | + |
| 28 | + public void setPosi(double posi) { |
| 29 | + this.posi = posi; |
| 30 | + } |
| 31 | + |
| 32 | + private double posi = 0; |
| 33 | + |
| 34 | + public double level0() { |
| 35 | + final double x = posi; |
| 36 | + assert ((0 <= x) && (x <= 100)); |
| 37 | + return ((((5 * x + 6) * x * x * x + 3) * x + 4) * x - 2 * y) * x; |
| 38 | + } |
| 39 | + |
| 40 | + public double level1(double x) { |
| 41 | + assert ((0 <= x) && (x <= 100)); |
| 42 | + return (((35 * x + 36) * x * x * x + 9) * x + 8) * x - 2 * y; |
| 43 | + } |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + public static List<funcer> read() { |
| 48 | + final var input = new Scanner(System.in); |
| 49 | + final int testcases = input.nextInt(); |
| 50 | + assert ((1 <= testcases) && (testcases <= 100)); |
| 51 | + final var cases = new ArrayList<funcer>(testcases); |
| 52 | + for (int i = 0; i < testcases; i++) { |
| 53 | + final double number = input.nextDouble(); |
| 54 | + assert ((0 < number) && (number < Math.pow(10, 10))); |
| 55 | + cases.add(new funcer(number)); |
| 56 | + } |
| 57 | + return cases; |
| 58 | + } |
| 59 | + |
| 60 | + public static List<funcer> reader() { |
| 61 | + final var input = new Reader(); |
| 62 | + final int testcases = input.nextInt(); |
| 63 | + assert ((1 <= testcases) && (testcases <= 100)); |
| 64 | + final var cases = new ArrayList<funcer>(testcases); |
| 65 | + for (int i = 0; i < testcases; i++) { |
| 66 | + final double number = input.nextDouble(); |
| 67 | + assert ((0 < number) && (number < Math.pow(10, 10))); |
| 68 | + cases.add(new funcer(number)); |
| 69 | + } |
| 70 | + return cases; |
| 71 | + } |
| 72 | + |
| 73 | + private static final double minidiff = 0.0000_0000_0000_0000_1d; // step should smaller enough |
| 74 | + private static final double fivezero = 0.000_01d; // but just need five or six |
| 75 | + |
| 76 | + public static List<funcer> cal(List<funcer> nums) { |
| 77 | + final List<funcer> results = nums.stream() |
| 78 | + .map(one -> { |
| 79 | + for (double begin = 0, end = 100; Math.abs(begin - end) > minidiff; ) { |
| 80 | + final double mid = (end - begin) / 2 + begin; |
| 81 | + final double value = one.level1(mid); |
| 82 | + if (Math.abs(value) < fivezero) { |
| 83 | + one.setPosi(mid); |
| 84 | + return one; |
| 85 | + } else if (value > 0) { |
| 86 | + end = mid - minidiff; // mid is too big |
| 87 | + } else { |
| 88 | + begin = mid + minidiff; // mid is too small |
| 89 | + } |
| 90 | + } |
| 91 | + return one; |
| 92 | + }) |
| 93 | + .collect(Collectors.toUnmodifiableList()); |
| 94 | + return results; |
| 95 | + } |
| 96 | + |
| 97 | + public static void main(String[] args) throws IOException { |
| 98 | + final var datas = reader(); |
| 99 | + final var result = cal(datas); |
| 100 | + output(result); |
| 101 | + } |
| 102 | + |
| 103 | + public static void output(List<funcer> decides) { |
| 104 | + for (int i = 1; i <= decides.size(); i++) { |
| 105 | + System.out.printf("Case %d: %.4f\n", i, decides.get(i - 1).level0()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // refactor from https://github.com/Kattis/kattio/blob/master/Kattio.java |
| 110 | + // url: https://raw.githubusercontent.com/Kattis/kattio/master/Kattio.java |
| 111 | + // license: MIT |
| 112 | + private static final class Reader { |
| 113 | + private final BufferedReader br; |
| 114 | + private StringTokenizer st; |
| 115 | + |
| 116 | + private Reader() { |
| 117 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 118 | + } |
| 119 | + |
| 120 | + String next() { |
| 121 | + while (st == null || !st.hasMoreElements()) { |
| 122 | + try { |
| 123 | + st = new StringTokenizer(br.readLine()); |
| 124 | + } catch (IOException e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + } |
| 128 | + return st.nextToken(); |
| 129 | + } |
| 130 | + |
| 131 | + int nextInt() {return Integer.parseInt(next());} |
| 132 | + |
| 133 | + long nextLong() {return Long.parseLong(next());} |
| 134 | + |
| 135 | + double nextDouble() {return Double.parseDouble(next());} |
| 136 | + |
| 137 | + String nextLine() { |
| 138 | + String str = ""; |
| 139 | + try { |
| 140 | + str = br.readLine(); |
| 141 | + } catch (IOException e) { |
| 142 | + e.printStackTrace(); |
| 143 | + } |
| 144 | + return str; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments