Magical, Marvelous Tour

Time limit5sMemory limit512 MB

Summary
Arnar picks a contiguous segment, Solveig claims the largest of the three parts it creates, and Arnar keeps the rest.
Level

Medium6 of 10

Topics
Prefix sum, Two pointers
Solved
No attempts yet

Problem

The owner of an electronics factory hid a golden transistor inside seven of her devices. Whoever buys one of those devices is invited to tour the factory.

Arnar and Solveig heard that exactly one device in their local store holds a golden transistor. They pooled their money, bought every device in the store, lined the devices up and numbered them 00 to N−1N-1. Each device holds some number of transistors. Then they agreed on a rule for deciding who keeps the golden transistor.

Arnar first picks a range [a,b][a, b], both ends included, with 0≤a≤b<N0 \le a \le b < N. Solveig then picks one group of devices to take.

  • If a>0a > 0, she may take every device in [0,a−1][0, a-1].
  • If b<N−1b < N-1, she may take every device in [b+1,N−1][b+1, N-1].
  • She may always take every device in [a,b][a, b].

Once Solveig has picked a group, Arnar keeps every device she did not take.

For example, with three devices and Arnar picking [1,1][1, 1], Solveig picks one of [0,0][0, 0], [1,1][1, 1] and [2,2][2, 2]. If Arnar picks [1,2][1, 2], Solveig picks either [0,0][0, 0] or [1,2][1, 2].

The golden transistor is equally likely to be any one of the transistors, so a person's chance of touring the factory is the number of transistors that person keeps divided by the total number of transistors. Both of them pick so that their own chance is as large as possible. Find Arnar's chance of touring the factory.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains five integers NN, pp, qq, rr and ss. There are NN devices, and device ii holds ((i×p+q) mod r)+s((i \times p + q) \bmod r) + s transistors. The devices are numbered 00 to N−1N-1.

Constraints:

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1061 \le N \le 10^6
  • 1≤p,q,r,s≤1061 \le p, q, r, s \le 10^6
  • The sum of NN over all test cases is at most 2×1062 \times 10^6.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is Arnar's chance of touring the factory. Print yy with exactly 10 digits after the decimal point, rounding the 11th digit half up.

Examples2

  1. Example 1

    Input
    8
    1 1 1 1 1
    10 17 1 7 1
    2 100 100 200 1
    20 17 3 23 100
    10 999999 999999 1000000 1000000
    2 1 1 1 1
    3 1 99 100 1
    999999 1000000 999999 1000000 1000000
    
    Expected output
    Case #1: 0.0000000000
    Case #2: 0.6111111111
    Case #3: 0.0098039216
    Case #4: 0.6471920290
    Case #5: 0.6000006000
    Case #6: 0.5000000000
    Case #7: 0.0291262136
    Case #8: 0.6666666667
    
  2. Example 2

    Input
    3
    1 1000000 1000000 1000000 1000000
    2 1 1 1 1000000
    3 1 1 1 5
    
    Expected output
    Case #1: 0.0000000000
    Case #2: 0.5000000000
    Case #3: 0.6666666667