{"id":3680,"date":"2018-09-29T22:18:09","date_gmt":"2018-09-29T19:18:09","guid":{"rendered":"http:\/\/java.mazurok.com\/?p=3680"},"modified":"2018-11-19T21:29:41","modified_gmt":"2018-11-19T18:29:41","slug":"e-olymp-480-%d0%b2%d0%be%d0%b7%d0%b2%d0%b5%d0%b4%d0%b5%d0%bd%d0%b8%d0%b5-%d0%b2-%d1%81%d1%82%d0%b5%d0%bf%d0%b5%d0%bd%d1%8c-2","status":"publish","type":"post","link":"https:\/\/java.mazurok.com\/?p=3680","title":{"rendered":"e-olymp 480. \u0412\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c &#8212; 2"},"content":{"rendered":"<h1>\u0417\u0430\u0434\u0430\u0447\u0430<\/h1>\n<p>\u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 $A$, $B$ \u0438 $M$ \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c $A^B \\mod M$.<\/p>\n<h2><em>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/em><\/h2>\n<p>\u0412\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0434\u0430\u043d\u044b \u0442\u0440\u0438 \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 $A$, $B$, $M$ $(1 \u2264 A, \\, B \u2264 10^{18}, \\, 2 \u2264 M \u2264 2 \\cdot 10^9)$, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b.<\/p>\n<h2><em>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/em><\/h2>\n<p>\u0412 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043e\u0434\u043d\u043e \u0447\u0438\u0441\u043b\u043e, \u0440\u0430\u0432\u043d\u043e\u0435 $A^B \\mod M$.<\/p>\n<h2>\u0422\u0435\u0441\u0442\u044b<\/h2>\n<table>\n<tr>\n<td><strong>\u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong><\/td>\n<td><strong>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/strong><\/td>\n<\/tr>\n<tr>\n<td>\n$531$ $348$ $1645$\n<\/td>\n<td>$911$<\/td>\n<\/tr>\n<tr>\n<td>\n$1784353$ $453345$ $463973$\n<\/td>\n<td>$214457$<\/td>\n<\/tr>\n<tr>\n<td>\n$39252362$ $345673$ $786536$\n<\/td>\n<td>$302328$<\/td>\n<\/tr>\n<tr>\n<td>\n$68790234$ $679643$ $789057$\n<\/td>\n<td>$281232$<\/td>\n<\/tr>\n<tr>\n<td>\n$324$ $8564$ $45074547$\n<\/td>\n<td>$32984424$<\/td>\n<\/tr>\n<\/table>\n<h2>\u041a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/h2>\n<pre class=\"lang:java decode:true \" title=\"e-olymp-480. Solution\">import java.util.Scanner;\r\n\r\npublic class Main {\r\n    public static long binPow(long a, long b, int m) {\r\n        a %= m;\r\n        if (b == 0) return 1;\r\n        else if (b % 2 == 0) {\r\n            return binPow((a * a) % m, b \/ 2, m);\r\n        }\r\n        else return (a * binPow(a, b - 1, m)) % m;\r\n    }\r\n    public static void main(String[] args) {\r\n        Scanner scanner = new Scanner(System.in);\r\n        long a = scanner.nextLong();\r\n        long b = scanner.nextLong();\r\n        int m = scanner.nextInt();\r\n        System.out.println(binPow(a, b, m));\r\n    }\r\n}<\/pre>\n<h2>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438<\/h2>\n<p>\u041f\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e:<br \/>\n$$C \\equiv C \\mod K \\pmod K$$<br \/>\n$$CD \\equiv (C \\mod K) \\cdot (D \\mod K) \\pmod K$$<br \/>\n$$C \\equiv D \\pmod K \\Rightarrow C^n \\equiv D^n \\pmod K$$<br \/>\n\u041e\u0442\u0441\u044e\u0434\u0430 \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u0443\u044e \u0444\u043e\u0440\u043c\u0443\u043b\u0443 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e:<br \/>\n$$<br \/>\nA^B \\mod M =<br \/>\n\\begin{cases}<br \/>\n1 \\text{ \u043f\u0440\u0438 } B = 0\\\\\\<br \/>\n\\left ( \\left (A \\mod M \\right ) \\left ( (A \\mod M)^{B-1} \\mod M \\right )\\right )\\mod M \\\\\\\\ \\text{ \u043f\u0440\u0438 } B \\equiv 1 \\pmod 2\\\\\\<br \/>\n\\left ( \\left (A \\mod M \\right)^2 \\right)^{\\frac{B}{2}} \\mod M \\text{ \u043f\u0440\u0438 } B \\equiv 0 \\pmod 2 \\wedge  B \\neq 0<br \/>\n\\end{cases}<br \/>\n$$<\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p><a href=\"https:\/\/www.e-olymp.com\/ru\/problems\/480\" rel=\"noopener\" target=\"_blank\">\u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 e-olymp<\/a><br \/>\n<a href=\"https:\/\/www.e-olymp.com\/ru\/submissions\/4485483\" rel=\"noopener\" target=\"_blank\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 e-olymp<\/a><br \/>\n<a href=\"https:\/\/ideone.com\/Pp2jKw\" rel=\"noopener\" target=\"_blank\">\u041a\u043e\u0434 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 Ideone<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0417\u0430\u0434\u0430\u0447\u0430 \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 $A$, $B$ \u0438 $M$ \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c $A^B \\mod M$. \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0412\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0434\u0430\u043d\u044b \u0442\u0440\u0438 \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u043b\u0430 $A$, $B$, $M$ $(1 \u2264 A, \\, B \u2264 10^{18}, \\, 2 \u2264 M \u2264 2 \\cdot 10^9)$, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b. \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0412 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0444\u0430\u0439\u043b \u0432\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043e\u0434\u043d\u043e \u0447\u0438\u0441\u043b\u043e, \u0440\u0430\u0432\u043d\u043e\u0435 $A^B \\mod &hellip; <a href=\"https:\/\/java.mazurok.com\/?p=3680\" class=\"more-link\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":119,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[391,392,394,393],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/3680"}],"collection":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/users\/119"}],"replies":[{"embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3680"}],"version-history":[{"count":1,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/3680\/revisions"}],"predecessor-version":[{"id":3681,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=\/wp\/v2\/posts\/3680\/revisions\/3681"}],"wp:attachment":[{"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.mazurok.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}