{"id":300,"date":"2024-04-27T17:11:24","date_gmt":"2024-04-27T09:11:24","guid":{"rendered":"https:\/\/seanxd.com\/?p=300"},"modified":"2024-04-27T17:11:26","modified_gmt":"2024-04-27T09:11:26","slug":"zerojudge-d712","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-d712\/","title":{"rendered":"ZeroJudge D712: The 3n + 1 problem"},"content":{"rendered":"\n\n\n<p class=\"\">\u8003\u616e\u4ee5\u4e0b\u7684\u6f14\u7b97\u6cd5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"\">\u8f38\u5165 N<\/li>\n\n\n\n<li class=\"\">\u5370\u51fa N<\/li>\n\n\n\n<li class=\"\">\u5982\u679c N = 1 \u7d50\u675f<\/li>\n\n\n\n<li class=\"\">\u5982\u679c N \u662f\u5947\u6578 \u90a3\u9ebc N = 3\uff0aN + 1<\/li>\n\n\n\n<li class=\"\">\u5426\u5247 N = N \/ 2<\/li>\n\n\n\n<li class=\"\">GOTO 2<\/li>\n<\/ol>\n\n\n\n<p class=\"\">\u4f8b\u5982\uff1a\u8f38\u5165 22\uff0c\u5f97\u5230\u7684\u6578\u5217 &#8211;> 22  11  34  17  52  26  13  40  20  10  5  16  8  4  2  1<\/p>\n\n\n\n<p class=\"\">\u64da\u63a8\u6e2c\u6b64\u6f14\u7b97\u6cd5\u5c0d\u4efb\u4f55\u6574\u6578\u800c\u8a00\u6703\u7d42\u6b62 (\u7576\u5217\u5370\u51fa 1 \u7684\u6642\u5019)\u3002\u96d6\u7136\u6b64\u6f14\u7b97\u6cd5\u5f88\u7c21\u55ae\uff0c\u4f46\u4ee5\u4e0a\u7684\u63a8\u6e2c\u662f\u5426\u771f\u5be6\u537b\u7121\u6cd5\u77e5\u9053\u3002\u7136\u800c\u5c0d\u6240\u6709\u7684 N ( 0 &lt; N &lt;= 1,000,000 ) \u4f86\u8aaa\uff0c\u4ee5\u4e0a\u7684\u63a8\u6e2c\u5df2\u7d93\u88ab\u9a57\u8b49\u662f\u6b63\u78ba\u7684\u3002<\/p>\n\n\n\n<p class=\"\"><strong>\u7d66\u4e00\u500b\u8f38\u5165 N\uff0c\u900f\u904e\u4ee5\u4e0a\u7684\u6f14\u7b97\u6cd5\u6211\u5011\u53ef\u4ee5\u5f97\u5230\u4e00\u500b\u6578\u5217 (1 \u4f5c\u70ba\u7d50\u5c3e\uff09\u3002\u6b64\u6578\u5217\u7684\u9577\u5ea6\u7a31\u70ba N \u7684 Cycle Length\u3002<\/strong>\u4e0a\u9762\u63d0\u5230\u7684\u4f8b\u5b50\uff1a22 \u7684 Cycle Length \u70ba 16.<\/p>\n\n\n\n<p class=\"\">\u554f\u984c\u4f86\u4e86\uff1a\u5c0d\u4efb 2 \u500b\u6574\u6578 i \u548c j \u6211\u5011\u60f3\u8981\u77e5\u9053\u4ecb\u65bc i \u548c j (\u542b) \u4e4b\u9593\u7684\u6578\u6240\u7522\u751f\u7684\u6578\u5217\u4e2d\u6700\u5927\u7684 Cycle Length \u662f\u591a\u5c11\u3002<\/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><strong>EOF<\/strong> \u8f38\u5165\uff0c\u6bcf\u7b46\u6e2c\u8cc7\u6709\u4e00\u5c0d\u6574\u6578\u8cc7\u6599 i \u548c j \u3002 (0 &lt;  i, j &lt;= 1,000,000)<\/td><td>\u8f38\u51fa i \u548c j \u9084\u6709\u300c\u5f9e i \u5230 j \u7684 3n+1 Problem \u7684\u6700\u9577\u7e3d Cycle Length\u300d<\/td><\/tr><tr><td>1  10<br>100  200<br>201  210<br>900  1000<\/td><td>1  10  20<br>100  200  125<br>201  210  89<br>900  1000  174<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">ZeroJudge D712 \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 class=\"\">\u672c\u984c\u53ef\u4ee5\u7528\u5efa\u7acb 1 \u5230 1000000 \u7684\u8868\u4f86\u505a\uff0c\u4f46\u662f\u8981\u8dd1 1000000 \u6b21\u7684 3N+1 \u51fd\u5f0f\u9084\u662f\u5f88\u8017\u6642\uff0c\u53ef\u4ee5\u767c\u73fe\u7576 N \u53ef\u4ee5\u88ab 2 \u6574\u9664\u6642\uff0c\u4ed6\u7684 3N+1 \u51fd\u5f0f\u56de\u50b3\u503c\u5c31\u6703\u662f N\/2 \u7684\u56de\u50b3\u503c\u518d\u52a0 1\uff0c\u5229\u7528\u9019\u9ede<strong>\u53ea\u9700\u8981\u5224\u65b7 1 \u5230 1000000 \u4e2d\u7684\u300c\u5947\u6578\u300d\u5c31\u597d\u4e86<\/strong>\u3002<\/p>\n\n\n\n<p class=\"\">\u4f46\u662f\u9019\u6a23\u5b50\u9084\u662f\u4e0d\u5920\u5feb\uff0c\u6240\u4ee5\u9664\u4e86\u5c07 3N+1 \u51fd\u5f0f\u7684\u56de\u50b3\u503c\u9032\u884c\u5efa\u8868\uff0c\u9084\u9700\u8981<strong>\u9032\u884c i \u5230 j \u7684\u6700\u5927\u503c\u7684\u7dda\u6bb5\u6578\u5efa\u7acb<\/strong>\uff0c\u6700\u5f8c\u518d<strong>\u5efa\u7acb\u4e00\u500b Query \u7684\u51fd\u5f0f\u5c07 i \u5230 j \u7684\u6700\u5927\u503c\u9032\u884c\u8a08\u7b97<\/strong>\uff0c\u9019\u6a23\u5b50\u5c31\u4e0d\u6703TLE\u4e86\u3002\u53e6\u5916\uff0c<strong>\u6e2c\u8cc7\u4e2d\u6709\u53ef\u80fd i \u6703\u5927\u65bc j\uff0c\u9019\u7a2e\u60c5\u6cc1\u5c31\u9700\u8981 Swap(i, j)<\/strong>\uff0c<strong>\u4f46\u662f\u8f38\u51fa\u7684\u6642\u5019\u9084\u662f\u8981\u6309\u7167\u539f\u6709\u7684\u9806\u5e8f\u9032\u884c\u8f38\u51fa<\/strong>\uff0c\u9019\u9ede\u9700\u6ce8\u610f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=d712\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge D712: The 3n + 1 problem<\/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;\nusing namespace std;\n\nint MAP[1000005] = {};\nint a, b, maxx = -1;\nint tree[3000000] = {};\n\nvoid initial() {\n    for(int i = 2; i &lt; 1000001; i++)\n    {\n        long long int c = i;\n        int count = 1;\n        if(i % 2 == 0)\n        {\n            MAP[i] = MAP[i&gt;&gt;1]+1;\n            continue;\n        }\n        while(c &gt;= i)\n        {\n            if(c % 2)\n            {\n                c = c*3+1;\n                c &gt;&gt;= 1;\n                count+=2;\n            }\n            else\n            {\n                c = c&gt;&gt;1;\n                count++;\n            }\n        }\n        MAP[i] = count + MAP[c]-1;\n    }\n}\n\nvoid build(int l, int r, int index)\n{\n    if (l == r)\n    {\n        tree[index] = MAP[l];\n        return;\n    }\n    build(l, (l+r)\/2, (2*index)+1);\n    build(((l+r)\/2)+1, r, (2*index)+2);\n    tree[index] = max(tree[(index*2)+1], tree[(index*2)+2]);\n    return;\n}\n\nvoid query (int l, int r, int index)\n{\n    if (a &lt;= l && r &lt;= b)\n    {\n        maxx = max(maxx, tree[index]);\n        return;\n    }\n    if (l == r)\n    {\n        maxx = max(maxx, tree[index]);\n        return;\n    }\n    int half = (l+r)\/2;\n    if (a &lt;= half)\n    {\n        query(l, half, (index*2)+1);\n    }\n    if (b &gt; half) query (half+1, r, (index*2)+2);\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    cout.sync_with_stdio(0);\n    cout.tie(0);\n    MAP[1] = 1;\n    initial();\n    build(1, 1000000, 0);\n    while (cin &gt;&gt; a &gt;&gt; b)\n    {\n        int c = a;\n        int d = b;\n        if (a &gt; b) swap(a, b);\n        maxx = -1;\n        query(1, 1000000, 0);\n        cout &lt;&lt; c &lt;&lt; &quot; &quot; &lt;&lt; d &lt;&lt; &quot; &quot; &lt;&lt; maxx &lt;&lt; &quot;\\n&quot;;\n    }\n}\n\n\/\/ZeroJudge D712\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8003\u616e\u4ee5\u4e0b\u7684\u6f14\u7b97\u6cd5\uff1a \u4f8b\u5982\uff1a\u8f38\u5165 22\uff0c\u5f97\u5230\u7684\u6578\u5217 &#8211;> 22 11 34 17 52 26 13  [&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,13,28],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-zerojudge-","tag-20","tag-8","tag-13","tag-28"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=300"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":302,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/300\/revisions\/302"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}