{"id":36,"date":"2021-06-12T23:28:35","date_gmt":"2021-06-12T15:28:35","guid":{"rendered":"https:\/\/blog.qwq.cafe\/?p=36"},"modified":"2022-04-27T23:54:33","modified_gmt":"2022-04-27T15:54:33","slug":"cf-1536-c-diluc-and-kaeya","status":"publish","type":"post","link":"https:\/\/qwq.cafe\/?p=36","title":{"rendered":"CF-1536-C Diluc and Kaeya"},"content":{"rendered":"<p>\u96be\u5ea6&amp;\u63a8\u8350\u6307\u6570\uff1a<\/p>\n<ul>\n<li>\u601d\u7ef4\u96be\u5ea6\uff1a\u666e\u53ca\/\u63d0\u9ad8- <\/li>\n<li>\u5b9e\u73b0\u96be\u5ea6\uff1a\u666e\u53ca\/\u63d0\u9ad8-<\/li>\n<li>\u63a8\u8350\u6307\u6570\uff1a5.0 \uff08\u63a8\u8350\u6307\u6570\u6700\u9ad8\u4e3a10.0\uff09<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p>\u94fe\u63a5\uff1a<a href=\"https:\/\/codeforces.com\/contest\/1536\/problem\/C\">CF<\/a> \/ <a href=\"https:\/\/www.luogu.com.cn\/problem\/CF1536C\">luogu<\/a><\/p>\n<h2>\u9898\u76ee\u7ffb\u8bd1<\/h2>\n<blockquote>\n<p><strong>Diluc and Kaeya<\/strong><\/p>\n<\/blockquote>\n<p><strong>\u8fea\u5362\u514b\u548c\u51ef\u4e9a<\/strong><\/p>\n<p><del>\u5410\u5608\uff1a\u624d\u770b\u5b8c\u67d0\u5e94\u6025\u98df\u7269\u7684\u9b3c\u755c\u5c31\u770b\u5230\u8fd9\u4e2a\u9898<\/del><\/p>\n<blockquote>\n<p><em>The tycoon of a winery empire in Mondstadt, unmatched in every possible way. A thinker in the Knights of Favonius with an exotic appearance.<\/em><\/p>\n<\/blockquote>\n<p><em>\u8fea\u5362\u514b\uff0c\u8499\u5fb7\u57ce\u7684\u8461\u8404\u9152\u5927\u4ea8\uff0c\u5728\u5404\u79cd\u65b9\u9762\u90fd\u5f88\u65e0\u654c\u3002\u51ef\u4e9a\uff0c\u897f\u98ce\u9a91\u58eb\u56e2\u7684\u4e00\u540d\u601d\u60f3\u5bb6\uff0c\u6709\u7740\u5947\u5f02\u7684\u5916\u8868\u3002<\/em><\/p>\n<blockquote>\n<p>This time, the brothers are dealing with a strange piece of wood marked with their names. This plank of wood can be represented as a string of $n$ characters. Each character is either a &#8216;D&#8217; or a &#8216;K&#8217;. You want to make some number of cuts (possibly $0$ ) on this string, partitioning it into several contiguous pieces, each with length at least $1$ . Both brothers act with dignity, so they want to split the wood as evenly as possible. They want to know the maximum number of pieces you can split the wood into such that the ratios of the number of occurrences of &#8216;D&#8217; to the number of occurrences of &#8216;K&#8217; in each chunk are the same.<\/p>\n<\/blockquote>\n<p>\u73b0\u5728\uff0c\u8fd9\u5bf9\u5144\u5f1f\u9700\u8981\u5904\u7406\u4e00\u5757\u523b\u7740\u4ed6\u4eec\u540d\u5b57\u7684\u5947\u602a\u7684\u6728\u677f\u3002\u8fd9\u4e2a\u6728\u677f\u53ef\u4ee5\u88ab\u4e00\u4e2a\u957f\u5ea6\u4e3a $ n $ \u7684\u5b57\u7b26\u4e32\u4ee3\u66ff\u3002\u6bcf\u4e00\u4e2a\u5b57\u7b26\u53ea\u80fd\u662f &#8216;D&#8217; \u6216\u8005\u662f &#8216;K&#8217; \u3002\u4f60\u60f3\u8981\u5bf9\u8fd9\u4e2a\u5b57\u7b26\u4e32\u8fdb\u884c\u51e0\u6b21\u5206\u5272\uff08\u53ef\u4ee5\u662f $ 0 $ \u6b21\uff09\uff0c\u628a\u5b83\u5206\u6210\u82e5\u5e72\u76f8\u90bb\u7684\u90e8\u5206\uff0c\u6bcf\u4e2a\u90e8\u5206\u7684\u957f\u5ea6\u81f3\u5c11\u662f $ 1 $ \u3002\u8fd9\u5bf9\u5144\u5f1f\u90fd\u5f88\u5c0a\u8d35\uff0c\u6240\u4ee5\u4ed6\u4eec\u60f3\u8981\u5c3d\u53ef\u80fd\u516c\u5e73\u5730\u5206\u5272\u3002\u6240\u4ee5\u5728\u4f60\u5206\u5272\u7684\u6bcf\u5757\u6728\u5934\u91cc &#8216;K&#8217; \u4e0e &#8216;D&#8217; \u7684\u6bd4\u4f8b\u5fc5\u987b\u76f8\u540c\u3002\u73b0\u5728\u4ed6\u4eec\u60f3\u77e5\u9053\u80fd\u5c06\u6728\u677f\u6700\u5927\u5206\u5272\u4e3a\u51e0\u5757\u3002<\/p>\n<blockquote>\n<p>Kaeya, the curious thinker, is interested in the solution for multiple scenarios. He wants to know the answer for every <strong>prefix<\/strong> of the given string. Help him to solve this problem!<\/p>\n<\/blockquote>\n<p>\u51ef\u4e9a\uff0c\u8fd9\u4e2a\u597d\u5947\u7684\u601d\u60f3\u5bb6\uff0c\u5bf9\u591a\u91cd\u53ef\u80fd\u53d1\u751f\u60c5\u51b5\u7684\u89e3\u90fd\u611f\u5174\u8da3\u3002\u4ed6\u60f3\u77e5\u9053\u88ab\u7ed9\u4e0e\u7684\u5b57\u7b26\u4e32\u7684<strong>\u6bcf\u4e00\u4e2a\u524d\u7f00<\/strong>\u7684\u7b54\u6848\u3002\u5e2e\u52a9\u4ed6\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5427\uff01<\/p>\n<blockquote>\n<p>For a string we define a ratio as $a:b$ where &#8216;D&#8217; appears in it $ a $ times, and &#8216;K&#8217; appears $ b $ times. Note that $ a $ or $ b $ can equal $ 0 $, but not both. Ratios $ a:b $ and $ c:d $ are considered equal if and only if $ ad=bc $ .<\/p>\n<\/blockquote>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u6bd4\u4f8b $ a:b $ \u662f &#8216;D&#8217; \u5728\u5176\u4e2d\u51fa\u73b0 $ a $ \u6b21\uff0c&#8217;K&#8217; \u51fa\u73b0 $ b $ \u6b21\u3002\u6ce8\u610f $ a $ \u6216\u8005 $ b $ \u90fd\u53ef\u4ee5\u4e3a\u96f6\u4f46\u4e0d\u80fd\u540c\u65f6\u4e3a\u96f6\u3002\u6bd4\u4f8b $ a:b $ \u548c $ c:d $ \u88ab\u8ba4\u4e3a\u76f8\u540c\u65f6\u5f53\u4e14\u4ec5\u5f53 $ ad=bc $ .<\/p>\n<h2>\u9898\u89e3<\/h2>\n<p>\u7b97\u6cd5\uff1a\u8d2a\u5fc3<\/p>\n<p>\u6570\u636e\u7ed3\u6784\uff1ahash\u8868 \u6216 \u5e73\u8861\u6811\uff08\u8fd9\u91cc\u7684\u5e73\u8861\u6811\u53ef\u4ee5\u7528 <code>std::map<\/code> \u4ee3\u66ff\uff09<\/p>\n<p>\u9898\u76ee\u8ba9\u8f93\u51fa\u5168\u90e8\u524d\u7f00\u7684\u7b54\u6848\uff0c\u6211\u4eec\u5148\u8003\u8651\u5176\u5f31\u5316\u7248\u672c\uff0c\u5373\u53ea\u8003\u8651\u5355\u72ec\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u3002\u663e\u7136\uff0c\u5982\u679c\u6211\u4eec\u5206\u5272\u7684\u5168\u90e8\u5b50\u4e32\u6ee1\u8db3 \u2018D\u2019 \u548c &#8216;K&#8217; \u7684\u6bd4\u4f8b\u76f8\u540c\uff0c\u90a3\u4e48\u5b83\u4eec\u548c\u539f\u5b57\u7b26\u4e32\u7684 &#8216;D&#8217; \u4e0e &#8216;K&#8217; \u7684\u6bd4\u4f8b\u4e5f\u76f8\u540c\u3002\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u60f3\u5230\u4e00\u4e2a\u8d2a\u5fc3\u7b97\u6cd5\uff1a<em>\u626b\u63cf\u8fd9\u4e2a\u5b57\u7b26\u4e32\u4e00\u65e6\u9047\u5230\u4e0e\u539f\u5b57\u7b26\u4e32\u6bd4\u4f8b\u76f8\u540c\u7684\u4e00\u4e2a\u524d\u7f00\u5c31\u5206\u5272\uff0c\u7136\u540e\u628a\u8ba1\u6570\u6e05\u96f6\u518d\u7ee7\u7eed\u626b\u6ca1\u626b\u8fc7\u7684\u5b57\u7b26\u4e32\u3002<\/em><\/p>\n<p>\u5982\u4f55\u8bc1\u660e\u8fd9\u4e2a\u8d2a\u5fc3\uff1f<\/p>\n<p>\u53ef\u4ee5\u8003\u8651\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684\u6700\u77ed\u7684\u548c\u539f\u4e32\u6bd4\u4f8b\u76f8\u540c\u7684\u524d\u7f00\u5982\u679c\u4e0d\u5206\u5272\u80fd\u5426\u66f4\u4f18\uff0c\u7b54\u6848\u663e\u7136\u4e0d\u80fd\u3002\u56e0\u4e3a\u53ea\u6709\u5728\u5b83\u540e\u9762\u518d\u63a5\u4e00\u4e2a\u6bd4\u4f8b\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u624d\u80fd\u6709\u6ee1\u8db3\u9898\u610f\u7684\u5206\u5272\uff0c\u4f46\u8fd9\u6837\u5fc5\u7136\u4f1a\u5bfc\u81f4\u6700\u540e\u7ed3\u679c\u51cf\u5c11\uff0c\u5373\u4e0d\u662f\u6700\u4f18\u3002\u800c\u5728\u8fd9\u4e2a\u6700\u77ed\u524d\u7f00\u540e\u9762\u5206\u5272\u5b8c\u4e4b\u540e\uff0c\u540e\u9762\u7684\u90a3\u4e2a\u5b57\u7b26\u4e32\u53ef\u4ee5\u770b\u505a\u662f\u4e0e\u5f53\u524d\u95ee\u9898\u76f8\u540c\u7684\u5b50\u95ee\u9898\uff08\u9012\u5f52\u601d\u60f3\uff09\uff0c\u56e0\u6b64\u8d2a\u5fc3\u5f97\u8bc1\u3002<\/p>\n<p>\u6211\u4eec\u77e5\u9053\u628a\u4e24\u4e2a\u5206\u5272\u597d\u7684\u5b57\u7b26\u4e32\u91cd\u65b0\u63a5\u5230\u4e00\u8d77\u6bd4\u4f8b\u4e0d\u53d8\uff0c\u518d\u52a0\u4e0a\u6211\u4eec\u521a\u624d\u7684\u8d2a\u5fc3\u7b97\u6cd5\uff0c\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u8f6c\u5316\u4e3a\u67e5\u8be2\u6709\u591a\u5c11\u4e2a\u524d\u7f00\u7684\u6bd4\u4f8b\u548c\u539f\u4e32\u76f8\u540c\u3002\u90a3\u4e48\u5982\u679c\u7528\u4e8c\u7ef4\u6570\u7ec4\u50a8\u5b58\u663e\u7136\u7a7a\u95f4\u4e0d\u591f\uff0c\u6240\u4ee5\u53ef\u4ee5\u8003\u8651\u7528\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u6765\u67e5\u8be2\uff0chash\u8868\u548c\u5e73\u8861\u6811\uff08<code>std::map<\/code>\uff09\u90fd\u53ef\u4ee5\u80dc\u4efb\u3002<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/p>\n<p>hash\u8868\u7684\u7248\u672c $ O(n) $\uff0c\u5e73\u8861\u6811\u7684\u7248\u672c $ O(n\\log n) $<\/p>\n<p>\uff08\u4f7f\u7528 <code>std::map<\/code> \u7684\u7248\u672c\u53ef\u4ee5\u53bb\u6d1b\u8c37\u770b\u5176\u4ed6\u4eba\u7684\u9898\u89e3\uff0c\u56e0\u4e3a\u6709\u522b\u4eba\u5199\u4e86\u6211\u5c31\u6362\u4e00\u4e2a\uff09<\/p>\n<p>\u4ee3\u7801\uff08\u7528hash\u8868\u5b9e\u73b0\u7684\uff09\uff1a<\/p>\n<pre><code class=\"language-cpp\">#include&lt;stdio.h&gt;\n#include&lt;string.h&gt;\n#define MAXN 500003\nstruct hash_node {\n    int x,y;\n    int sum;\n    int next;\n}h[MAXN];\nint tot;\nint head[MAXN];\nint hashcode(int x,int y)\n{\n    return (x*1331+y)%MAXN;\n}\nint add_hash(int x,int y)\n{\n    int code=hashcode(x,y),p;\n    for(p=head[code];p;p=h[p].next)\n    {\n        if(h[p].x==x&amp;&amp;h[p].y==y)\n        {\n            h[p].sum++;\n            return h[p].sum;\n        }\n    }\n    h[++tot]=(hash_node){x,y,1,head[code]};\n    head[code]=tot;\n    return 1;\n}\nint gcd(int x,int y)\n{\n    int r=x%y;\n    while(r)\n    {\n        x=y;\n        y=r;\n        r=x%y;\n    }\n    return y;\n}\nint main()\n{\n    int T,i,n,dsum,ksum,x,y,g;\n    char c;\n    scanf(&quot;%d&quot;,&amp;T);\n    while(T--)\n    {\n        tot=dsum=ksum=0;\n        memset(h,0,sizeof(h));\n        memset(head,0,sizeof(head));\n\n        scanf(&quot;%d&quot;,&amp;n);\n        for(i=0;i&lt;n;i++)\n        {\n            scanf(&quot; %c&quot;,&amp;c);\n            if(c==&#039;D&#039;) dsum++;\n            else ksum++;\n            if(dsum==0) x=0,y=1;\n            else if(ksum==0) x=1,y=0;\n            else\n            {\n                g=gcd(dsum,ksum);\n                x=dsum\/g,y=ksum\/g;\n            }\n            g=add_hash(x,y);\n            printf(&quot;%d &quot;,g);\n        }\n        printf(&quot;\\n&quot;);\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u96be\u5ea6&amp;\u63a8\u8350\u6307\u6570\uff1a \u601d\u7ef4\u96be\u5ea6\uff1a\u666e\u53ca\/\u63d0\u9ad8- \u5b9e\u73b0\u96be\u5ea6\uff1a\u666e\u53ca\/\u63d0\u9ad8- \u63a8\u8350\u6307\u6570\uff1a5.0 \uff08\u63a8\u8350\u6307\u6570\u6700\u9ad8\u4e3a1 [&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":[10],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-4","tag-10"],"_links":{"self":[{"href":"https:\/\/qwq.cafe\/index.php?rest_route=\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qwq.cafe\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qwq.cafe\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qwq.cafe\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qwq.cafe\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=36"}],"version-history":[{"count":2,"href":"https:\/\/qwq.cafe\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/qwq.cafe\/index.php?rest_route=\/wp\/v2\/posts\/36\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/qwq.cafe\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qwq.cafe\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qwq.cafe\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}