{"id":459319,"date":"2025-05-12T15:04:27","date_gmt":"2025-05-12T15:04:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=459319"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=459319","title":{"rendered":"<span>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 Leetcode \u043f\u0440\u043e \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <a href=\"https:\/\/habr.com\/ru\/articles\/764718\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u0438 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0431\u0438\u0442\u043e\u0432\u043e\u0439 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0438 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0441 LeetCode.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 50. Pow(x, n) \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u044f\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 <strong>x<\/strong> \u0432 \u0446\u0435\u043b\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c <strong>n<\/strong>. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445:<\/p>\n<p><img decoding=\"async\" class=\"formula\" source=\"-100.0 \\le x \\le 100.0 \\\\ -2^{31} \\le n \\le 2^{31}-1\" alt=\"-100.0 \\le x \\le 100.0 \\\\ -2^{31} \\le n \\le 2^{31}-1\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/741\/bbd\/10a\/741bbd10ac6062452dce16e59ca89be6.svg\" width=\"697\" height=\"48\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/741\/bbd\/10a\/741bbd10ac6062452dce16e59ca89be6.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/741\/bbd\/10a\/741bbd10ac6062452dce16e59ca89be6.svg 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u043d\u0430 \u0443\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u0433\u0434\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c <strong>n-1 <\/strong>\u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0437\u0430 O(n) \u0438 \u043f\u0440\u043e\u0441\u0442 \u0432 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0438. <\/p>\n<p>\u041d\u043e, \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c, \u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u043a\u0430\u043a-\u0442\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0435\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 <strong>n <\/strong>\u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0432\u0438\u0434\u0435, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0440\u0435\u0437\u0430\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e O(log n) <\/p>\n<p><img decoding=\"async\" class=\"formula\" source=\"a^n = a^{b_0 \\cdot 2^0 + b_1 \\cdot 2^1 + \\dots + b_k \\cdot 2^k} = \\prod_{i=0}^k a^{b_i \\cdot 2^i}\" alt=\"a^n = a^{b_0 \\cdot 2^0 + b_1 \\cdot 2^1 + \\dots + b_k \\cdot 2^k} = \\prod_{i=0}^k a^{b_i \\cdot 2^i}\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dce\/f11\/a61\/dcef11a61d9c524e3e194ff0d7de77e2.svg\" width=\"289\" height=\"61\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dce\/f11\/a61\/dcef11a61d9c524e3e194ff0d7de77e2.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dce\/f11\/a61\/dcef11a61d9c524e3e194ff0d7de77e2.svg 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">def my_pow(x: float, n: int) -&gt; float:     if n == 0:         return 1.0      # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438     if n &lt; 0:         x = 1 \/ x         n = -n      result = 1.0     current_product = x      while n &gt; 0:         if n &amp; 1:           # \u043d\u0430\u0448\u043b\u0438 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043e\u0442 1 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u0438           result *= current_product         current_product *= current_product                  n = n &gt;&gt; 1          return result<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0432\u043c\u0435\u0441\u0442\u043e <strong>n-1 <\/strong>\u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430\u0438\u0432\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439, \u0440\u0430\u0432\u043d\u043e\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u0447\u0438\u0441\u043b\u0430 <strong>n<\/strong>, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(log n) \u0438 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0443\u043c\u0435\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u043e\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0435\u0448\u0438\u043c \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443: 372. Super Pow \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u044f\u0442 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c <\/p>\n<p><img decoding=\"async\" class=\"formula\" source=\"a^{b} mod \\space 1337\" alt=\"a^{b} mod \\space 1337\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/97f\/edb\/4e7\/97fedb4e76576f0350dcae935665e319.svg\" width=\"99\" height=\"21\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/97f\/edb\/4e7\/97fedb4e76576f0350dcae935665e319.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/97f\/edb\/4e7\/97fedb4e76576f0350dcae935665e319.svg 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<p>\u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e <strong>b &#8212; <\/strong>\u044d\u043a\u0441\u0442\u0440\u0435\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435:<\/p>\n<p><img decoding=\"async\" class=\"formula\" source=\"1 \\le a \\le 2^{31} - 1 \\\\ 1 \\le b.lenght \\le 2000 \\\\ 0 \\le b[i] \\le 9 \\\\ b \\space \u043d\u0435 \\space\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \\space\u0432\u0435\u0434\u0443\u0449\u0438\u0445 \\space\u043d\u0443\u043b\u0435\u0439 \" alt=\"1 \\le a \\le 2^{31} - 1 \\\\ 1 \\le b.lenght \\le 2000 \\\\ 0 \\le b[i] \\le 9 \\\\ b \\space \u043d\u0435 \\space\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \\space\u0432\u0435\u0434\u0443\u0449\u0438\u0445 \\space\u043d\u0443\u043b\u0435\u0439 \" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a3c\/735\/a35\/a3c735a35043c46520088757a4737372.svg\" width=\"697\" height=\"110\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a3c\/735\/a35\/a3c735a35043c46520088757a4737372.svg 780w,&#10;       https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a3c\/735\/a35\/a3c735a35043c46520088757a4737372.svg 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e b &#8212; \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435, \u043d\u043e \u043d\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0442\u043e\u0442 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e\u0442 \u043d\u0430\u0441 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0442\u0432\u0435\u0442\u0430, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u0442\u0432\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1337<\/p>\n<p>\u041e\u043f\u044f\u0442\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0435\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c b \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0435\u0439 2. <\/p>\n<pre><code class=\"python\">class Solution:     def super_pow(self, a: int, b: list[int]) -&gt; int:         MOD = 1337          def pow_mod(base: int, exponent: int) -&gt; int:             result = 1             base = base % MOD             while exponent &gt; 0:                 if exponent &amp; 1:                     result = (result * base) % MOD                 base = (base * base) % MOD                 exponent = exponent &gt;&gt; 1             return result          result = 1         a = a % MOD            for digit in b:             result = (pow_mod(result, 10) * pow_mod(a, digit)) % MOD          return result <\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430 O(n log k), \u0433\u0434\u0435 n &#8212; \u0447\u0438\u0441\u043b\u043e \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u0447\u0438\u0441\u043b\u0430 b (\u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430), \u0430 k &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0446\u0438\u0444\u0440\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0442 0 \u0434\u043e 4. \u041e\u0442\u043c\u0435\u0442\u0438\u043c \u0442\u0430\u043a\u0436\u0435, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/851920\/\"> https:\/\/habr.com\/ru\/articles\/851920\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <a href=\"https:\/\/habr.com\/ru\/articles\/764718\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u0438 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0431\u0438\u0442\u043e\u0432\u043e\u0439 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u043a\u0438 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0441 LeetCode.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 50. Pow(x, n) \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u044f\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 <strong>x<\/strong> \u0432 \u0446\u0435\u043b\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c <strong>n<\/strong>. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445:<\/p>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u043d\u0430 \u0443\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u0433\u0434\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c <strong>n-1 <\/strong>\u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0437\u0430 O(n) \u0438 \u043f\u0440\u043e\u0441\u0442 \u0432 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0438. <\/p>\n<p>\u041d\u043e, \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c, \u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u043a\u0430\u043a-\u0442\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0435\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 <strong>n <\/strong>\u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u0432\u0438\u0434\u0435, \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u0440\u0435\u0437\u0430\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e O(log n) <\/p>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"python\">def my_pow(x: float, n: int) -&gt; float:     if n == 0:         return 1.0      # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438     if n &lt; 0:         x = 1 \/ x         n = -n      result = 1.0     current_product = x      while n &gt; 0:         if n &amp; 1:           # \u043d\u0430\u0448\u043b\u0438 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u043e\u0442 1 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \u0432 \u043d\u0430\u0448\u0435\u043c \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u0438           result *= current_product         current_product *= current_product                  n = n &gt;&gt; 1          return result<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435, \u0432\u043c\u0435\u0441\u0442\u043e <strong>n-1 <\/strong>\u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430\u0438\u0432\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439, \u0440\u0430\u0432\u043d\u043e\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u0447\u0438\u0441\u043b\u0430 <strong>n<\/strong>, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(log n) \u0438 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0443\u043c\u0435\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u043e \u0432\u043e\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0435\u0448\u0438\u043c \u043f\u043e\u0445\u043e\u0436\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443: 372. Super Pow \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u044f\u0442 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c <\/p>\n<p>\u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e <strong>b &#8212; <\/strong>\u044d\u043a\u0441\u0442\u0440\u0435\u043c\u0430\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435:<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e b &#8212; \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435, \u043d\u043e \u043d\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0442\u043e\u0442 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e\u0442 \u043d\u0430\u0441 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0442\u0432\u0435\u0442\u0430, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u0442\u0432\u0435\u0442 \u0431\u0443\u0434\u0435\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043e\u0442 0 \u0434\u043e 1337<\/p>\n<p>\u041e\u043f\u044f\u0442\u044c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0442\u0435\u043c \u0444\u0430\u043a\u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c b \u0432 \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0435\u0439 2. <\/p>\n<pre><code class=\"python\">class Solution:     def super_pow(self, a: int, b: list[int]) -&gt; int:         MOD = 1337          def pow_mod(base: int, exponent: int) -&gt; int:             result = 1             base = base % MOD             while exponent &gt; 0:                 if exponent &amp; 1:                     result = (result * base) % MOD                 base = (base * base) % MOD                 exponent = exponent &gt;&gt; 1             return result          result = 1         a = a % MOD            for digit in b:             result = (pow_mod(result, 10) * pow_mod(a, digit)) % MOD          return result <\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430 O(n log k), \u0433\u0434\u0435 n &#8212; \u0447\u0438\u0441\u043b\u043e \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u0447\u0438\u0441\u043b\u0430 b (\u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430), \u0430 k &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u0432 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0446\u0438\u0444\u0440\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0442 0 \u0434\u043e 4. \u041e\u0442\u043c\u0435\u0442\u0438\u043c \u0442\u0430\u043a\u0436\u0435, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/851920\/\"> https:\/\/habr.com\/ru\/articles\/851920\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-459319","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/459319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=459319"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/459319\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=459319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=459319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=459319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}