RNG

Time limit1sMemory limit128 MB

Summary
Given y and a, b, c, n, find all x in [0, 2^n) with a x^2 + b x + c = y mod 2^n, printing x only when exactly one solution exists.
Level

Hard8 of 10

Topics
Number theory, Math, Brute force, Implementation
Solved
No attempts yet

Problem

Hyunsu is making a new adventure game featuring pirates and monkeys. To drive its many random elements, the game uses a random number generator, RNG for short.

Given the previous random number xx, the RNG produces the next number yy by

y=ax2+bx+c(mod2n)y = ax^2 + bx + c \pmod{2^n}

where aa, bb, cc, and nn are integers.

While debugging, Hyunsu sees the current random number yy in the debug console and wants to recover the number xx that was generated immediately before it.

Given the current number yy and the constants aa, bb, cc, nn, find the previous number xx (0≤x<2n0 \le x < 2^n) satisfying the equation above. If exactly one such xx exists, print it; if none exists or two or more exist, print No unique solution.

Input

The first line contains the number of test cases TT.

Each test case is given on one line as five space-separated integers yy, aa, bb, cc, nn. (0≤y,a,b,c<2n0 \le y, a, b, c < 2^n, 1≤n≤311 \le n \le 31)

Output

For each test case, print the previous number xx satisfying the equation, one per line.

If such an xx is not unique (there is none, or there are two or more), print No unique solution instead.

Examples1

  1. Example 1

    Input
    4
    26 2 1 5 5
    10 1 0 0 4
    1 1 1 1 4
    3 14 15 92 7
    
    Expected output
    3
    No unique solution
    No unique solution
    55