tipstar0125 / atcoder

atcoder
6 stars 0 forks source link

M^(K^N) mod Pとフェルマーの小定理 #9

Open tipstar0125 opened 11 months ago

tipstar0125 commented 11 months ago

https://atcoder.jp/contests/abc228/tasks/abc228_e

tipstar0125 commented 11 months ago

解説:https://atcoder.jp/contests/abc228/editorial/2932

tipstar0125 commented 11 months ago

フェルマーの小定理:M^(P-1) = 1 (mod P) により、Mの(P-1)乗は1なので、K^Nを(P-1)で割った商は1になるので、余りだけを考えればよい。 よって、r = K^N mod (P-1)を求めて、M^r mod Pが答え。

tipstar0125 commented 11 months ago
tipstar0125 commented 10 months ago

べき乗のべき乗のmod

M^(K^N) mod P

フェルマーの小定理:M^(P-1) = 1 (mod P) より、K^NはP-1毎に1になるので、K^NをP-1で割った余りのみを考えればよい。