{"id":172757,"date":"2022-04-22T10:24:03","date_gmt":"2022-04-22T02:24:03","guid":{"rendered":"http:\/\/www.idc.net\/help\/172757\/"},"modified":"2022-04-22T10:24:03","modified_gmt":"2022-04-22T02:24:03","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e4%b8%adpow%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%86%85%e7%bd%ae%e8%b0%83%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/172757\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u4e2dpow\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u8c03\u7528\uff1f"},"content":{"rendered":"<p style=\"text-align: center\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/image\/203\/719\/567\/1607334159950246.png\" class=\"aligncenter\"><\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 16px\">\u6211\u4eec\u5728\u7528\u51fd\u6570\u8c03\u7528\u7684\u65f6\u5019\u4e00\u822c\u662f\u8981\u8003\u8651\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5176\u6b21\u5c31\u662f\u53c2\u6570\u7684\u95ee\u9898\u3002\u76f8\u4fe1\u7ecf\u8fc7\u4e4b\u524d\u51e0\u7bc7\u53c2\u6570\u6587\u7ae0\u7684\u8bb2\u89e3\uff0c\u5927\u90e8\u5206\u5c0f\u4f19\u4f34\u5df2\u7ecf\u5bf9\u53c2\u6570\u4e0d\u964c\u751f\u4e86\u3002\u90a3\u4e48\u4eca\u5929\u6211\u4eec\u5c31\u6765\u8bb2\u8bb2\u5e38\u89c1\u8fd4\u56de\u503c\u7684\u95ee\u9898\u3002\u8fd9\u91cc\u4ee5pow\u51fd\u6570\u7684\u8c03\u7528\u4e3a\u4f8b\uff0c\u5728\u5b66\u4e60\u4f7f\u7528\u65b9\u6cd5\u7684\u540c\u65f6\uff0c\u6211\u4eec\u4f1a\u8bd5\u7740\u627e\u5bfb\u5185\u7f6e\u8c03\u7528\u8fd4\u56de\u503c\u5f97\u7ed3\u679c\u4ee5\u53ca\u8fdb\u884c\u7ed3\u679c\u5206\u6790\u3002<\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\">pow() \u65b9\u6cd5\u8fd4\u56de xy\uff08x \u7684 y \u6b21\u65b9\uff09 \u7684\u503c\u3002<\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\">math \u6a21\u5757 pow() \u65b9\u6cd5\u7684\u8bed\u6cd5:<\/span><\/p>\n<pre>import&nbsp;math\nmath.pow(x,&nbsp;y)<\/pre>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\"><\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\">\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 10.5pt\">\u5185\u7f6e\u7684<\/span> pow() \u65b9\u6cd5:<\/span><\/p>\n<pre>pow(x,&nbsp;y[,&nbsp;z])<\/pre>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;font-size: 10.5pt\">\u51fd\u6570\u662f\u8ba1\u7b97<\/span> x \u7684 y \u6b21\u65b9\uff0c\u5982\u679c z \u5728\u5b58\u5728\uff0c\u5219\u518d\u5bf9\u7ed3\u679c\u8fdb\u884c\u53d6\u6a21\uff0c\u5176\u7ed3\u679c\u7b49\u6548\u4e8e pow(x,y) %z\u3002<\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\"><strong><span style=\"font-size: 14px\"><span style=\"font-size: 14px\">\u6ce8\u610f\uff1a<\/span>pow() \u901a\u8fc7\u5185\u7f6e\u7684\u65b9\u6cd5\u76f4\u63a5\u8c03\u7528\uff0c\u5185\u7f6e\u65b9\u6cd5\u4f1a\u628a\u53c2\u6570\u4f5c\u4e3a\u6574\u578b\uff0c\u800c math \u6a21\u5757\u5219\u4f1a\u628a\u53c2\u6570\u8f6c\u6362\u4e3a float\u3002<\/span><\/strong><\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\">&nbsp;<\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 14px\">\u4e3e\u4f8b\uff1a<\/span><\/p>\n<pre>#&nbsp;pow(base,&nbsp;exp[,&nbsp;mod])\n&nbsp;\n#&nbsp;pow&nbsp;power&nbsp;exp&nbsp;exponent&nbsp;mod&nbsp;module\n&nbsp;\n#&nbsp;\u8fd4\u56de&nbsp;base&nbsp;\u7684&nbsp;exp&nbsp;\u6b21\u5e42\uff1b\u5982\u679c&nbsp;mod&nbsp;\u5b58\u5728\uff0c\u5219\u8fd4\u56de&nbsp;base&nbsp;\u7684&nbsp;exp&nbsp;\u6b21\u5e42\u5bf9&nbsp;mod&nbsp;\u53d6\u4f59\uff08\u6bd4&nbsp;pow(base,&nbsp;exp)&nbsp;%&nbsp;mod&nbsp;\u66f4\u9ad8\u6548\uff09\u3002&nbsp;\u4e24\u53c2\u6570\u5f62\u5f0f&nbsp;pow(base,&nbsp;exp)&nbsp;\u7b49\u4ef7\u4e8e\u4e58\u65b9\u8fd0\u7b97\u7b26:&nbsp;base**exp\u3002\n&nbsp;\n#&nbsp;\u8fd4\u56de&nbsp;base&nbsp;\u7684&nbsp;exp&nbsp;\u6b21\u5e42\uff1b\nprint(f'{&nbsp;pow(2,&nbsp;3)&nbsp;=&nbsp;}')\nprint(f'{&nbsp;pow(10,&nbsp;2)&nbsp;=&nbsp;}')\n#&nbsp;\u4e24\u53c2\u6570\u5f62\u5f0f&nbsp;pow(base,&nbsp;exp)&nbsp;\u7b49\u4ef7\u4e8e\u4e58\u65b9\u8fd0\u7b97\u7b26:&nbsp;base**exp\u3002\nprint(f'{&nbsp;2**3&nbsp;=&nbsp;}')\nprint(f'{&nbsp;10**2&nbsp;=&nbsp;}')\n#&nbsp;\u5982\u679c&nbsp;mod&nbsp;\u5b58\u5728\uff0c\u5219\u8fd4\u56de&nbsp;base&nbsp;\u7684&nbsp;exp&nbsp;\u6b21\u5e42\u5bf9&nbsp;mod&nbsp;\u53d6\u4f59\uff08\u6bd4&nbsp;pow(base,&nbsp;exp)&nbsp;%&nbsp;mod&nbsp;\u66f4\u9ad8\u6548\uff09\n#&nbsp;\u5982\u679cmod\u5b58\u5728\uff0c\u4f1a\u4f7f\u7528\u5feb\u901f\u5e42\u53d6\u6a21\u7684\u7b97\u6cd5\u6765\u53d6\u6a21\nimport&nbsp;time\nstart&nbsp;=&nbsp;time.time()\nprint(f'{&nbsp;pow(1234,&nbsp;1234567,&nbsp;67)&nbsp;=&nbsp;}')\nend&nbsp;=&nbsp;time.time()\nprint(f'&nbsp;\u8017\u65f6\uff1a{&nbsp;end&nbsp;-&nbsp;start&nbsp;=&nbsp;}&nbsp;')\n#&nbsp;start&nbsp;=&nbsp;time.time()\n#&nbsp;print(f'{&nbsp;pow(1234,&nbsp;1234567)&nbsp;%&nbsp;67&nbsp;=&nbsp;}')\n#&nbsp;end&nbsp;=&nbsp;time.time()\n#&nbsp;print(f'&nbsp;\u8017\u65f6\uff1a{&nbsp;end&nbsp;-&nbsp;start&nbsp;=&nbsp;}&nbsp;')\n#\n#&nbsp;\u53c2\u6570\u5fc5\u987b\u5177\u6709\u6570\u503c\u7c7b\u578b\u3002&nbsp;\u5bf9\u4e8e\u6df7\u7528\u7684\u64cd\u4f5c\u6570\u7c7b\u578b\uff0c\u5219\u5c06\u5e94\u7528\u53cc\u76ee\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\u89c4\u5219\u3002&nbsp;\u5bf9\u4e8e&nbsp;int&nbsp;\u64cd\u4f5c\u6570\uff0c\u7ed3\u679c\u5177\u6709\u4e0e\u64cd\u4f5c\u6570\u76f8\u540c\u7684\u7c7b\u578b\uff08\u5f3a\u5236\u8f6c\u6362\u540e\uff09\uff0c\u9664\u975e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8d1f\u503c\uff1b\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u53c2\u6570\u5c06\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u5e76\u8f93\u51fa\u6d6e\u70b9\u6570\u7ed3\u679c\u3002&nbsp;\u4f8b\u5982\uff0c10**2&nbsp;\u8fd4\u56de&nbsp;100\uff0c\u4f46&nbsp;10**-2&nbsp;\u8fd4\u56de&nbsp;0.01\u3002\n&nbsp;\n#&nbsp;\u53c2\u6570\u5fc5\u987b\u5177\u6709\u6570\u503c\u7c7b\u578b\n#&nbsp;print(f'{&nbsp;pow(2,&nbsp;\"3\")&nbsp;=&nbsp;}')\n#&nbsp;\u5bf9\u4e8e\u6df7\u7528\u7684\u64cd\u4f5c\u6570\u7c7b\u578b\uff0c\u5219\u5c06\u5e94\u7528\u53cc\u76ee\u7b97\u672f\u8fd0\u7b97\u7b26\u7684\u7c7b\u578b\u5f3a\u5236\u8f6c\u6362\u89c4\u5219\u3002\nprint(f'{&nbsp;pow(2.0,&nbsp;3)&nbsp;=&nbsp;}')\n#&nbsp;\u5bf9\u4e8e&nbsp;int&nbsp;\u64cd\u4f5c\u6570\uff0c\u7ed3\u679c\u5177\u6709\u4e0e\u64cd\u4f5c\u6570\u76f8\u540c\u7684\u7c7b\u578b\uff08\u5f3a\u5236\u8f6c\u6362\u540e\uff09\uff0c\u9664\u975e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8d1f\u503c\uff1b\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u53c2\u6570\u5c06\u88ab\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u5e76\u8f93\u51fa\u6d6e\u70b9\u6570\u7ed3\u679c\u3002\nprint(f'{&nbsp;pow(2,&nbsp;3)&nbsp;=&nbsp;}')\nprint(f'{&nbsp;pow(2,&nbsp;-3)&nbsp;=&nbsp;}')\n#\n#&nbsp;\u5bf9\u4e8e&nbsp;int&nbsp;\u64cd\u4f5c\u6570&nbsp;base&nbsp;\u548c&nbsp;exp\uff0c\u5982\u679c\u7ed9\u51fa&nbsp;mod\uff0c\u5219&nbsp;mod&nbsp;\u5fc5\u987b\u4e3a\u6574\u6570\u7c7b\u578b\u5e76\u4e14&nbsp;mod&nbsp;\u5fc5\u987b\u4e0d\u4e3a\u96f6\u3002&nbsp;\u5982\u679c\u7ed9\u51fa&nbsp;mod&nbsp;\u5e76\u4e14&nbsp;exp&nbsp;\u4e3a\u8d1f\u503c\uff0c\u5219&nbsp;base&nbsp;\u5fc5\u987b\u76f8\u5bf9\u4e8e&nbsp;mod&nbsp;\u4e0d\u53ef\u6574\u9664\u3002&nbsp;\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c06\u4f1a\u8fd4\u56de&nbsp;pow(inv_base,&nbsp;-exp,&nbsp;mod)\uff0c\u5176\u4e2d&nbsp;inv_base&nbsp;\u4e3a&nbsp;base&nbsp;\u7684\u5012\u6570\u5bf9&nbsp;mod&nbsp;\u53d6\u4f59\u3002\n#\n&nbsp;\n#&nbsp;\u5bf9\u4e8e&nbsp;int&nbsp;\u64cd\u4f5c\u6570&nbsp;base&nbsp;\u548c&nbsp;exp\uff0c\u5982\u679c\u7ed9\u51fa&nbsp;mod\uff0c\u5219&nbsp;mod&nbsp;\u5fc5\u987b\u4e3a\u6574\u6570\u7c7b\u578b\u5e76\u4e14&nbsp;mod&nbsp;\u5fc5\u987b\u4e0d\u4e3a\u96f6\u3002\n#&nbsp;print(f'{&nbsp;pow(2,&nbsp;3,&nbsp;0)&nbsp;=&nbsp;}')\n&nbsp;\n#&nbsp;\u4e0b\u9762\u7684\u4f8b\u5b50\u662f&nbsp;38&nbsp;\u7684\u5012\u6570\u5bf9&nbsp;97&nbsp;\u53d6\u4f59:\n#\n#&nbsp;&gt;&gt;&gt;\n#&nbsp;&gt;&gt;&gt;&nbsp;pow(38,&nbsp;-1,&nbsp;mod=97)\n#&nbsp;23\n#&nbsp;&gt;&gt;&gt;&nbsp;23&nbsp;*&nbsp;38&nbsp;%&nbsp;97&nbsp;==&nbsp;1\n#&nbsp;True\n#&nbsp;\u5728&nbsp;3.8&nbsp;\u7248\u66f4\u6539:&nbsp;\u5bf9\u4e8e&nbsp;int&nbsp;\u64cd\u4f5c\u6570\uff0c\u4e09\u53c2\u6570\u5f62\u5f0f\u7684&nbsp;pow&nbsp;\u73b0\u5728\u5141\u8bb8\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u8d1f\u503c\uff0c\u5373\u53ef\u4ee5\u8ba1\u7b97\u5012\u6570\u7684\u4f59\u6570\u3002\n#\n#&nbsp;\u5728&nbsp;3.8&nbsp;\u7248\u66f4\u6539:&nbsp;\u5141\u8bb8\u5173\u952e\u5b57\u53c2\u6570\u3002&nbsp;\u4e4b\u524d\u53ea\u652f\u6301\u4f4d\u7f6e\u53c2\u6570\u3002<\/pre>\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 16px\"><\/span><\/p>\n<p style=\"margin: 0pt;line-height: 1.75em\">\n<p style=\"margin: 0pt;line-height: 1.75em\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1,Microsoft YaHei;font-size: 16px\">\u4ee5\u4e0a\u5c31\u662fpython\u4e2dpow\u51fd\u6570\u4f7f\u7528\u5185\u7f6e\u8c03\u7528\u7684\u65b9\u6cd5\uff0c\u6700\u540e\u5bf9\u4e8e\u53c2\u6570\u7684\u8f93\u51fa\u548cmath\u6709\u6240\u4e0d\u540c\uff0c\u8fd9\u70b9\u9700\u8981\u5f15\u8d77\u5927\u5bb6\u7684\u6ce8\u610f\u3002<\/span><\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u5728\u7528\u51fd\u6570\u8c03\u7528\u7684\u65f6\u5019\u4e00\u822c\u662f\u8981\u8003\u8651\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5176\u6b21\u5c31\u662f\u53c2\u6570\u7684\u95ee\u9898\u3002\u76f8\u4fe1\u7ecf\u8fc7\u4e4b\u524d\u51e0\u7bc7\u53c2\u6570\u6587\u7ae0\u7684\u8bb2\u89e3\uff0c\u5927\u90e8\u5206\u5c0f\u4f19 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":172758,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-172757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/172757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=172757"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/172757\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/172758"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=172757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=172757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=172757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}