{"id":431,"date":"2023-01-15T01:44:39","date_gmt":"2023-01-14T17:44:39","guid":{"rendered":"https:\/\/blog.cirno.fun\/?p=431"},"modified":"2024-09-12T09:29:57","modified_gmt":"2024-09-12T01:29:57","slug":"%e7%bb%84%e5%90%88%e6%95%b0%e7%9a%84%e6%b1%82%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.sssn.tech\/?p=431","title":{"rendered":"\u7ec4\u5408\u6570\u7684\u4e09\u79cd\u5e38\u89c4\u7cbe\u5ea6\u6c42\u6cd5"},"content":{"rendered":"<p>\u672c\u8282\u4ecb\u7ecd\u7ec4\u5408\u6570\u7684\u4e09\u79cd\u5e38\u89c4\u7cbe\u5ea6\u6c42\u6cd5\uff0c\u7528\u4ee5\u5e94\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e<\/p>\n<h3>\u770b\u4e00\u4e0b\u7b2c\u4e00\u79cd\u6570\u636e\u7c7b\u578b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/blog.cirno.fun\/wp-content\/uploads\/2023\/01\/image-1673716375011.png\" alt=\"file\" \/><\/p>\n<p>\u5206\u6790\u6570\u636e\u8303\u56f4\u53d1\u73b0\u7279\u70b9\u662fab\u90fd\u8f83\u5c0f\uff0c\u95ee\u8be2\u6570\u8f83\u591a \u8fd9\u79cd\u6570\u636e\u6bd4\u8f83\u9002\u5408\u7528DP\u6253\u8868\u9884\u5904\u7406\u51fa\u6240\u6709\u7684Cab\uff0c\u7136\u540e\u6839\u636e\u95ee\u8be2\u8f93\u51fa<\/p>\n<p>\u9012\u63a8\u4f7f\u7528\u516c\u5f0f<\/p>\n<pre><code class=\"language-katex\">{n \\choose k } = {n - 1 \\choose k - 1} + {n - 1 \\choose k}<\/code><\/pre>\n<p>\u4e00\u4e2a\u611f\u6027\u597d\u7528\u7684\u7406\u89e3\uff1a \u5bf9\u4e8e\u8981\u4ecen\u4e2a\u82f9\u679c\u91cc\u9009\u51fak\u4e2a\u7684\u9009\u6cd5\uff0c\u7531\u4e24\u79cd\u60c5\u51b5\u6784\u6210\uff1a\u5bf9\u4e8e\u4efb\u610f\u4e00\u4e2a\u82f9\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\uff0c\u7136\u540e\u4ecen &#8211; 1\u4e2a\u82f9\u679c\u91cc\u9009k &#8211; 1\u4e2a\uff0c\u6216\u8005\u4e0d\u9009\uff0c\u5373\u4ece n &#8211; 1\u4e2a\u82f9\u679c\u91cc\u9009k\u4e2a<\/p>\n<p>\u76f4\u63a5DP\uff0c\u521d\u59cb\u5316\u51fa\u8fb9\u754c\u4e3a1\u7684\u60c5\u51b5\u7136\u540e\u63a8\u51fa\u4e00\u4e2a\u4e09\u89d2\u5f62\u533a\u57df\u5373\u53ef \u5982\u679c\u4e0d\u7406\u89e3\u7528Excel\u753b\u4e00\u4e0b\u4f1a\u5f88\u6709\u5e2e\u52a9 <img decoding=\"async\" src=\"https:\/\/blog.cirno.fun\/wp-content\/uploads\/2023\/01\/image-1673717420903.png\" alt=\"file\" \/><\/p>\n<pre><code class=\"language-cpp\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n\nconst int N = 2010, MOD = 1e9 + 7;\nint c[N][N];\nvoid init()\n{\n    \/\/ C[a][b] = C[a - 1][b - 1] + C[a][b - 1];\n    for(int i = 0; i &lt; N; i++)\n        c[0][i] = 1;\n    for(int i = 1; i &lt; N; i++)\n        for(int j = i; j &lt; N; j++)\n            c[i][j] = (c[i - 1][j - 1] + c[i][j - 1]) % MOD;\n}\n\nint main()\n{\n    \/\/ ios::sync_with_stdio(false);\n    int n;\n    scanf(&quot;%d&quot;, &amp;n);\n\n    init();\n\n    while(n--)\n    {\n        int a, b;\n        scanf(&quot;%d %d&quot;, &amp;a, &amp;b);\n        printf(&quot;%d\\n&quot;, c[b][a]);\n    }\n    return 0;\n}<\/code><\/pre>\n<p>\u9700\u8981\u7279\u522b\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u6211\u5728\u5199Cab\u65f6\uff0cab\u7684\u987a\u5e8f\u6bd4\u8f83\u968f\u610f\uff0c\u770b\u4ee3\u7801\u65f6\u8bf7\u7559\u610f\u4e00\u4e0b<\/p>\n<h3>\u770b\u4e00\u4e0b\u7b2c\u4e8c\u79cd\u6570\u636e\u7c7b\u578b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/blog.cirno.fun\/wp-content\/uploads\/2023\/01\/image-1673775296122.png\" alt=\"file\" \/><\/p>\n<p>\u663e\u7136\u65e0\u6cd5\u5904\u7406\u51fa\u6240\u6709Cab\u6765\u6253\u8868\u89e3\u51b3\u95ee\u9898<\/p>\n<p>\u6b64\u65f6\u53ef\u4ee5\u8003\u8651\u4ece\u516c\u5f0f\u65b9\u9762\u89e3\u51b3<\/p>\n<pre><code class=\"language-katex\">{a \\choose b} = \\frac{a!}{b!(a-b)!} <\/code><\/pre>\n<p>\u800c\u9898\u76ee\u8981\u6c42\u7ed3\u679c\u5bf91e9+7\u53d6\u6a21\uff0c\u53ef\u4ee5\u8003\u8651\u6253\u8868\u6d3b\u7684\u8303\u56f4\u5185ab\u7684\u9636\u4e58\u548cab mod p\u7684\u9006\uff0c\u4ee5\u6b64\u6765\u89e3\u51b3\u95ee\u9898<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u672c\u9898\u6a21\u6570p\u662f\u8d28\u6570\uff0c\u6240\u4ee5\u6c42\u9006\u9996\u9009\u5feb\u901f\u5e42\uff08\u8d39\u9a6c\u5c0f\u5b9a\u7406\uff09\u6cd5\uff0c\u4e0d\u4e86\u89e3\u4e58\u6cd5\u9006\u5143\u548c\u8d39\u9a6c\u5c0f\u5b9a\u7406\u7684\u53ef\u4ee5\u770b<a href=\"https:\/\/blog.cirno.fun\/?p=422\" title=\"\u6211\u8fd9\u7bc7\u6587\u7ae0\">\u6211\u8fd9\u7bc7\u6587\u7ae0<\/a><\/p>\n<pre><code class=\"language-cpp\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n\nconst ll MOD = 1e9 + 7;\nconst int N = 1e5 + 10;\n\nll fact[N], infact[N]; \/\/ \u5206\u522b\u50a8\u5b58n\u7684\u9636\u4e58\u548cn\u7684\u9636\u4e58mod MOD\u7684\u9006\n\nll qmi(ll x, ll q, ll m)\n{\n    \/\/ x ^ q % m\uff0c \u5feb\u901f\u5e42\u6a21\u677f\n    ll res = 1;\n    while (q)\n    {\n        if (q &amp; 1)\n            res = (res * x % m) % m;\n        q &gt;&gt;= 1;\n        x = (x * x % m) % m;\n    }\n    return res;\n}\n\nvoid init()\n{\n    \/\/ \u51fa\u59cb\u5316\u51fan\u7684\u9636\u4e58\u548cn\u7684\u9636\u4e58mod MOD\u7684\u9006\n    \/\/ 1 &lt;= n &lt;= 1e5\n    ll x = 1;\n    fact[0] = 1, infact[0] = 1;\n    for (int i = 1; i &lt;= (int)1e5; i++)\n    {\n        x = (x * i) % MOD;\n        fact[i] = x;\n        infact[i] = qmi(x, MOD - 2, MOD);\n    }\n}\n\nll getC(ll a, ll b)\n{\n    \/\/ C(b, a) = a! \/ (b! (a- b)!)\n    return ((fact[a] * infact[b] % MOD) * infact[a - b] % MOD) % MOD;\n}\n\nint main()\n{\n    \/\/ios::sync_with_stdio(false);\n    init();\n    int n;\n    scanf(&quot;%d&quot;, &amp;n);\n    while (n--)\n    {\n        ll a, b;\n        scanf(&quot;%lld %lld&quot;, &amp;a, &amp;b);\n        printf(&quot;%lld\\n&quot;, getC(a, b));\n    }\n    return 0;\n}<\/code><\/pre>\n<h3>\u6765\u770b\u7b2c\u4e09\u79cd\u6570\u636e\u7c7b\u578b<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/blog.cirno.fun\/wp-content\/uploads\/2023\/01\/image-1673775711755.png\" alt=\"file\" \/>\u8fd9\u79cd\u6570\u636e\u7c7b\u578b\u7684\u7279\u70b9\u662f\u6a21\u6570\u5f88\u5c0f\uff0cab\u90fd\u5f88\u5927 \u6b64\u65f6\u9700\u8981\u7528\u5230Lucas\u5b9a\u7406\u6765\u5bf9\u6570\u636e\u7684\u8303\u56f4\u8fdb\u884c\u5904\u7406 Lucas\u5b9a\u7406\/\u5362\u5361\u65af\u5b9a\u7406:<\/p>\n<pre><code class=\"language-katex\">{a \\choose b} \\equiv {a\\%p \\choose b\\%p} \\cdot {{\\left \\lfloor \\frac{a}{p}  \\right \\rfloor }  \\choose{{\\left \\lfloor \\frac{b}{p}  \\right \\rfloor }}} \\quad (mod \\quad p)<\/code><\/pre>\n<p>\u8bc1\u660e\u8fd9\u91cc\u4e0d\u5173\u5fc3\uff0c\u6709\u5174\u8da3\u53ef\u4ee5b\u7ad9\u641c\u7d22\u4e00\u4e0b\u76f8\u5173\u8bc1\u6cd5<\/p>\n<p>\u5229\u7528Lucas\u5b9a\u7406\u53ef\u4ee5\u5c06\u95ee\u9898\u89c4\u6a21\u53d8\u5c0f\uff0c\u65b9\u4fbf\u89e3\u9898 \u5728\u5199\u4ee3\u7801\u65f6\u5019\u8981\u7279\u522b\u6ce8\u610f\u53d6\u6a21\u95ee\u9898\uff0c\u56e0\u4e3a\u6570\u636e\u5f88\u5927\uff0c\u76f8\u4e58\u65f6\u5019\u975e\u5e38\u5bb9\u6613\u6ea2\u51fa<\/p>\n<pre><code class=\"language-cpp\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\ntypedef long long ll;\n\nll qmi(ll a, ll k, ll p)\n{\n    \/\/ a ^ p % q\uff0c \u5feb\u901f\u5e42\u6a21\u677f\n    ll res = 1;\n    while (k)\n    {\n        if (k &amp; 1)\n            res = (res * a % p) % p;\n        k &gt;&gt;= 1;\n        a = (a * a % p) % p;\n    }\n    return res;\n}\n\nll C(ll a, ll b, ll p)\n{\n    \/\/ C(a, b) = a! \/ (b!(a - b)!) = (a * (a - 1) * (a - 2) * ... * (a - b + 1)) \/ b!\n    if(b &gt; a)    return 0; \/\/\u8fd9\u4e00\u884c\u7279\u5224\u52a0\u4e0a\uff0c \u4e0b\u4e00\u884c\u5176\u5b9e\u53ef\u6709\u53ef\u65e0\n    if(b == a)  return 1;\n    ll res = 1;\n    \/\/ \u5faa\u73af\u770b\u4e0d\u61c2\u7684\u8bdd\u591a\u770b\u770b\u4e0a\u9762\u8fd9\u6bb5\u516c\u5f0f\uff0c \u5176\u5b9e\u5c31\u662f\u624b\u7b97\u65f6\u5019\u7684\u65b9\u6cd5\uff0c \u4e0d\u8981\u88ab\u5b9a\u4e49\u641e\u4e86\n    for (int i = 1, j = a; i &lt;= b; i++, j--)\n    {\n        res = (res * j) % p;\n        res = (res * qmi(i, p - 2, p)) % p;\n    }\n    return res;\n}\n\nll lucas(ll a, ll b, ll p)\n{\n    \/\/ \u9012\u5f52\u5904\u7406\n    if(a &lt; p &amp;&amp; b &lt; p)\n        return C(a, b, p);\n    \/\/ \u5bf9p\u53d6\u6a21\u540e\u80af\u5b9a\u90fd\u5c0f\u4e8ep\u4e86\uff0c\u6240\u4ee5\u7528C\uff0c \u800c\u6574\u9664p\u4e4b\u540e\u4e0d\u4e00\u5b9a\u5c0f\u4e8ep\uff0c \u8fd8\u9700\u8981\u9012\u5f52\n    \/\/ return\u7684\u53d6\u6a21\u975e\u5e38\u9ebb\u70e6\uff0c \u4f46\u662f\u4e00\u5b9a\u8981\u6ce8\u610f\n    return (C(a % p, b % p, p) * lucas(a \/ p, b \/ p, p) % p) % p;\n}\n\nint main()\n{\n    \/\/ios::sync_with_stdio(false);\n    int n;\n    cin &gt;&gt; n;\n    while (n--)\n    {\n        ll a, b, p;\n        cin &gt;&gt; a &gt;&gt; b &gt;&gt; p;\n        cout &lt;&lt; lucas(a, b, p) &lt;&lt; endl;\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u8282\u4ecb\u7ecd\u7ec4\u5408\u6570\u7684\u4e09\u79cd\u5e38\u89c4\u7cbe\u5ea6\u6c42\u6cd5\uff0c\u7528\u4ee5\u5e94\u5bf9\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e \u770b\u4e00\u4e0b\u7b2c\u4e00\u79cd\u6570\u636e\u7c7b\u578b  [&#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-431","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\/431","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=431"}],"version-history":[{"count":1,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":731,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=\/wp\/v2\/posts\/431\/revisions\/731"}],"wp:attachment":[{"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sssn.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}