{"id":114,"date":"2010-08-27T08:28:50","date_gmt":"2010-08-27T00:28:50","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=114"},"modified":"2012-03-03T08:29:08","modified_gmt":"2012-03-03T00:29:08","slug":"hoj-2963-count-subset","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hoj-2963-count-subset\/","title":{"rendered":"HOJ 2963. Count Subset"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>  \u5361\u641c\u7d22\uff0c\u5361DP\u7684\u5b50\u96c6\u548c\u95ee\u9898\u3002<br \/>\n  (N<=40, M<=1,000,000,000)\n<!--more--><\/p>\n<h3>Analyse :<\/h3>\n<p>\u5b58\u5728\u7740\u4e00\u79cd\u6298\u8877\u7684\u529e\u6cd5\uff0c\u5373\u5c06\u6298\u534a\u641c\u7d22\u51fa\u4e24\u5f20\u6563\u5217\u8868\uff0c\u518d\u62fc\u63a5\u3002<br \/>\n\u590d\u6742\u5ea6\u662f O(2^20) \uff0c\u52c9\u5f3a\u53ef\u4ee5\u5361\u8fc7\u5b83\u3002<\/p>\n<pre lang=\"cpp\" file=\"HOJ_2963.cpp\">\r\n\/*\r\n    Author: xiaodao\r\n    Prog: HOJ 2963. Count Subset\r\n    Status: Accepted\r\n    Last modifiy: GMT +8 Aug. 29th 17:49\r\n*\/\r\n#include <cstdio>\r\n#include <cstring>\r\nconst int PRIME = 3214567; \/\/4500989\r\nconst int N = 40;\r\nstruct node{\r\n    int k, s;\r\n} hash[PRIME];\r\nint n, step; int a[N];\r\nlong long m, ans;\r\n\r\n\r\n\r\nint h(int k){\r\n\treturn (k % PRIME);\r\n}\r\nint Search(int k){\r\n\tint y = h(k);\r\n\twhile (hash[y].k != k && hash[y].s!=0)\r\n\t\ty = (y+1) % PRIME;\r\n\treturn y;\r\n}\r\nvoid Insert(int k){\r\n\tnode &y = hash[Search(k)];\r\n\ty.k = k; y.s++;\r\n}\r\n\r\n\r\nvoid dfs1(int k, long long s){\r\n    if (s>m) return;\r\n    if (k==step) Insert(s);\r\n    else{\r\n        dfs1(k+1, s+a[k]);\r\n        dfs1(k+1, s);\r\n    }\r\n}\r\n\r\nvoid dfs2(int k, long long s){\r\n    if (s>m) return;\r\n    if (k==step) ans += hash[Search(m-s)].s;\r\n    else{\r\n        dfs2(k+1, s+a[k]);\r\n        dfs2(k+1, s);\r\n    }\r\n}\r\n\r\n\r\nvoid solve(){\r\n    step = n\/2; dfs1(0, 0);\r\n    step = n; dfs2(n\/2, 0);\r\n}\r\n\r\n\r\nvoid init(){\r\n    scanf(\"%d\", &n);\r\n\r\n    int x, y;\r\n    for (int i=0;i<n;i++){\r\n        y = 0; scanf(\"%d.%d\", &#038;x, &#038;y);\r\n        a[i] = x * 1000 + y;\r\n    }\r\n\r\n    y = 0; scanf(\"%d.%d\", &#038;x, &#038;y);\r\n    m = x * 1000 + y;\r\n\r\n    memset(hash, 0, sizeof(hash));\r\n    ans = 0;\r\n}\r\n\r\nint main(){\r\n    int T; scanf(\"%d\", &#038;T);\r\n    while (T--){\r\n        init(); solve();\r\n        printf(\"%lld\\n\", ans);\r\n    }\r\n}\r\n<\/pre>\n<h3>Hit :<\/h3>\n<p>\u6b64\u9898\u5185\u5b58\u4f9b\u5e94\u6bd4\u8f83\u7d27\u5f20\uff0c\u8981\u6ce8\u610f\u4e00\u4e0b\u51e0\u4e2a\u65b9\u9762\u3002<\/p>\n<p>1. iostream \u4f1a\u6d88\u80171000KB\u7684\u7a7a\u95f4\uff0cstdio.c \u6d88\u8017\u7684\u7a7a\u95f4\u6bd4 cstdio \u7684\u7a7a\u95f4\u8981\u591a\u3002<br \/>\n2. \u53ea\u5f00\u4e00\u5f20\u6563\u5217\u8868\u3002\uff08\u4e5f\u5c31\u662f\u7b2c\u4e8c\u6b21\u641c\u7d22\u5230\u53f6\u5b50\u7ed3\u70b9\u7684\u65f6\u5019\u76f4\u63a5\u5728\u5148\u524d\u7684\u6563\u5217\u8868\u4e2d\u67e5\u8be2\u4e86\u3002\uff09<br \/>\n3. \u91c7\u7528\u7b80\u5355\u7684\u95ed\u6563\u5217\u3002\uff08\u5176\u5b9e\u7406\u8bba\u5206\u6790\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u6709\u52a9\u4e8e\u56fa\u5b9a\u5185\u5b58\uff0c\u4f46\u5e94\u8be5\u5bf9\u603b\u7684\u5185\u5b58\u8bbf\u95ee\u91cf\u6ca1\u6709\u5e2e\u52a9\uff0c<br \/>\n\u4e0d\u8fc7\u8003\u8651\u5230\u4e5f\u8bb8\u7cfb\u7edf\u6ca1\u6709\u529e\u6cd5\u7acb\u5373\u91ca\u653e\u6389\u6307\u9488\u7684\u5185\u5b58\u3002\uff09<\/p>\n<p>\u56e0\u6b64\u6211\u624b\u5199\u4e86\u4e00\u4e2a\u7ebf\u6027\u63a2\u67e5\u7684\u95ed\u6563\u5217\uff0c\u6ce8\u610f\u5230\u5982\u679c\u7528STL::MAP\uff0c\u65e0\u8bba\u6211\u600e\u4e48\u5199\u90fd\u8981 MLE \u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u5361\u641c\u7d22\uff0c\u5361DP\u7684\u5b50\u96c6\u548c\u95ee\u9898\u3002 (N<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-1Q","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}