Logo
(追記) (追記ここまで)

13358번 - Exponial 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB91921217622.194%

문제

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:

  • Exponentiation: \( 42^{2016}= \underbrace {42 \cdot 42 \cdot ... \cdot 42}_\text{2016 times} \)
  • Factorials: 2016! = 2016 · 2015 · . . . · 2 · 1.

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.

제한

예제 입력 1

2 42

예제 출력 1

2

예제 입력 2

5 123456789

예제 출력 2

16317634

예제 입력 3

94 265

예제 출력 3

39

힌트

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > Nordic Collegiate Programming Contest > NCPC 2016 E번

  • 문제를 만든 사람: Per Austrin
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /