시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 647 | 153 | 123 | 22.694% |
Everybody loves big numbers (if you do not, you might want to stop reading at this point). There are many ways of constructing really big numbers known to humankind, for instance:
In this problem we look at their lesser-known love-child the exponial, which is an operation defined for all positive integers n as
exponial(n) = \( n^{{(n-1)}^{{{(n-2)}^{{...}^{{2^1}}}}}} \)
For example, exponial(1) = 1 and exponial(5) = \( 5^{4^{3^{2^1}}} \) ≈ 6.206 · 10183230 which is already pretty big. Note that exponentiation is right-associative: \( a^{b^c}=a^{(b^c)} \).
Since the exponials are really big, they can be a bit unwieldy to work with. Therefore we would like you to write a program which computes exponial(n) mod m (the remainder of exponial(n) when dividing by m).
The input consists of two integers n (1 ≤ n ≤ 109) and m (1 ≤ m ≤ 109)
Output a single integer, the value of exponial(n) mod m.
2 42
2
5 123456789
16317634
94 265
39
ICPC > Regionals > Europe > Northwestern European Regional Contest > Nordic Collegiate Programming Contest > NCPC 2016 E번