Professor A. Karim is working on a project of measuring the surface area of an unknown unearthly object. After a lot of calculation he finds that the surface area of that object is (a+b)(1-ab), where a and b are two parameters related to surface area of that object. With the help of some more advanced experiments he finds N floating-point numbers, which can be possible values of a and b. From the N numbers he can select two values for a and b in NC2 ways (Note that the selections a=2, b=3 and a=3, b=2 are considered same because (a+b)(1-ab) is equal to (b+a)(1-ba)). Karim needs to do some more expensive experiments to find out the real value of a and b, but before doing that he wants to keep only the obvious choices: the selections, which cause the surface of the object to be positive (Greater than zero). Your job is to help Prof. Karim to count how many of the NC2 selections (the value of a and b) causes (a+b)(1-ab) to be positive. Please note that your method must be efficient. (An O(N2) solution will not do)
Input
The input fine contains maximum 7 sets of inputs.
First line of each set contains an integer N (0<N<=10000). Each of the next N lines contains one floating-point number F (|F|<30000.0). The meaning of N is given in the problem statement.
The Input can have the same number twice or even more times. In such cases two same numbers should be considered different.
Input is terminated by a case where the value of N is zero. This case should not be processed.
Output
For each set of input produce one line of output. This line contains the serial no of output followed by an integer which indicates how many of the NC2 selections will cause the value of the expression (a+b)(1-ab) to be positive. Look at the output for sample input for details. You can consider any value greater than 10-15 is positive.
Sample Input
|
|
Output for Sample Input
|
|
Solution
題目描述:
從一堆數字中,任挑兩個實數出來,計算 (a + b) * (1 + a * b) > 1e-15
的對數有多少個。
題目解法:
固定其中一個數字 a 後,可以找到 b 的一元二次方程式,找到區間解的個數即可。
將輸入的數字排序,二分根的位置即可,但是麻煩的地方在於 > 1e-15
,因此這部分做了些許的微調。
// 因為沒有考慮 1e-15 吞了不少 WA。
|
|