TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description


Hyper operator a(n)b
is a family of operators, where each level n

operator is defined as iteration of the previous level (n - 1)
operator. It is defined recursively as follows:




<!-- MATH
$a{(n)}b = \left { \begin{array}{cc} b+1, & \mbox{if } n=0 \a, & \mbox{if } n=1, b=0 \0, & \mbox{if } n=2,b=0 \1, & \mbox{if } n \ge 3, b = 0 \a{(n-1)}(a{(n)}(b-1)) & \mbox{if } n \ge 1, b \ge 1, a \ge 0 \end{array} \right.$
-->
a(n)b = WIDTH="20" HEIGHT="126" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-1.png"
ALT="$ \left\{\vphantom{ \begin{array}{cc} b+1, & \mbox{if } n=0 \\ a, & \mbox{if } ...
...n-1)}(a^{(n)}(b-1)) & \mbox{if } n \ge 1, b \ge 1, a \ge 0 \end{array} }\right.$"> WIDTH="330" HEIGHT="126" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-2.png"
ALT="$ \begin{array}{cc} b+1, & \mbox{if } n=0 \\ a, & \mbox{if } n=1, b=0 \\ 0, & ...
... \\ a^{(n-1)}(a^{(n)}(b-1)) & \mbox{if } n \ge 1, b \ge 1, a \ge 0 \end{array}$">




With some algebraic manipulations, it can be shown that the operators for n =
1, 2 and 3 correspond respectively to addition, multiplication and exponentiation; as given below:





<!-- MATH
$a{(1)}b = a + {1+1+1+ \cdots + 1} = a + b$
-->
a(1)b = a + 1+1+1+ ... +1 = a + b




<!-- MATH
$a{(2)}b = a + a + a + \cdots + a = a \times b$
-->
a(2)b = a + a + a + ... + a = a×b




<!-- MATH
$a{(3)}b = a \times a \times a \times \cdots \times a = a{b}$
-->
a(3)b = a×a×a× ... ×a = ab






For values of n > 3
, a(n)b

typically corresponds to a (very) tall exponentiation tower even for small values of a
and b
, which makes it difficult or even impossible to calculate exactly. Nevertheless, as they are being iterations (or iterations of iterations of ...
) of exponents, their modulus can still be calculated by means of modular hyper-exponentiation (a form of iterated modular exponentiation). Your task is to compute <!-- MATH
$a{(n)}b \bmod m$
-->
a(n)b mod m

, given the values of a
, b
, m
and n
.

Input Format


The first line of input contains an integer T
(not more than 100), the number of test cases follow. The next T
lines of input describe the input for each test case on one line, in the form of <!-- MATH
$m \ n \ a \ b$
-->
m n a b

. You can safely assume that <!-- MATH
$0 \le n \le 10$
-->
0 WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-3.png"
ALT="$ \le$">n WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-3.png"
ALT="$ \le$">10

; <!-- MATH
$0 \le a, b \le 1000$
-->
0 WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-3.png"
ALT="$ \le$">a, b WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-3.png"
ALT="$ \le$">1000

; and <!-- MATH
$1 \le m \le 10000$
-->
1 WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-3.png"
ALT="$ \le$">m WIDTH="18" HEIGHT="31" ALIGN="MIDDLE" BORDER="0"
SRC="/ntujudge/problemdata/635-3.png"
ALT="$ \le$">10000

.

Output Format


For each of the test cases, print the test case number followed by the value of <!-- MATH
$a{(n)}b \bmod m$
-->
a(n)b mod m

on one line. The sample output shows the exact format for printing the test case number.

Sample Input 1



4
78 1 123 456
257 3 16 16
2008 4 8 3
1000 4 13 27



Sample Output 1



Case #1: 33
Case #2: 1
Case #3: 816
Case #4: 53



Hints

Problem Source

Migrated from old NTUJ.

ACM ICPC regional Jakarta site 2008

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 10000 65536 200