{"id":158,"date":"2025-08-15T11:28:43","date_gmt":"2025-08-15T03:28:43","guid":{"rendered":"https:\/\/bolinio.top\/?p=158"},"modified":"2025-08-15T11:30:04","modified_gmt":"2025-08-15T03:30:04","slug":"c%e4%bd%8d%e8%bf%90%e7%ae%97%ef%bc%9a%e9%80%9a%e8%bf%87%e4%ba%8c%e8%bf%9b%e5%88%b6%e4%bf%9d%e5%ad%98%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/bolinio.top\/?p=158","title":{"rendered":"C++\u4f4d\u8fd0\u7b97\uff1a\u901a\u8fc7\u4e8c\u8fdb\u5236\u4fdd\u5b58\u4fe1\u606f"},"content":{"rendered":"\n<p>\u8fd9\u51e0\u5929\u9047\u5230\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u52a8\u6001\u89c4\u5212&#8212;-\u6570\u4f4dDP\u3002\u505a\u7684\u6211\u5934\u75db\u6b32\u88c2\uff0c\u8fde\u5fd9\u8fc7\u6765\u5199\u4e00\u7bc7\u6709\u5173\u4f4d\u8fd0\u7b97\u7684\u6587\u7ae0\uff0c\u770b\u770b\u5b83\u7684\u795e\u5947\u4e4b\u5904\u3002<\/p>\n\n\n\n<p>\u5728C++\u4e2d\u6709&#8221;\u4e0e&amp;\u3001\u6216|\u3001\u975e~\u3001\u5f02\u6216^\u3001\u53f3\u79fb&gt;&gt;\u3001&lt;&lt;\u5de6\u79fb&#8221;\u8fd9\u516d\u79cd\u8fd0\u7b97\u7b26\u3002\u4f46\u5b9e\u9645\u5728\u4e8c\u8fdb\u5236\u7684\u7b97\u6cd5\u4e2d\u8fdc\u4e0d\u6b62\u8fd9\u4e9b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.0 \u57fa\u672c\u8fd0\u7b97<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u8fd0\u7b97\u524d\u63d0<\/h3>\n\n\n\n<p>\u4f7f\u7528\u4e8c\u8fdb\u5236\u4fdd\u5b58\u4fe1\u606f\uff0c\u4ee3\u8868\u8fd9\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u800c\u4e0d\u662f\u8be5\u4e8c\u8fdb\u5236\u7684\u771f\u5b9e\u6570\u5b57\uff0c\u4f8b\u5982101\uff0c\u5728\u8fd9\u91cc\u4ee3\u8868\u7684\u662f\u96c6\u5408{0, 2}\uff0c\u800c\u4e0d\u662f5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 \u7a7a\u96c6\u3001\u5355\u5143\u96c6\u3001\u5168\u96c6\u548c\u8865\u96c6\uff08\u521d\u59cb\u5316\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7a7a\u96c6 \u2205 \u5c31\u662f\u5168\u7a7a\uff0c\u4e5f\u5c31\u662f <code>res = 0;<\/code><\/li>\n\n\n\n<li>\u5355\u5143\u96c6\uff0c\u5373\u5355\u4e2a\u5143\u7d20\u7684\u96c6\u5408\uff0c\u4f8b\u5982\u6211\u9700\u8981 4 \uff0c\u4e5f\u5c31\u662f 10000\uff0c\u53ea\u9700\u8981 <code>res = 1 &lt;&lt; 4;<\/code> \u5373\u53ef<\/li>\n\n\n\n<li>\u5168\u96c6\uff0c\u6307\u7684\u662f\u4ece 0 \u5230\u6307\u5b9a\u6570\u5b57\u7684\u6240\u6709\u5143\u7d20\uff0c<code>res = (1 &lt;&lt; n) - 1;<\/code> \u53ef\u4ee5\u83b7\u53d6\u4ece 0 ~ n &#8211; 1 \u7684\u6240\u6709\u5143\u7d20<\/li>\n\n\n\n<li>\u8865\u96c6\uff0c\u628a s \u5728\u6307\u5b9a\u6570\u5b57\u5185\u6240\u6709\u6b20\u7f3a\u5143\u7d20\u6307\u51fa\uff0c<code>res  = ((1 &lt;&lt; n) - 1) ^ s;<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 \u5c5e\u4e8e\u4e0e\u4e0d\u5c5e\u4e8e\uff08\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5728s\u4e2d\uff09<\/h3>\n\n\n\n<p><code>res = (s >> i) &amp; 1;<\/code> res\u4e3a 1 \u662f\u5c5e\u4e8e\uff0c\u4e3a 0 \u4fbf\u662f\u4e0d\u5c5e\u4e8e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.4 \u6dfb\u52a0\/\u5220\u9664\u5143\u7d20<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6dfb\u52a0\u5143\u7d20\uff0c\u5f80 s \u4e2d\u6dfb\u52a0\u5143\u7d20 i <code>res = s | (1 &lt;&lt; i);<\/code><\/li>\n\n\n\n<li>\u5220\u9664\u5143\u7d20\uff0c\u5220\u9664 s \u4e2d\u7684\u5143\u7d20 i\uff08\u53ef\u80fd\u4e0d\u5728\u96c6\u5408 s \u4e2d\uff09<code>res = s &amp;~ (1 &lt;&lt; i);<\/code><\/li>\n\n\n\n<li>\u5220\u9664\u5143\u7d20\uff0c\u5220\u9664 s \u4e2d\u7684\u5143\u7d20 i\uff08\u4e00\u5b9a\u5728\u96c6\u5408 s \u4e2d\uff09<code>res = s ^ (1 &lt;&lt; i);<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2.0 \u8fdb\u9636\u8fd0\u7b97<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u8ba1\u7b97\u96c6\u5408\u5927\u5c0f<\/h3>\n\n\n\n<p>\u5373\u8ba1\u7b97 s \u4e2d 1 \u7684\u6570\u91cf\uff0c\u4f7f\u7528C++\u5185\u7f6e\u590d\u6742\u5ea6\u4e3a O( 1 ) \u51fd\u6570\u3002<code>res = __builtin_popcount(s);<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u8ba1\u7b97\u4e8c\u8fdb\u5236\u957f\u5ea6<\/h3>\n\n\n\n<p><code>res = __lg(s) + 1;<\/code> <strong>\u6ce8\u610flg(0)\u662f\u975e\u6cd5\u884c\u4e3a<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 \u96c6\u5408\u6700\u5927\u5143\u7d20<\/h3>\n\n\n\n<p><code>res = __lg(s);<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.4 lowbit\uff08\u96c6\u5408\u6700\u5c0f\u5143\u7d20\uff09<\/h3>\n\n\n\n<p>lowbit\uff0c\u987e\u540d\u601d\u4e49\u5c31\u662f\u83b7\u53d6\u6700\u5c0f\uff08\u6700\u53f3\u8fb9\u7684 1 \uff09\u7684\u5143\u7d20\uff0c<code>lowbit(s) = s &amp; ~s;<\/code><\/p>\n\n\n\n<p>\u4f8b\u5982 s = 1010\uff0c\u5728lowbit\u8fd0\u7b97\u4e4b\u540e\u5c31\u662f 0010\uff0c\u83b7\u53d6\u4e86\u6700\u53f3\u8fb9\u7684 1 \u5143\u7d20\u6240\u5728\u7684\u4f4d\u7f6e<\/p>\n\n\n\n<p>\u5728C++\u5185\u7f6e\u51fd\u6570\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>res = __builtin_ctz(s);<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.5 \u5220\u9664lowbit\u5143\u7d20<\/h3>\n\n\n\n<p>\u5220\u9664\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5176\u4ed6\u4fdd\u6301\u4e0d\u53d8\u3002<code>delete(s) = s &amp; (s - 1);<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3.0 \u904d\u5386\u4e0e\u679a\u4e3e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 \u904d\u5386\u96c6\u5408<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for(int i = 0; i &lt; n; i ++){\n    if((s >> i) &amp; 1){\n        \/\/ \u5904\u7406 s\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u679a\u4e3e\u5168\u96c6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for(int s = 0; s &lt; (1 &lt;&lt; n); s ++) {\n    \/\/ \u5904\u7406 s\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u679a\u4e3e\u975e\u7a7a\u5b50\u96c6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>for(int sub = s; sub; sub = (sub - 1) &amp; s) {\n    \/\/ \u5904\u7406 sub\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 \u679a\u4e3e\u542b\u7a7a\u5b50\u96c6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>int sub = s;\ndo {\n    \/\/ \u5904\u7406 sub\n    sub = (sub - 1) &amp; s;\n} while (sub != s);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.5 \u679a\u4e3e\u8d85\u96c6<\/h3>\n\n\n\n<p>\u5982\u679c&nbsp;<em>T<\/em>&nbsp;\u662f&nbsp;<em>S<\/em>&nbsp;\u7684\u5b50\u96c6\uff0c\u90a3\u4e48\u79f0&nbsp;<em>S<\/em>&nbsp;\u662f&nbsp;<em>T<\/em>&nbsp;\u7684<strong>\u8d85\u96c6<\/strong>\uff08superset\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int s = t; s &lt; (1 &lt;&lt; n); s = (s + 1) | t) {\n    \/\/ \u5904\u7406 s\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4.0 \u603b\u7ed3<\/h2>\n\n\n\n<p>\u9996\u5148\u662f\u8d44\u6599\u6574\u7406\u7684\u6765\u6e90\uff0c<em>\u90e8\u5206\u5185\u5bb9\u6765\u81ea\u4e8e\u7f51\u7edc\u6536\u96c6\uff0c3.0\u7684\u5185\u5bb9\u501f\u9274\u4e86<a href=\"https:\/\/leetcode.cn\/u\/endlesscheng\/\">\u7075\u8336\u5c71\u827e\u5e9c<\/a>\u7684\u6574\u7406\u3002<\/em><\/p>\n\n\n\n<p>\u6574\u7406\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u786e\u5b9e\u7406\u89e3\u5230\u4e86\u4e8c\u8fdb\u5236\u7684\u5999\u5904\u3002\u63a5\u89e6\u4f4d\u8fd0\u7b97\u7684\u611f\u89c9\u5176\u5b9e\u5c31\u662f&#8221;\u7b80\u5355 -> \u597d\u96be -> <s>\u4e0d\u60f3\u5b66<\/s> -> \u6709\u70b9\u610f\u601d -> \u609f\u4e86&#8221;\u7684\u8fc7\u7a0b\u3002\u6240\u4ee5\u5982\u679c\u65e0\u6cd5\u7406\u89e3\u5927\u4f6c\u89e3\u91ca\u7684\u8bdd\uff0c\u5c31\u5148\u6253\u597d\u57fa\u7840\uff0c\u4e86\u89e3\u7528\u9014\uff0c\u7136\u540e\u518d\u53bb\u6162\u6162\u7406\u89e3\u5c31\u597d\u8fa3~<\/p>\n\n\n\n<p>\u6709\u5174\u8da3\u7684\u8bdd\u53ef\u4ee5\u89c2\u770b\u6211\u7684\u4ee5\u5f80\u6587\u7ae0<s>(\u8fd8\u6ca1\u591a\u5c11\u6b38\uff09<\/s>\u3002\u535a\u5ba2\u8fd8\u5728\u6301\u7eed\u66f4\u65b0\u4e2d\uff0c\u656c\u8bf7\u671f\u5f85~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u51e0\u5929\u9047\u5230\u4e86\u4e00\u79cd\u7279\u6b8a\u7684\u52a8\u6001\u89c4\u5212&#8212;-\u6570\u4f4dDP\u3002\u505a\u7684\u6211\u5934\u75db\u6b32\u88c2\uff0c\u8fde\u5fd9\u8fc7\u6765\u5199\u4e00\u7bc7\u6709\u5173\u4f4d\u8fd0\u7b97\u7684\u6587\u7ae0\uff0c\u770b\u770b\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,20],"tags":[15,21],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-c-c","category-20","tag-c-c","tag-21"],"_links":{"self":[{"href":"https:\/\/bolinio.top\/index.php?rest_route=\/wp\/v2\/posts\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bolinio.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bolinio.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bolinio.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bolinio.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158"}],"version-history":[{"count":22,"href":"https:\/\/bolinio.top\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/bolinio.top\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions\/180"}],"wp:attachment":[{"href":"https:\/\/bolinio.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bolinio.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bolinio.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}