{"id":489,"date":"2018-01-10T20:44:25","date_gmt":"2018-01-10T12:44:25","guid":{"rendered":"http:\/\/kylelv.com\/?p=489"},"modified":"2018-01-16T21:30:42","modified_gmt":"2018-01-16T13:30:42","slug":"bzoj-3667-rabin-miller%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=489","title":{"rendered":"bzoj 3667: Rabin-Miller\u7b97\u6cd5"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdef1b31838\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdef1b31838\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#3667_Rabin-Miller%E7%AE%97%E6%B3%95\" title=\"3667: Rabin-Miller\u7b97\u6cd5\">3667: Rabin-Miller\u7b97\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#HINT\" title=\"HINT\">HINT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.kylelv.com\/?p=489\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"3667_Rabin-Miller%E7%AE%97%E6%B3%95\"><\/span>3667: Rabin-Miller\u7b97\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\">Time Limit:\u00a060 Sec\u00a0\u00a0Memory Limit:\u00a0512 MB<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7b2c\u4e00\u884c\uff1aCAS,\u4ee3\u8868\u6570\u636e\u7ec4\u6570\uff08\u4e0d\u5927\u4e8e350\uff09\uff0c\u4ee5\u4e0bCAS\u884c\uff0c\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u4fdd\u8bc1\u572864\u4f4d\u957f\u6574\u5f62\u8303\u56f4\u5185\uff0c\u5e76\u4e14\u6ca1\u6709\u8d1f\u6570\u3002\u4f60\u9700\u8981\u5bf9\u4e8e\u6bcf\u4e2a\u6570\u5b57\uff1a\u7b2c\u4e00\uff0c\u68c0\u9a8c\u662f\u5426\u662f\u8d28\u6570\uff0c\u662f\u8d28\u6570\u5c31\u8f93\u51faPrime<br \/>\n\u7b2c\u4e8c\uff0c\u5982\u679c\u4e0d\u662f\u8d28\u6570\uff0c\u8f93\u51fa\u5b83\u6700\u5927\u7684\u8d28\u56e0\u5b50\u662f\u54ea\u4e2a\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u7b2c\u4e00\u884cCAS(CAS&lt;=350\uff0c\u4ee3\u8868\u6d4b\u8bd5\u6570\u636e\u7684\u7ec4\u6570)<br \/>\n\u4ee5\u4e0bCAS\u884c\uff1a\u6bcf\u884c\u4e00\u4e2a\u6570\u5b57\uff0c\u4fdd\u8bc1\u662f\u572864\u4f4d\u957f\u6574\u5f62\u8303\u56f4\u5185\u7684\u6b63\u6570\u3002<br \/>\n\u5bf9\u4e8e\u6bcf\u7ec4\u6d4b\u8bd5\u6570\u636e\uff1a\u8f93\u51faPrime\uff0c\u4ee3\u8868\u5b83\u662f\u8d28\u6570\uff0c\u6216\u8005\u8f93\u51fa\u5b83\u6700\u5927\u7684\u8d28\u56e0\u5b50\uff0c\u4ee3\u8868\u5b83\u662f\u548c\u6570<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"font-weight: 400;\">6<br \/>\n2<br \/>\n13<br \/>\n134<br \/>\n8897<br \/>\n1234567654321<br \/>\n1000000000000<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"font-weight: 400;\">Prime<br \/>\nPrime<br \/>\n67<br \/>\n41<br \/>\n4649<br \/>\n5<\/p>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u6570\u636e\u8303\u56f4\uff1a<\/p>\n<p>\u4fdd\u8bc1cas&lt;=350\uff0c\u4fdd\u8bc1\u6240\u6709\u6570\u5b57\u5747\u572864\u4f4d\u957f\u6574\u5f62\u8303\u56f4\u5185\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Rabin-Miller\u7d20\u6570\u6d4b\u8bd5\u3001Pollard-rho\u5927\u56e0\u6570\u5206\u89e3 \u7684\u6a21\u677f\u9898<\/p>\n<p>\u9644\u4e00\u4e2a\u7b97\u6cd5\u8bb2\u89e3\uff0c\u975e\u5e38\u5999<\/p>\n<p><a href=\"http:\/\/www.cnblogs.com\/Doggu\/p\/MillerRabin_PollardRho.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.cnblogs.com\/Doggu\/p\/MillerRabin_PollardRho.html<\/a><\/p>\n<p>orz \u53e4\u5927\u7237<\/p>\n<pre class=\"lang:c++ decode:true \">#include&lt;map&gt;\r\n#include&lt;cmath&gt;\r\n#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\n#include&lt;cstdlib&gt;\r\nusing namespace std;\r\n#define inf 1000000007\r\n#define ll long long\r\n#define N 100010\r\ninline ll rd()\r\n{\r\n\tll x=0,f=1;char ch=getchar();\r\n\twhile(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n\twhile(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n\treturn x*f;\r\n}\r\nll T,x,pri[10]={0,2,3,5,7,11,13};\r\nll mul(ll a,ll b,ll p)\r\n{\r\n\tll tmp=a*b-(ll)((long double)a\/p*b+1e-8)*p;\r\n\tif(tmp&lt;0) tmp+=p;return tmp;\r\n}\r\nll ksm(ll a,ll b,ll p)\r\n{\r\n\tll sum=1;\r\n\twhile(b)\r\n\t{\r\n\t\tif(b&amp;1) sum=mul(sum,a,p);\r\n\t\ta=mul(a,a,p);b&gt;&gt;=1;\r\n\t}\r\n\treturn sum;\r\n}\r\nbool MR(ll n)\r\n{\r\n\tif(n==2) return 1;\r\n\tif(n&lt;2||!(n&amp;1)) return 0;\r\n\tfor(int i=1;i&lt;=6;i++)\r\n\t{\r\n\t\tif(n==pri[i]) return 1;\r\n\t\tif(ksm(pri[i],n-1,n)!=1) return 0;\r\n\t}\r\n\tll t,a,tp;\r\n\tfor(int i=1;i&lt;=6;i++)\r\n\t{\r\n\t\tt=n-1;a=pri[i];\r\n\t\twhile(!(t&amp;1))\r\n\t\t{\r\n\t\t\ttp=ksm(a,t&gt;&gt;1,n);\r\n\t\t\tif(tp==n-1) break;;\r\n\t\t\tif(tp^1) return 0;\r\n\t\t\tt&gt;&gt;=1;\r\n\t\t}\r\n\t}\r\n\treturn 1;\r\n}\r\nll mx;\r\nll gcd(ll a,ll b){return b?gcd(b,a%b):a;}\r\nll f(ll x,ll n,ll c)\r\n{\r\n\tll y=mul(x,x,n)+c;\r\n\tif(y&gt;=n) y-=n;\r\n\treturn y;\r\n}\r\nll rho(ll n)\r\n{\r\n\tll c=406,x,y,p;\r\n\tfor(;;c++)\r\n\t{\r\n\t\tx=rand()%n;\r\n\t\ty=f(x,n,c);\r\n\t\twhile(1)\r\n\t\t{\r\n\t\t\tp=gcd(abs(x-y),n);\r\n\t\t\tif(p^1) return p;\r\n\t\t\tx=f(x,n,c);y=f(f(y,n,c),n,c);\r\n\t\t\tif(x==y) break;\r\n\t\t}\r\n\t}\r\n}\r\nvoid sol(ll n)\r\n{\r\n\tif(n==1||n&lt;mx) return ;\r\n\tif(MR(n)){mx=max(mx,n);return;}\r\n\tll tp=rho(n);\r\n\tsol(tp);sol(n\/tp);\r\n}\r\nint main()\r\n{\r\n\tsrand(19740727);\r\n\tT=rd();\r\n\twhile(T--)\r\n\t{\r\n\t\tx=rd();\r\n\t\tif(MR(x)){puts(\"Prime\");continue;}\r\n\t\tmx=0;sol(x);\r\n\t\tprintf(\"%lld\\n\",mx);\r\n\t}\r\n\treturn 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3667: Rabin-Miller\u7b97\u6cd5 Time Limit:\u00a060 Sec\u00a0\u00a0Memory Limit:\u00a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[52],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-rabin-miller"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=489"}],"version-history":[{"count":1,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}