{"id":4221,"date":"2024-02-27T12:53:57","date_gmt":"2024-02-27T04:53:57","guid":{"rendered":""},"modified":"2024-02-27T12:53:57","modified_gmt":"2024-02-27T04:53:57","slug":"\u5e38\u7528\u7b97\u6cd5\u6a21\u677f\u603b\u7ed3\uff081\uff09----\u5feb\u901f\uff0c\u6700\u5927\u516c\u7ea6\u6570\uff0c\u6700\u5c0f\u516c\u500d\u6570\uff0c\u6c42\u4e00\u4e2a\u6570\u7684\u6240\u6709\u56e0\u6570\u4e4b\u548c\uff0c\u7d20\u6570\u5224\u65ad","status":"publish","type":"post","link":"https:\/\/mushiming.com\/4221.html","title":{"rendered":"\u5e38\u7528\u7b97\u6cd5\u6a21\u677f\u603b\u7ed3\uff081\uff09----\u5feb\u901f\uff0c\u6700\u5927\u516c\u7ea6\u6570\uff0c\u6700\u5c0f\u516c\u500d\u6570\uff0c\u6c42\u4e00\u4e2a\u6570\u7684\u6240\u6709\u56e0\u6570\u4e4b\u548c\uff0c\u7d20\u6570\u5224\u65ad"},"content":{"rendered":"
\u5feb\u901f\u5e42\u7684\u6a21\u677f\u5927\u5bb6\u5e94\u8be5\u662f\u4e0d\u964c\u751f\u7684\uff0c\u4e4b\u524d\u6211\u4e00\u76f4\u662f\u76f4\u63a5\u8bb0\u6a21\u677f\u7684\uff0c\u4eca\u5929\u6765\u5177\u4f53\u89e3\u91ca\u4e00\u4e0b\u5feb\u901f\u5e42\u6a21\u677f\u7684\u610f\u4e49\u3002<\/strong><\/p>\n \u4e0d\u53d6\u6a21\u7684\u6a21\u677f\u5982\u4e0b\uff1a\uff08\u53d6\u6a21\u81ea\u5df1\u4fee\u6539\u4e00\u4e0b\uff09<\/strong><\/p>\n \u5982\u4f55\u7406\u89e3\u8be5\u6a21\u677f\uff1a<\/span><\/strong><\/p>\n \u9996\u5148\u5feb\u901f\u5e42\u7684\u57fa\u672c\u601d\u60f3\u662f\u5206\u6cbb\u548c\u4e8c\u8fdb\u5236\uff0c\u6211\u4eec\u76f4\u63a5\u5e26\u5165\u4e00\u4e2a\u4f8b\u5b50\u66f4\u597d\u7406\u89e3\uff0c\u5982\u8ba1\u7b972\u768411\u6b21\u65b9\u3002<\/strong><\/span><\/p>\n \u9996\u5148ans=1,base=2,b=11;\u6211\u4eec\u77e5\u905311\u7684\u4e8c\u8fdb\u5236\u662f1011\uff1b<\/strong><\/span>if(b&1!=0) ans=ans*base;\u8fd9\u4e00\u4e2a\u8bed\u53e5\u7684\u4f5c\u7528\u662f\uff0c\u6839\u636e\u5e42\u7684\u4e8c\u8fdb\u5236\u6765\u51b3\u5b9a\u662f\u5426\u8981\u4e58\u5165\u7b54\u6848\uff0cbase*=base;\u7684\u4f5c\u7528\u662f\u8ba9\u57fa\u6570\u7ffb\u500d\uff1b<\/strong><\/span><\/p>\n \u62ff2\u768411\u6b21\u65b9\u6765\u8bf4\uff0c\u2460b=1011,ans=1,base=2 \u2461ans=1*2,base=2*2,b=101, \u2462ans=2*2*2\uff0cbase=2*2*2*2\uff0cb=10 \u2463ans=2*2*2\uff0cbase=2*2*2*2*2*2*2*2,b=1 \u2464 ans=2*2*2*2*2*2*2*2*2*2*2,b=0 \u2465\u8df3\u51fafor\u5faa\u73af\uff0c\u7ed3\u675f\u3002<\/strong><\/span><\/p>\n \u8fd9\u4e48\u7406\u89e3\u4e00\u4e0b\uff0c\u5feb\u901f\u5e42\u7684\u6a21\u677f\u518d\u4e5f\u4e0d\u7528\u56db\u7ea7\u786c\u80cc\u4e86\uff0c\u4fe1\u624b\u62c8\u6765\u3002<\/strong><\/span><\/p>\n \u7531\u4e0a\u53ef\u89c1\uff0c\u8fbe\u5230\u4e86\u8ba1\u7b972\u768411\u6b21\u65b9\u7684\u76ee\u7684\uff0c\u6700\u574f\u7684\u590d\u6742\u5ea6\u4e5f\u53ea\u4e3aO(logn)\uff0c\u800c\u66b4\u529b\u7684\u590d\u6742\u5ea6\u662fO(n),\u5feb\u901f\u5e42\u7b80\u76f4\u662f\u5b8c\u7f8e\u60c5\u4eba\uff01<\/strong><\/span><\/p>\n \uff081\uff09\u7ecf\u5178\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\uff08\u8f97\u8f6c\u76f8\u9664\u6cd5\uff09\u4e00\u822c\u7528\u8fd9\u4e2a\u5c31\u591f\u4e86<\/strong><\/span><\/p>\n \u5982\u4f55\u7406\u89e3\u8bb0\u5fc6\uff1a<\/span><\/strong><\/p>\n \u5199\u9898\u6700\u8ba8\u538c\u7684\u5c31\u662f\u627e\u4ee3\u7801\u6a21\u677f\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u8981\u505a\u5230\u8ba9\u8fd9\u4e9b\u5e38\u7528\u4ee3\u7801\u5370\u5728\u5fc3\u4e2d\uff01<\/strong><\/p>\n \u5176\u5b9e\u5c31\u662f\u4e24\u4e2a\u6570\u53cd\u590d\u6362\u4f4d\u76f8\u4e92\u6c42\u4f59\uff0c\u5f53b\u4e3a0\u65f6\uff0ca\u5373\u4e3a\u6700\u5927\u516c\u56e0\u6570\u3002<\/strong><\/p>\n \u5982\u8ba1\u7b978\u548c12\u7684\u6700\u5927\u516c\u56e0\u6570\uff0cgcd(8,12),gcd(12\uff0c8)\uff0cgcd(8,4),gcd(4,0)\u3002<\/strong><\/p>\n \u63a8\u5bfc\u4e00\u904d\u5c31\u5f88\u5bb9\u6613\u8bb0\u4e0b\u3002\u6b27\u51e0\u91cc\u5f97\u7b97\u6cd5\u4f9d\u65e7\u662f\u590d\u6742\u5ea6\u4e3aO\uff08logn\uff09\u7684\u4f18\u79c0\u7b97\u6cd5\u3002<\/strong><\/p>\n \uff082\uff09stein\u7b97\u6cd5\uff08\u5982\u679c\u4e0d\u80fdA\u9898\uff0c\u7528\u8fd9\u4e2a\u6539\u8fdb\u4e00\u4e0b\uff09<\/strong><\/span><\/p>\n<\/p>\n \u9488\u5bf9\u6b27\u51e0\u91cc\u5fb7\u7b97\u6cd5\u5728\u5bf9\u5927\u6574\u6570\u8fdb\u884c\u8fd0\u7b97\u65f6\uff0c\u9700\u8981\u8bd5\u5546\u5bfc\u81f4\u589e\u52a0\u8fd0\u7b97\u65f6\u95f4\u7684\u7f3a\u9677\u800c\u63d0\u51fa\u7684\u6539\u8fdb\u7b97\u6cd5\u3002<\/strong><\/p>\n \u76f4\u63a5\u8d34\u4ee3\u7801\uff0c\u56e0\u4e3a\u4e5f\u4e0d\u5e38\u7528\uff0c\u6ca1\u6709\u53bb\u7406\u89e3\uff0c\u7528\u5f97\u4e0a\u7684\u673a\u4f1a\u6bd4\u8f83\u5c11\u3002<\/strong><\/p>\n \u6c42\u4e24\u4e2a\u6570\u7684\u6700\u5c0f\u516c\u500d\u6570\u7b97\u6cd5\u662f\u57fa\u4e8egcd\u7b97\u6cd5\u7684\u57fa\u7840\u4e0a\u7684\u3002<\/strong><\/p>\n \u5982\u4f55\u7406\u89e3\u8bb0\u5fc6\uff1a<\/span><\/strong><\/p>\n \u8fd9\u4e2a\u8fd8\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u5c31\u662f\u7528\u4e00\u4e2a\u6570\u53bb\u9664\u4ee5\u6700\u5927\u516c\u56e0\u6570\u5f97\u5230\u7684\u7ed3\u679c\uff0c\u518d\u4e58\u4ee5\u53e6\u4e00\u4e2a\u6570<\/u>\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e24\u4e2a\u6570\u7684\u6700\u5c0f\u516c\u500d\u6570\u3002<\/strong><\/p>\n<\/p>\n \uff08\u5305\u62ec\u4e00\uff0c\u4e0d\u5305\u62ec\u672c\u8eab\uff09\u7b97\u6cd5\u7684\u89e3\u91ca\u90fd\u5728\u4ee3\u7801\u91cc\u9762\u4e86\uff0c\u6bd4\u8f83\u597d\u7406\u89e3\u3002<\/strong><\/p>\n <\/p>\n \uff081\uff09\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u7b80\u5355\u7684\u5224\u65ad\u7d20\u6570\u4ee3\u7801\u3002<\/strong><\/span><\/p>\n \u8fd9\u4e2a\u6bd4\u8f83\u597d\u7406\u89e3\uff0c\u53ea\u8981\u6709\u96641\u4ee5\u5916\u7684\u56e0\u6570\u76f4\u63a5\u8fd4\u56defalse\uff0c\u5426\u5219\u8fd4\u56detrue<\/strong><\/p>\n \u590d\u6742\u5ea6O(\u6839\u53f7n)\uff0c\u8fd9\u4e2a\u6a21\u677f\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5bf9\u4e8e10\u768412\u6b21\u65b9\u4ee5\u5185\u7684\u6570\u7684\u5224\u65ad\u662f\u6ca1\u6709\u95ee\u9898\u7684<\/strong><\/p>\n \uff082\uff09\u66f4\u5feb\u901f\u7684\u65b9\u6cd5<\/strong><\/span><\/p>\n \u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5728\u6570\u636e\u4e0d\u662f\u5f88\u5927\u65f6\u5bf9\u6bd4\u4e0d\u660e\u663e\uff0c\u6570\u636e\u5f88\u5927\u65f6\u53ef\u4ee5\u4f53\u73b0\u51fa\u65b9\u6cd52\u7684\u4f18\u8d8a\u6027\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/span><\/p>\n \u5982\u4f55\u7406\u89e3\u8bb0\u5fc6\uff1a<\/span><\/strong><\/p>\n \u8fd9\u4e2a\u7684\u7406\u89e3\u8fc7\u7a0b\u5c31\u6bd4\u8f83\u957f\u4e86\u3002<\/span><\/strong>\u3010\u7d20\u6570\u7684\u5224\u5b9a-\u4ece\u66b4\u529b\u5230\u9ad8\u6548\u3011-C++ - \u6478\u9c7c\u9171 - \u535a\u5ba2\u56ed (cnblogs.com)<\/p>\n \u53ef\u4ee5\u53bb\u535a\u5ba2\u56ed\u770b\u4e00\u4e0b\u8fd9\u7bc7\u6bd4\u8f83\u8be6\u7ec6\u7684\u535a\u5ba2\uff0c\u642c\u8fd0\u4e00\u4e0b\u3002<\/p>\nll fp(ll a,ll b){\n\tll ans=1;\n\tll base=a;\n\twhile(b!=0){\n\t\tif(b&1!=0)\n ans=ans*base;\n base*=base;\n b>>=1;\n\t}\n\treturn ans;\n} <\/code><\/pre>\n
2.\u6c42\u4e24\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u6a21\u677f<\/strong><\/span><\/h3>\n
int gcd(int a,int b){\n\tif(b==0) return a;\n\telse return gcd(b,a%b);\n}<\/code><\/pre>\n
ll gcd(ll u,ll v)\n{\n if (u == 0) return v;\n if (v == 0) return u;\n if (~u & 1) \n {\n if (v & 1) \n return gcd(u >> 1, v);\n else \n return gcd(u >> 1, v >> 1) << 1;\n }\n if (~v & 1) \n return gcd(u, v >> 1);\n if (u > v)\n return gcd((u - v) >> 1, v);\n return gcd((v - u) >> 1, u);\n}\n<\/code><\/pre>\n
3.\u6700\u5c0f\u516c\u500d\u6570\u3002<\/strong><\/h3>\n
int lcm(int a,int b){\n\treturn a\/gcd(a,b)*b;\n}<\/code><\/pre>\n
4.\u6c42\u4e00\u4e2a\u6570\u7684\u6240\u6709\u56e0\u6570\u4e4b\u548c\u3002<\/strong><\/h3>\n
\nint factorSum(int n){\n\t int sum=0;\n for (int i=1;i*i<=n;i++){\/\/\u6298\u534a\u9664\uff0c\u964d\u4f4e\u590d\u6742\u5ea6\n if (n%i==0){\n if (i==1||i*i==n) sum+=i;\/\/\u5982\u679c\u8fd9\u4e2a\u56e0\u6570\u4e3a1\u6216\u8005\u521a\u597d\u662f\u8be5\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ea\u52a0\u4e00\u4e2a\u5c31\u53ef\u4ee5\u4e86\u3002\n else\tsum+=i+n\/i;\/\/\u52a0\u4e0a\u8be5\u56e0\u6570\u548c\u8be5\u56e0\u6570\u7684\u5173\u8054\u56e0\u6570\u3002\n }\n }\n return sum;\n}\n <\/code><\/pre>\n
5.\u7d20\u6570\u5224\u65ad<\/h3>\n
bool is_prime(int n){\n\tif(n<=1) return false;\n\tfor(int i=2;i*i<=n;i++)\n\t if(n%i==0) return false;\n\treturn true; \n}<\/code><\/pre>\n
bool isPrime(int a) {\n if(a == 1) return 0;\n if(a == 2 || a == 3) return 1;\n if(a%6 != 1 && a%6 != 5) return 0;\n int sqrtA = sqrt(a);\n for(int i = 5; i <= sqrtA; i += 6) {\n if(!(a%i)|| !(a%(i+2))) return 0;\n }\n return 1;\n}<\/code><\/pre>\n
\u770b\u5230\u8fd9\u91cc\uff0c\u611f\u8c22\u89c2\u770b\uff0c\u5199\u4e86\u8fd9\u4e48\u4e45\uff0c\u70b9\u4e2a\u8d5e\u5427\uff0c\u563f\u563f\u563f\uff0c\u4e0b\u6b21\u89c1\u3002<\/h4>\n","protected":false},"excerpt":{"rendered":"\u5e38\u7528\u7b97\u6cd5\u6a21\u677f\u603b\u7ed3\uff081\uff09----\u5feb\u901f\uff0c\u6700\u5927\u516c\u7ea6\u6570\uff0c\u6700\u5c0f\u516c\u500d\u6570\uff0c\u6c42\u4e00\u4e2a\u6570\u7684\u6240\u6709\u56e0\u6570\u4e4b\u548c\uff0c\u7d20\u6570\u5224\u65ad1.\u5feb\u901f\u5e42\u6a21\u677f","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4221"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=4221"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4221\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=4221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=4221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=4221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}