This page is still under construction.

Parts of this page are still being built. What you see may change.

Candies

Time limit40sMemory limit1024 MB

Summary
Find the maximum-sum contiguous subarray with at most O odd values and total sum at most D, where the values are generated by a linear recurrence.
Level

Hard8 of 10

Topics
Array, Prefix sum, Binary search, Sliding window
Solved
No attempts yet

Problem

Supervin loves to eat candies. Today, his favorite candy shop is offering N candies, which are arranged in a line. The i-th candy in the line (counting starting from 1) has a sweetness level Si. Note that the sweetness level of a candy might be negative, which means the candy tastes bitter.

Supervin likes to eat sweet candies. However, candies with a combined sweetness level of more than D would be too much sweetness even for him. Supervin also realises that a candy with an odd sweetness level is "odd", and he does not want to eat more than O odd candies. In other words, an odd candy is a candy with a sweetness level that is not evenly divisible by 2. Additionally, since Supervin is in a rush, he can only eat a single contiguous subset of candies.

Therefore, he wants to eat a contiguous non-empty subset of candies in which there are at most O odd candies and the total sweetness level is maximized, but not more than D. Help Supervin to determine the maximum total sweetness level he can get, or return IMPOSSIBLE if there is no contiguous subset satisfying these constraints.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case contains two lines. The first line contains three integers N, O, and D, as described above. The second line contains seven integers X1, X2, A, B, C, M, L; these values are used to generate the values Si, as follows:

We define:

  • Xi = (A × Xi - 1 + B × Xi - 2 + C) modulo M, for i = 3 to N.
  • Si = Xi + L, for i = 1 to N.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum total sweetness level Supervin can get, or IMPOSSIBLE if there is no possible contiguous subset satisfying the problem constraints.

Limits

  • 1 ≤ T ≤ 100.
  • 2 ≤ N ≤ 5 × 105.
  • 0 ≤ O ≤ N.
  • -1015 ≤ D ≤ 1015.
  • 0 ≤ X1, X2, A, B, C ≤ 109.
  • 1 ≤ M ≤ 109.

Examples1

  1. Example 1

    Input
    2
    6 1 1000000000000000
    1 1 1 1 0 100 0
    6 1 -100
    1 1 1 1 0 100 0
    
    Expected output
    Case #1: 13
    Case #2: IMPOSSIBLE