{"id":445,"date":"2017-04-16T22:31:36","date_gmt":"2017-04-16T14:31:36","guid":{"rendered":"http:\/\/kylelv.com\/?p=445"},"modified":"2018-01-03T18:26:00","modified_gmt":"2018-01-03T10:26:00","slug":"bzoj-3944-sum-%e6%9d%9c%e6%95%99%e7%ad%9b","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=445","title":{"rendered":"bzoj 3944: Sum  &#8212; \u675c\u6559\u7b5b"},"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-69fdeef335152\" 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-69fdeef335152\"  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=445\/#3944_Sum\" title=\"3944: Sum\">3944: Sum<\/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=445\/#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=445\/#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=445\/#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=445\/#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=445\/#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=445\/#HINT\" title=\"HINT\">HINT<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"3944_Sum\"><\/span>3944: Sum<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><b><\/b>Time Limit:\u00a010 Sec\u00a0\u00a0Memory Limit:\u00a0128 MB<b><\/b><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-446 size-full\" src=\"http:\/\/kylelv.com\/wp-content\/uploads\/2018\/01\/aaa.png\" alt=\"\" width=\"424\" height=\"129\" srcset=\"https:\/\/blog.kylelv.com\/wp-content\/uploads\/2018\/01\/aaa.png 424w, https:\/\/blog.kylelv.com\/wp-content\/uploads\/2018\/01\/aaa-300x91.png 300w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e00\u5171T+1\u884c<\/p>\n<p>\u7b2c1\u884c\u4e3a\u6570\u636e\u7ec4\u6570T(T&lt;=10)<\/p>\n<p>\u7b2c2~T+1\u884c\u6bcf\u884c\u4e00\u4e2a\u975e\u8d1f\u6574\u6570N\uff0c\u4ee3\u8868\u4e00\u7ec4\u8be2\u95ee<\/p>\n<p>&nbsp;<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4e00\u5171T\u884c\uff0c\u6bcf\u884c\u4e24\u4e2a\u7528\u7a7a\u683c\u5206\u9694\u7684\u6570ans1,ans2<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>6<br \/>\n1<br \/>\n2<br \/>\n8<br \/>\n13<br \/>\n30<br \/>\n2333<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>1 1<br \/>\n2 0<br \/>\n22 -2<br \/>\n58 -3<br \/>\n278 -3<br \/>\n1655470 2<\/p>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-447 size-full\" src=\"http:\/\/kylelv.com\/wp-content\/uploads\/2018\/01\/1014417-20170405141316128-22697353.png\" alt=\"\" width=\"615\" height=\"259\" srcset=\"https:\/\/blog.kylelv.com\/wp-content\/uploads\/2018\/01\/1014417-20170405141316128-22697353.png 615w, https:\/\/blog.kylelv.com\/wp-content\/uploads\/2018\/01\/1014417-20170405141316128-22697353-300x126.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/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\nusing namespace std;\r\n#define ll long long\r\n#define N 5000555 \r\nll t,tot,pri[N],mu[N];\r\nll phi[N],tp;\r\nbool vs[N];\r\nmap&lt;ll,pair&lt;ll,ll&gt; &gt;m;\r\nvoid sol(ll n,ll &amp;ans1,ll &amp;ans2)\r\n{\r\n    if(n&lt;N){ans1=phi[n];ans2=mu[n];return;}\r\n    if(m.count(n)){ans1=m[n].first;ans2=m[n].second;return;}\r\n    ans1=(ll)n*(n+1)\/2;ans2=1;\r\n    ll last;\r\n    for(ll i=2;i&lt;=n;i=last+1)\r\n    {\r\n        last=n\/(n\/i);\r\n        ll t1=0,t2=0;\r\n        sol(n\/i,t1,t2);\r\n        ans1-=t1*(last-i+1);\r\n        ans2-=t2*(last-i+1);\r\n    }\r\n    m[n]=make_pair(ans1,ans2);\r\n}\r\nint main()\r\n{\r\n    mu[1]=phi[1]=1;\r\n    for(ll i=2;i&lt;N;i++)\r\n    {\r\n        if(!vs[i])\r\n        {\r\n            pri[++tot]=i;\r\n            mu[i]=-1;phi[i]=i-1;\r\n        }\r\n        for(ll j=1;j&lt;=tot;j++)\r\n        {\r\n            tp=i*pri[j];\r\n            if(tp&gt;N) break;\r\n            vs[tp]=1;\r\n            if(i%pri[j]==0)\r\n            {\r\n                phi[tp]=phi[i]*pri[j];\r\n                mu[tp]=0; break;\r\n            }\r\n            mu[tp]=-mu[i];\r\n            phi[tp]=phi[i]*(pri[j]-1);\r\n        }\r\n        phi[i]+=phi[i-1];mu[i]+=mu[i-1];\r\n    }\r\n    scanf(\"%lld\",&amp;t);\r\n    ll n;\r\n    while(t--)\r\n    {\r\n        scanf(\"%lld\",&amp;n);\r\n        if(!n){puts(\"0 0\");continue;}\r\n        ll ans1=0,ans2=0;\r\n        sol(n,ans1,ans2);\r\n        printf(\"%lld %lld\\n\",ans1,ans2);\r\n    }\r\n    return 0;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>3944: Sum Time Limit:\u00a010 Sec\u00a0\u00a0Memory Limit:\u00a0128 MB Desc [&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":[48,19],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-48","tag-19"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/445","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=445"}],"version-history":[{"count":4,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}