#P4543. [JSOI2011] Apple 的美食

[JSOI2011] Apple 的美食

Description

Apple really likes eating chocolate, but it is a picky pig, and some kinds of chocolate cannot satisfy its taste. If the owner gives it chocolate that it does not like, it will be very unhappy. The owner does not know what kind of chocolate Apple likes, but she knows that the cocoa content is an important factor affecting a chocolate’s taste, so she decides to judge which chocolates to give Apple based on their cocoa content.

Assume that the cocoa content of a chocolate has 1N1\sim N different measured levels. She considers chocolates whose cocoa content lies in the range [a,b][a,b] to be tasty. However, she does not know what this range is, and you need to help determine a range so that she can let Apple eat as many chocolates it likes as possible.

Let posipos_i be the number of chocolates with cocoa butter content ii that Apple thinks are tasty, and let negineg_i be the number of chocolates with cocoa butter content ii that Apple thinks are not tasty. These two sequences are generated as follows:

$$\begin{cases}pos_i=a_{2i-1}\\neg_i=a_{2i}\end{cases}$$

where ai=(p1ai1+p2)modM(i>1)a_i=(p_1a_{i-1}+p_2)\bmod M(i>1).

Let TPTP be the number of chocolates that Apple thinks are tasty and the owner also judges as tasty, TNTN be the number of chocolates that Apple thinks are not tasty and the owner also judges as not tasty, FPFP be the number of chocolates that Apple thinks are not tasty but the owner judges as tasty, and FNFN be the number of chocolates that Apple thinks are tasty but the owner judges as not tasty.

Let rr be the ratio of chocolates correctly judged as tasty among all truly tasty chocolates, and let pp be the ratio of chocolates correctly judged as tasty among all chocolates judged as tasty. Then

$$\begin{cases}r=\frac{TP}{TP+FN}\\p=\frac{TP}{TP+FP}\end{cases}$$

Please help Apple find a range that maximizes f=2prp+rf=\frac{2pr}{p+r}.

Input Format

The input consists of multiple lines. Each line is one dataset: N,a1,p1,p2,MN,a_1,p_1,p_2,M, separated by spaces.

The input ends with a single 00. The number of datasets does not exceed 10001000.

Output Format

For each dataset, output one line: the maximum value of ff (rounded to 66 decimal places).

4 4 4 1 5
12 9 6 6 11
0
0.800000
0.683938

Hint

Constraints

For 100%100\% of the testdata, 1N106,a1,p1,p2<M201\leq N\leq 10^6,a_1,p_1,p_2<M\leq 20. It is guaranteed that there is at least one chocolate that Apple thinks is tasty.

Translated by ChatGPT 5