{"id":39,"date":"2026-04-05T16:01:57","date_gmt":"2026-04-05T08:01:57","guid":{"rendered":"https:\/\/blog.sjll.top\/?p=39"},"modified":"2026-04-05T16:01:57","modified_gmt":"2026-04-05T08:01:57","slug":"%e6%ac%a7%e6%8b%89%e7%ad%9b%ef%bc%88%e7%ba%bf%e6%80%a7%e7%ad%9b%ef%bc%89%e7%ae%97%e6%b3%95%e8%af%a6%e8%a7%a3%ef%bc%9ac%e5%ae%9e%e7%8e%b0%e4%b8%8e%e8%b4%a8%e5%9b%a0%e6%95%b0%e5%88%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.sjll.top\/?p=39","title":{"rendered":"\u6b27\u62c9\u7b5b\uff08\u7ebf\u6027\u7b5b\uff09\u7b97\u6cd5\u8be6\u89e3\uff1aC++\u5b9e\u73b0\u4e0e\u8d28\u56e0\u6570\u5206\u89e3"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u6b27\u62c9\u7b5b\uff1f<\/h2>\n\n\n\n<p><strong>\u6b27\u62c9\u7b5b\uff08Euler&#8217;s Sieve\uff09<\/strong>\uff0c\u53c8\u79f0<strong>\u7ebf\u6027\u7b5b<\/strong>\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u5feb\u901f\u627e\u51fa\u4e00\u5b9a\u8303\u56f4\u5185\u6240\u6709\u8d28\u6570\u7684\u7b97\u6cd5\u3002\u7531\u6570\u5b66\u5bb6\u6b27\u62c9\u53d1\u660e\uff0c\u5176\u6838\u5fc3\u4f18\u52bf\u662f\u6bcf\u4e2a\u5408\u6570\u53ea\u4f1a\u88ab\u5176<strong>\u6700\u5c0f\u7684\u8d28\u56e0\u5b50<\/strong>\u6807\u8bb0\u4e00\u6b21\uff0c\u5b9e\u73b0\u4e86\u771f\u6b63\u7684 O(n) \u65f6\u95f4\u590d\u6742\u5ea6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f20\u7edf\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\u7684\u4e0d\u8db3<\/h3>\n\n\n\n<p>\u5728\u4ecb\u7ecd\u6b27\u62c9\u7b5b\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u770b\u770b\u4f20\u7edf\u7684<strong>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5\uff08Sieve of Eratosthenes\uff09<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\">\/\/ \u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5 - \u65f6\u95f4\u590d\u6742\u5ea6 O(n log log n)\nvector&lt;bool&gt; is_prime(n + 1, true);\nis_prime[0] = is_prime[1] = false;\n\nfor (int i = 2; i * i &lt;= n; i++) {\n    if (is_prime[i]) {\n        for (int j = i * i; j &lt;= n; j += i) {\n            is_prime[j] = false;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u95ee\u9898\u5728\u4e8e\uff1a\u6bcf\u4e2a\u5408\u6570\u4f1a\u88ab\u591a\u4e2a\u8d28\u6570\u6807\u8bb0\uff08\u598212\u4f1a\u88ab2\u548c3\u90fd\u6807\u8bb0\uff09\uff0c\u5bfc\u81f4\u6548\u7387\u4e0d\u662f\u6700\u4f18\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b27\u62c9\u7b5b\u7684\u6838\u5fc3\u601d\u60f3<\/h3>\n\n\n\n<p>\u6b27\u62c9\u7b5b\u901a\u8fc7\u4ee5\u4e0b\u5173\u952e\u4f18\u5316\u89e3\u51b3\u91cd\u590d\u6807\u8bb0\u95ee\u9898\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>\u6bcf\u4e2a\u5408\u6570\u53ea\u88ab\u5176\u6700\u5c0f\u7684\u8d28\u56e0\u5b50\u7b5b\u53bb<\/strong><\/p><\/blockquote>\n\n\n\n<p>\u5b9e\u73b0\u8fd9\u4e00\u601d\u60f3\u7684\u5173\u952e\u662f\uff1a<strong>\u5728\u904d\u5386\u8d28\u6570\u65f6\uff0c\u4e00\u65e6\u8d28\u6570\u5927\u4e8e\u5f53\u524d\u6570\u7684\u6700\u5c0f\u8d28\u56e0\u5b50\uff0c\u5c31\u505c\u6b62\u904d\u5386<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C++ \u4ee3\u7801\u5b9e\u73b0<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"cpp\">#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\n\/\/ \u6b27\u62c9\u7b5b\uff08\u7ebf\u6027\u7b5b\uff09\u6c42\u8d28\u6570\nvector&lt;int&gt; euler_sieve(int n) {\n    vector&lt;int&gt; primes;       \/\/ \u5b58\u50a8\u6240\u6709\u8d28\u6570\n    vector&lt;bool&gt; is_prime(n + 1, true);\n    is_prime[0] = is_prime[1] = false;\n    \n    for (int i = 2; i &lt;= n; i++) {\n        if (is_prime[i]) {\n            primes.push_back(i);\n        }\n        \n        \/\/ \u5173\u952e\uff1a\u904d\u5386\u6240\u6709\u5df2\u77e5\u8d28\u6570\n        for (int p : primes) {\n            long long x = 1LL * i * p;\n            if (x &gt; n) break;           \/\/ \u8d85\u51fa\u8303\u56f4\uff0c\u505c\u6b62\n            \n            is_prime[x] = false;        \/\/ \u6807\u8bb0\u4e3a\u5408\u6570\n            \n            \/\/ \u6838\u5fc3\u4f18\u5316\uff1ap \u662f i \u7684\u8d28\u56e0\u5b50\u65f6\u505c\u6b62\n            \/\/ \u8fd9\u6837\u4fdd\u8bc1\u6bcf\u4e2a\u5408\u6570\u53ea\u88ab\u6700\u5c0f\u8d28\u56e0\u5b50\u7b5b\u53bb\n            if (i % p == 0) break;\n        }\n    }\n    \n    return primes;\n}\n\n\/\/ \u6269\u5c55\uff1a\u540c\u65f6\u8ba1\u7b97\u6bcf\u4e2a\u6570\u7684\u6700\u5c0f\u8d28\u56e0\u5b50\nvoid euler_sieve_with_spf(int n, vector&lt;int&gt;&amp; primes, vector&lt;int&gt;&amp; spf) {\n    spf.assign(n + 1, 0);\n    \n    for (int i = 2; i &lt;= n; i++) {\n        if (spf[i] == 0) {\n            spf[i] = i;\n            primes.push_back(i);\n        }\n        \n        for (int p : primes) {\n            long long x = 1LL * i * p;\n            if (x &gt; n) break;\n            \n            spf[x] = p;  \/\/ p \u662f x \u7684\u6700\u5c0f\u8d28\u56e0\u5b50\n            \n            if (p == spf[i]) break;\n        }\n    }\n}\n\n\/\/ \u5229\u7528 SPF \u8fdb\u884c\u8d28\u56e0\u6570\u5206\u89e3\nvector&lt;pair&lt;int, int&gt;&gt; factorize(int n, const vector&lt;int&gt;&amp; spf) {\n    vector&lt;pair&lt;int, int&gt;&gt; factors;\n    \n    while (n &gt; 1) {\n        int p = spf[n];\n        int cnt = 0;\n        while (n % p == 0) {\n            n \/= p;\n            cnt++;\n        }\n        factors.push_back({p, cnt});\n    }\n    \n    return factors;\n}\n\nint main() {\n    int n = 100;\n    vector&lt;int&gt; primes = euler_sieve(n);\n    \n    cout &lt;&lt; \"1\u5230\" &lt;&lt; n &lt;&lt; \"\u7684\u6240\u6709\u8d28\u6570: \" &lt;&lt; endl;\n    for (int p : primes) {\n        cout &lt;&lt; p &lt;&lt; \" \";\n    }\n    cout &lt;&lt; endl;\n    \n    \/\/ \u6269\u5c55\uff1a\u8ba1\u7b97\u6700\u5c0f\u8d28\u56e0\u5b50\n    vector&lt;int&gt; spf;\n    euler_sieve_with_spf(n, primes, spf);\n    \n    cout &lt;&lt; \"\\n\u6700\u5c0f\u8d28\u56e0\u5b50\u8868: \" &lt;&lt; endl;\n    for (int i = 2; i &lt;= n; i++) {\n        cout &lt;&lt; \"spf[\" &lt;&lt; i &lt;&lt; \"] = \" &lt;&lt; spf[i] &lt;&lt; endl;\n    }\n    \n    \/\/ \u8d28\u56e0\u6570\u5206\u89e3\u793a\u4f8b\n    int num = 36;\n    vector&lt;pair&lt;int, int&gt;&gt; factors = factorize(num, spf);\n    cout &lt;&lt; \"\\n\" &lt;&lt; num &lt;&lt; \"\u7684\u8d28\u56e0\u6570\u5206\u89e3: \";\n    for (auto [p, cnt] : factors) {\n        cout &lt;&lt; p &lt;&lt; \"^\" &lt;&lt; cnt &lt;&lt; \" \";\n    }\n    cout &lt;&lt; endl;\n    \n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6b27\u62c9\u7b5b vs \u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u7279\u6027<\/th><th>\u6b27\u62c9\u7b5b<\/th><th>\u57c3\u62c9\u6258\u65af\u7279\u5c3c\u7b5b\u6cd5<\/th><\/tr><\/thead><tbody><tr><td>\u65f6\u95f4\u590d\u6742\u5ea6<\/td><td>O(n)<\/td><td>O(n log log n)<\/td><\/tr><tr><td>\u7a7a\u95f4\u590d\u6742\u5ea6<\/td><td>O(n)<\/td><td>O(n)<\/td><\/tr><tr><td>\u91cd\u590d\u6807\u8bb0<\/td><td>\u65e0<\/td><td>\u6709<\/td><\/tr><tr><td>\u5b9e\u73b0\u96be\u5ea6<\/td><td>\u7a0d\u96be<\/td><td>\u7b80\u5355<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u6b27\u62c9\u7b5b\u662f O(n)\uff1f<\/h3>\n\n\n\n<p>\u5173\u952e\u5728\u4e8e\u5185\u5c42\u5faa\u73af\u7684 <code>if (i % p == 0) break;<\/code><\/p>\n\n\n\n<p>\u5047\u8bbe i = 12\uff0cprimes = {2, 3, 5, 7, &#8230;}\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>p = 2\uff1a12 \u00d7 2 = 24\uff0c\u6807\u8bb0\u4e3a\u5408\u6570\uff0c12 % 2 == 0\uff0c<strong>\u505c\u6b62<\/strong><\/li><li>\u5982\u679c\u4e0d\u505c\u6b62\uff0c\u63a5\u7740\u4f1a\u7528 p = 3 \u6807\u8bb0 36\uff0c\u4f46\u8fd9\u6b65\u88ab\u8df3\u8fc7\u4e86<\/li><\/ul>\n\n\n\n<p>\u8fd9\u6837\uff0c\u6bcf\u4e2a\u5408\u6570 n \u53ea\u4f1a\u88ab\u5176\u6700\u5c0f\u8d28\u56e0\u5b50 p \u6807\u8bb0\u4e00\u6b21\uff0c\u603b\u64cd\u4f5c\u6b21\u6570\u63a5\u8fd1 n\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e94\u7528\u573a\u666f<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>\u8d28\u6570\u7b5b\u53d6<\/strong>\uff1a\u5feb\u901f\u751f\u6210\u5927\u91cf\u8d28\u6570<\/li><li><strong>\u8d28\u56e0\u6570\u5206\u89e3<\/strong>\uff1a\u5229\u7528 SPF \u5feb\u901f\u5206\u89e3\u4efb\u610f\u6570<\/li><li><strong>\u6570\u8bba\u95ee\u9898<\/strong>\uff1a\u9700\u8981\u8d28\u6570\u76f8\u5173\u4fe1\u606f\u7684\u573a\u666f<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p>\u6b27\u62c9\u7b5b\u662f\u8d28\u6570\u7b5b\u53d6\u7684\u6700\u4f18\u7b97\u6cd5\uff0c\u867d\u7136\u5b9e\u73b0\u6bd4\u4f20\u7edf\u7b5b\u6cd5\u7a0d\u590d\u6742\uff0c\u4f46 O(n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f7f\u5176\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u5177\u6709\u660e\u663e\u4f18\u52bf\u3002\u5efa\u8bae\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f18\u5148\u4f7f\u7528\u6b27\u62c9\u7b5b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u6b27\u62c9\u7b5b\uff1f \u6b27\u62c9\u7b5b\uff08Euler&#8217;s Sieve\uff09\uff0c\u53c8\u79f0\u7ebf\u6027\u7b5b\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u5feb\u901f\u627e\u51fa\u4e00\u5b9a\u8303\u56f4\u5185\u6240\u6709 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[47,46,45,44],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-3","tag-eratosthenes","tag-euler","tag-sieve","tag-c"],"_links":{"self":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":1,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}