{"id":425,"date":"2023-01-02T00:43:43","date_gmt":"2023-01-01T16:43:43","guid":{"rendered":"https:\/\/blog.cirno.fun\/?p=425"},"modified":"2024-09-12T09:30:17","modified_gmt":"2024-09-12T01:30:17","slug":"%e6%89%a9%e5%b1%95%e6%ac%a7%e5%87%a0%e9%87%8c%e5%be%97%e7%ae%97%e6%b3%95%e6%b1%82%e4%b9%98%e6%b3%95%e9%80%86%e5%85%83","status":"publish","type":"post","link":"https:\/\/blog.sssn.tech\/?p=425","title":{"rendered":"\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u6c42\u4e58\u6cd5\u9006\u5143"},"content":{"rendered":"<p>\u672c\u8282\u9700\u8981\u524d\u7f6e\u77e5\u8bc6\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u548c\u4e58\u6cd5\u9006\u5143<\/p>\n<p>\u5728\u524d\u9762\u8bb2\u5230\u4e58\u6cd5\u9006\u5143\u4e2d\uff0c\u6211\u4eec\u63a8\u5bfc\u51fa\u4e86\u9006\u5143\u7684\u4e00\u4e2a\u6bd4\u8f83\u7b80\u4ecb\u7684\u8868\u793a\u6cd5<\/p>\n<pre><code class=\"language-katex\">a\\cdot a^{-1} \\equiv 1 (mod \\quad p)<\/code><\/pre>\n<p>\u8fd9\u91cc\u7684<code>a^{-1}<\/code>\u53eb\u505aa mod p\u7684\u4e58\u6cd5\u9006\u5143<\/p>\n<p>\u4e0a\u5f0f\u4e0d\u96be\u5199\u6210<\/p>\n<pre><code class=\"language-katex\">aa^{-1}=kp+1,k \\in \\mathbb{Z}<\/code><\/pre>\n<p>\u79fb\u9879\u53ef\u4ee5\u5f97\u5230<\/p>\n<pre><code class=\"language-katex\">aa^{-1} - kp = 1<\/code><\/pre>\n<p>\u4e0a\u5f0f\u7684\u5f62\u5f0f\u975e\u5e38\u50cf\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u6240\u6c42\u7684\u65b9\u7a0b<\/p>\n<pre><code class=\"language-katex\">ax+by=gcd(a, b)<\/code><\/pre>\n<p>\u800c\u6211\u4eec\u77e5\u9053a mod p\u7684\u4e58\u6cd5\u9006\u5143\u5b58\u5728\u7684\u5145\u8981\u6761\u4ef6\u662fa\u4e0ep\u4e92\u8d28\uff0c\u5373gcd(a,p)=1 \u6240\u4ee5\u6211\u4eec\u4ee4\u4e0a\u5f0f\u4e2da\u9006\u4e3ax\uff0c-k\u4e3ay\uff0c\u8fd0\u7528\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u89e3\u65b9\u7a0b<\/p>\n<pre><code class=\"language-katex\">ax+ky=1<\/code><\/pre>\n<p>\u5373\u53ef\u6c42\u5f97x\u4e3aa mod p\u7684\u4e58\u6cd5\u9006\u5143<\/p>\n<p><strong>\u6b64\u65b9\u6cd5\u6c42\u9006\u5143\u6ca1\u6709\u5feb\u901f\u5e42\u6c42\u9006\u5143\u4e2dp\u5fc5\u987b\u4e3a\u8d28\u6570\u7684\u6761\u4ef6\uff0c\u6240\u4ee5\u662f\u66f4\u901a\u7528\u7684\u65b9\u6cd5<\/strong><\/p>\n<pre><code class=\"language-cpp\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n\n\/\/ ax + by = gcd(a, b)\nll exgcd(ll a, ll b, ll &amp;x, ll &amp;y)\n{\n    if(!b)\n    {\n        x = 1; y = 0;\n        return a;\n    }\n    ll d = exgcd(b, a % b, y, x);\n    y -= (a \/ b) * x;\n    return d;\n}\n\nint main()\n{\n    \/\/ ios::sync_with_stdio(false);\n    int n;\n    scanf(&quot;%d&quot;, &amp;n);\n    while (n--)\n    {\n        ll a, p;\/\/\u6c42a\u6a21p\u7684\u4e58\u6cd5\u9006\u5143\n        scanf(&quot;%lld %lld&quot;, &amp;a, &amp;p);\n        ll x, y;\n        ll d = exgcd(a, p, x, y);\n        if(d == 1)\n        {\n            printf(&quot;%lld\\n&quot;, x);\n        }else{\n            puts(&quot;impossible&quot;);\n        }\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u8282\u9700\u8981\u524d\u7f6e\u77e5\u8bc6\u6269\u5c55\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u548c\u4e58\u6cd5\u9006\u5143 \u5728\u524d\u9762\u8bb2\u5230\u4e58\u6cd5\u9006\u5143\u4e2d\uff0c\u6211\u4eec\u63a8\u5bfc\u51fa\u4e86\u9006 [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,16],"tags":[],"class_list":["post-425","post","type-post","status-publish","format-standard","hentry","category-13","category-16"],"_links":{"self":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=425"}],"version-history":[{"count":1,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}