{"id":138,"date":"2017-03-14T09:10:57","date_gmt":"2017-03-14T09:10:57","guid":{"rendered":"http:\/\/www.luo666.com\/?p=138"},"modified":"2019-01-19T04:43:31","modified_gmt":"2019-01-19T04:43:31","slug":"topcoder-yetanotherorproblem2-py","status":"publish","type":"post","link":"http:\/\/www.luo666.com\/?p=138","title":{"rendered":"topcoder: YetAnotherORProblem2"},"content":{"rendered":"<p>\u8fd9\u9898\u663e\u793a\u53ea\u670918%\u7684AC\u7387\uff0c\u9898\u76ee\u672c\u8eab\u4e0d\u96be\uff0c\u4f30\u8ba1\u90fd\u662f\u8d25\u5728\u6267\u884c\u65f6\u95f4\u4e0a\u4e86\uff1b\u6211\u7684\u89e3\u6cd5\u5982\u4e0b\uff0c\u5178\u578b\u7684DP\u601d\u7ef4\uff0c\u5b50\u95ee\u9898\u662f\uff1a\u5982\u679c\u5df2\u77e5\u957f\u5ea6\u4e3an-1\u7684\u60c5\u51b5\u4e0b\uff0c\u548c\u6216\u64cd\u4f5c\u76f8\u7b49\u7684\u6570\u5217\uff0c\u6c42\u957f\u5ea6\u4e3an\u7684\u60c5\u51b5\uff1b\u72b6\u6001index\u4e3a\u6bcf\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u5217\u7684\u548cSn-1\uff08\u56e0\u4e3a\u548c\u6216\u76f8\u7b49\uff0c\u4e5f\u662f\u8be5\u6570\u5217\u7684\u6216\uff09\uff0c\u72b6\u6001\u503c\u4e3a\u8be5index\u5bf9\u5e94\u7684\u6570\u5217\u6709\u591a\u5c11\u79cd\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a:<\/p>\n<p>if Xn | Sn-1 == Xn + Sn-1: then Stat[Xn|Sn-1] += Stat[Sn-1]<\/p>\n<pre class=\"lang:default decode:true \">class YetAnotherORProblem2:\n    def countSequences(self, n, r):\n        ls = {0:1}\n        c = 0\n        mc = 1\n        while mc &lt; r:\n            mc &lt;&lt;= 1\n        mc -= 1\n\n        for i in range(0, n+1):\n            if i == n:\n                for k,v in ls.items():\n                    c += v\n                return c%1000000009\n            cs = {0:0}\n            for k,v in ls.items():\n                mask = mc^k\n                # \u8fd9\u91cc\u662f\u4e2a\u91cd\u8981\u7684\u4f18\u5316\uff0c\u5927\u4e8emask+1\u7684\u503c\u4e0d\u53ef\u80fd\u6ee1\u8db3\u6761\u4ef6\n                for j in range(0, mask+1):\n                    if k &amp; j == 0:\n                        cs[k+j] =  cs.get(k+j, 0) + v\n            ls = cs.copy()<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u9898\u663e\u793a\u53ea\u670918%\u7684AC\u7387\uff0c\u9898\u76ee\u672c\u8eab\u4e0d\u96be\uff0c\u4f30\u8ba1\u90fd\u662f\u8d25\u5728\u6267\u884c\u65f6\u95f4\u4e0a\u4e86\uff1b\u6211\u7684\u89e3\u6cd5\u5982\u4e0b\uff0c\u5178\u578b\u7684DP\u601d\u7ef4\uff0c\u5b50\u95ee\u9898\u662f\uff1a\u5982\u679c\u5df2\u77e5\u957f\u5ea6\u4e3an-1\u7684\u60c5\u51b5\u4e0b\uff0c\u548c\u6216\u64cd\u4f5c\u76f8\u7b49\u7684\u6570\u5217\uff0c\u6c42\u957f\u5ea6\u4e3an\u7684\u60c5\u51b5\uff1b\u72b6\u6001index\u4e3a\u6bcf\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u5217\u7684\u548cSn-1\uff08\u56e0\u4e3a\u548c\u6216\u76f8\u7b49\uff0c\u4e5f\u662f\u8be5\u6570\u5217\u7684\u6216\uff09\uff0c\u72b6\u6001\u503c\u4e3a\u8be5index\u5bf9\u5e94\u7684\u6570\u5217\u6709\u591a\u5c11\u79cd\u3002\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a: if Xn | Sn-1 == Xn + Sn-1: then Stat[Xn|Sn-1] += Stat[Sn-1] class YetAnotherORProblem2: def countSequences(self, n, r): ls = {0:1} c = 0 mc = 1 while mc &lt; r: mc &lt;&lt;= 1 mc -= 1 for i in range(0, n+1): if i == n: for k,v in ls.items(): c += v return &hellip; <a href=\"http:\/\/www.luo666.com\/?p=138\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;topcoder: YetAnotherORProblem2&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[2],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7Dhki-2e","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/138"}],"collection":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138"}],"version-history":[{"count":2,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":148,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions\/148"}],"wp:attachment":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}