{"id":615,"date":"2024-06-27T09:00:00","date_gmt":"2024-06-27T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=615"},"modified":"2024-06-27T09:04:57","modified_gmt":"2024-06-27T01:04:57","slug":"zerojudge-b573","status":"publish","type":"post","link":"https:\/\/seanxd.com\/zh\/zerojudge-b573\/","title":{"rendered":"ZeroJudge B573: \u6392\u5217\u7d44\u5408\u3001\u6700\u5927\u516c\u56e0\u6578"},"content":{"rendered":"\n\n\n<p>\u6700\u5927\u516c\u56e0\uf969\uff08Greatest Common Divisor\uff0c\u7c21\u5beb\u70baG.C.D.\uff09\uff0c\u6307\u67d0\u5e7e\u500b\u6574\uf969\u5171\u6709\u56e0\uf969\u4e2d\u6700\u5927\u7684\u4e00\u500b\u3002<br>\u5728\u6392\uf99c\u7d44\u5408\u554f\u984c\u4e2d\u5c07\u4e00\u7d44\uf969\u5b57\u9032\ufa08\u6392\uf99c\uff0c\u53ef\u4ee5\u5f97\u5230\uf967\u540c\u7684\uf969\u5b57\u9806\u5e8f\uff0c\uf9b5\u5982 12 \u9019\u500b\uf969\u7684\u6392\uf99c\u5171\u6709\uff1a(1)12\u3001(2)21 \u4e8c\u7d44(\u7531\u5c0f\u5230\u6392\u5e8f)\uff1b\uf9b5\u5982123 \u9019\u500b\uf969\u7684\u6392\uf99c\u7d44\u5408\u9806\u5e8f\u70ba\uff1a(1) 123\u3001(2) 132\u3001(3) 213\u3001(4) 231\u3001(5) 312\u3001(6) 321 \uf9d1\u7d44 (\u7531\u5c0f\u5230\u6392\u5e8f)\uff1b\uf9b5\u5982 1234 \u9019\uf969\u7684\u6392\uf99c\u7d44\u5408\u6709 24 \u7d44\uff0c\uf969\uf99c\u9806\u5e8f\u5982\u4e0b\uff1a<br>(1) 1234 (2) 1243 (3) 1324 (4) 1342 (5) 1423 (6) 1432<br>(7) 2134 (8) 2143 (9) 2314 (10) 2341 (11) 2413 (12) 2431<br>(13) 3124 (14) 3142 (15) 3214 (16) 3241 (17) 3412 (18) 3421<br>(19) 4123 (20) 4132 (21) 4213 (22) 4231 (23) 4312 (24) 4321<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table nfd-wb-animate nfd-wb-fade-in-bottom-short\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u7b2c 1 \uf99c\u7684\uf969\u5b57 N \u4ee3\u8868\u6709\u5e7e\u7b46\u8cc7\uf9be\u8981\u6e2c\u8a66\uff0c2 &lt;= N &lt;=5 \uff0c\u4e4b\u5f8c\u6bcf\uf99c\u70ba\u6bcf\u7b46\u7684\u6e2c\u8a66\u8cc7\uf9be\uff0c\u5171\u6709\u4e09\u500b\u6b63\u6574\uf969 i\u3001j\u3001k \u4e2d\u9593\u7a7a\u767d\u9694\u958b\u3002<br><br>\u4ee5 i \u9019\u503c\u9032\ufa08\u6392\uf99c\u7d44\u5408\uff0ci \u7684\u96c6\u5408\u70ba{12, 123, 1234, 12345, 123456}\uff0c\u518d\u4f9d i \u503c\u6392\uf99c\u7d44\u5408\u9806\u5e8f (\u7531\u5c0f\u5230\u5927\u6392\u5e8f)\uff0c\u627e\u51fa\u7b2c j \u500b\u548c\u7b2c k \u500b\u7684\u503c\uff0c\u518d\u7b97\u51fa\u500b\u9019\u4e8c\u500b\u503c\u7684\u6700\u5927\u516c\u56e0\uf969\u3002\u7b2cj\u500b\u548c\u7b2ck \u500b\u7684\u503c\u6703\u5b58\u5728\u4ee5 i \u503c\u6392\uf99c\u7d44\u5408\uf969\uf99c\u4e2d\u3002<\/td><td>\u6bcf\u7b46\u6e2c\u8a66\u8cc7\uf9be\u8f38\u51fa\u4e00\uf99c\u3002\u8f38\u51fa\u4ee5 i \u503c\u6392\uf99c\u7d44\u5408\u9806\u5e8f\u4e2d\uff0c\u627e\u51fa\u7b2c j \u500b\u548c\u7b2c k \u500b\u7684\u503c\uff0c\u518d\u7b97\u51fa\u500b\u9019\u4e8c\u500b\u503c\u7684\u6700\u5927\u516c\u56e0\uf969\u3002<\/td><\/tr><tr><td>5<br>12 1 2<br>123 2 5<br>1234 15 9<br>1234 3 4<br>1234 2 5<\/td><td>3<br>12<br>2<br>2<br>1<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge B573 \u7bc4\u4f8b\u6e2c\u8cc7<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u4f7f\u7528 DFS \u7684\u65b9\u5f0f\u4f86\u627e\u4e0d\u540c\u7d44\u5408\u7684\u6578\u5b57\uff0c\u6e2c\u8cc7\u4e2d\u4e0d\u6703\u6709\u76f8\u540c\u7684\u6578\u5b57 (\u4f8b\u5982\uff1a22)\u3002<\/p>\n\n\n\n<p>\u8a2d\u5b9a\u4e00\u500b\u7a7a\u7684\u5b57\u4e32\u5b58\u4e0d\u540c\u7684\u6578\u5b57\u7d44\u5408\u3001\u4e00\u500b Map \u5b58\u5df2\u7d93\u52a0\u904e\u54ea\u4e9b\u6578\u5b57\u4e86\u3002\u5982\u679c\u5b57\u4e32\u9577\u5ea6\u7b49\u65bc\u539f\u59cb\u5b57\u4e32\u9577\u5ea6\u5c31\u53ef\u4ee5\u5c07\u5176\u5b58\u5230\u4e00\u500b\u9663\u5217\u4e2d\uff0c\u5426\u5247\u5c31\u8dd1\u4e00\u500b For\u8ff4\u5708\uff0c\u5982\u679c\u76ee\u524d\u5b57\u5143\u6c92\u6709\u5728 Map \u4e2d\u6709\u8cc7\u6599\uff0c\u5c31\u518d\u4e92\u53eb\u4e00\u6b21 DFS\uff0c\u8981\u5148\u5c07\u5b57\u4e32\u52a0\u4e0a\u76ee\u524d\u7684\u5b57\u5143\u4e26\u4e14 Map \u4e5f\u8981\u66f4\u65b0\uff0c\u547c\u53eb\u5b8c\u4e4b\u5f8c\u8a18\u5f97\u8981\u5c07\u5b57\u4e32\u548c Map \u9084\u539f\u3002<\/p>\n\n\n\n<p>\u627e\u5230\u5169\u500b\u6578\u5b57\u4e4b\u5f8c\u5c31\u627e\u5176\u4e2d\u4e00\u500b\u6578\u5b57\u7684\u6240\u6709\u56e0\u6578\uff0c\u518d\u548c\u53e6\u5916\u4e00\u500b\u6578\u5b57\u4e00\u4e00\u5224\u65b7\u6709\u54ea\u4e9b\u56e0\u6578\u662f\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=b573\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge B573: \u6392\u5217\u7d44\u5408\u3001\u6700\u5927\u516c\u56e0\u6578<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right nfd-delay-50\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;math.h&gt;\n#include &lt;map&gt;\nusing namespace std;\n\nstring num;\nint one, two, a, b;\nvector&lt;int&gt;v;\n\nint toint(string str) {\n    if (str.length() == 1) return int(str[0] - &#39;0&#39;);\n    return stoi(str);\n}\n\nvoid DFS(string sum, map&lt;char, int&gt;MAP) {\n    if (sum.length() == num.length()) {\n        v.push_back(toint(sum));\n        if (v.size() == a) one = v[a-1];\n        if (v.size() == b) two = v[b-1];\n        return;\n    }\n    for (int i = 0; i&lt;num.length(); i++) {\n        if (MAP[num[i]] == 0) {\n            MAP[num[i]]++;\n            DFS(sum+num[i], MAP);\n            MAP[num[i]]--;\n        }\n    }\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    int N;\n    cin &gt;&gt; N;\n    for (int i = 0; i&lt;N; i++) {\n        v.clear();\n        cin &gt;&gt; num &gt;&gt; a &gt;&gt; b;\n        if (a &gt; b) swap(a, b);\n        string sum = &quot;&quot;;\n        map&lt;char, int&gt;MAP;\n        DFS(sum, MAP);\n        vector&lt;int&gt;factor;\n        for (int j = 1; j&lt;sqrt(one)+1; j++) {\n            if (one % j == 0) {\n                factor.push_back(j);\n                factor.push_back(one\/j);\n            }\n        }\n        int ans = 1;\n        for (int j = 0; j&lt;factor.size(); j++) {\n            if (two % factor[j] == 0) ans = factor[j];\n        }\n        cout &lt;&lt; ans &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge B573\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u5927\u516c\u56e0\uf969\uff08Greatest Common Divisor\uff0c\u7c21\u5beb\u70baG.C.D.\uff09\uff0c\u6307\u67d0\u5e7e\u500b\u6574\uf969\u5171\u6709\u56e0\uf969\u4e2d\u6700\u5927\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[29],"tags":[20,8,11,13,12,9],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-20","tag-8","tag-11","tag-13","tag-12","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":3,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/posts\/615\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/zh\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}