{"id":367,"date":"2018-08-02T17:10:54","date_gmt":"2018-08-02T17:10:54","guid":{"rendered":"https:\/\/www.haoyuan.info\/?p=367"},"modified":"2018-08-02T17:10:54","modified_gmt":"2018-08-02T17:10:54","slug":"hdu-6333-problem-b-harvest-of-apples-%e6%9d%ad%e7%94%b5%e5%a4%9a%e6%a0%a1%e7%ac%ac%e5%9b%9b%e5%9c%ba","status":"publish","type":"post","link":"https:\/\/haoyuan.info\/?p=367","title":{"rendered":"HDU 6333 Problem B. Harvest of Apples \u676d\u7535\u591a\u6821\u7b2c\u56db\u573a"},"content":{"rendered":"<h2>\u9898\u9762<\/h2>\n<p>There are n apples on a tree, numbered from 1 to n.<br \/>\nCount the number of ways to pick at most m apples.<\/p>\n<h2>\u9898\u610f<\/h2>\n<p>\u5b9a\u4e49 $S(n, m) = \\sum_{i = 0} ^ {m} {n \\choose i}$\uff0c\u4e0d\u96be\u53d1\u73b0 $S(n, m) = S(n, m &#8211; 1) + {n \\choose m}, S(n, m) = 2S(n &#8211; 1, m) &#8211; {n &#8211; 1 \\choose m}$\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u77e5\u9053 $S(n, m)$\uff0c\u5c31\u80fd\u4ee5 $O(1)$ \u7684\u4ee3\u4ef7\u8ba1\u7b97\u51fa $S(n &#8211; 1, m), S(n,  m &#8211; 1), S(n + 1, m), S(n, m + 1)$\uff0c\u53ef\u4ee5\u91c7\u7528\u83ab\u961f\u7b97\u6cd5\u3002<\/p>\n<p>\u8fd9\u9053\u9898\u4e0d\u662f\u5355\u7eaf\u66b4\u529b\u53ef\u4ee5\u8003\u8651\u7684\uff0c\u4e5f\u4e0d\u662f\u7528\u4ec0\u4e48\u9ed1\u79d1\u6280\u6765\u63d0\u901f\u3002<\/p>\n<p>\u7531\u4e8e\u9898\u76ee\u8303\u56f4\u6bd4\u8f83\u5927\uff0c\u4e0d\u59a8\u8003\u8651\u662f\u5426\u53ef\u4ee5\u51cf\u5c11\u66b4\u529b\u8ba1\u7b97\u7ec4\u5408\u6570\u7684\u8fc7\u7a0b\u3002<br \/>\n\u56e0\u6b64\u6211\u4eec\u8003\u8651\u901a\u8fc7\u83ab\u961f\u7b97\u6cd5\uff0c\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898<\/p>\n<h2>AC\u4ee3\u7801<\/h2>\n<pre><code class=\" line-numbers\">#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\n#include &lt;vector&gt;\n#include &lt;cmath&gt;\n#include &lt;cstring&gt;\nusing namespace std;\n\nconst int N = 200000;\nconst int MOD = 1000000007;\nstruct query\n{\n    int n, k, i;\n} Q[N];\nint T;\nvector &lt;query&gt; lst[N];\nint cnt, mx, chunk;\nint fac[N], inv[N], res[N], in_chunk[N];\nint powi(int a, int b)\n{\n    int c = 1;\n    for (; b; b &gt;&gt;= 1, a = 1ll * a * a % MOD)\n        if (b &amp; 1) c = 1ll * c * a % MOD;\n    return c;\n}\nint C(int a, int b)\n{\n    return 1ll * fac[a] * inv[b] % MOD * inv[a - b] % MOD;\n}\nint comp(query a, query b)\n{\n    return a.n &lt; b.n;\n}\nint main()\n{\n    mx = 100000;\n    fac[0] = 1; for (int i = 1; i &lt;= mx; ++ i) fac[i] = 1ll * fac[i - 1] * i % MOD;\n    inv[mx] = powi(fac[mx], MOD - 2); for (int i = mx - 1; ~i; -- i) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;\n    chunk = sqrt(mx);\n    cnt = 1;\n    for (int i = 1; i &lt;= mx; i += chunk, ++ cnt)\n        for (int j = i; j &lt; i + chunk &amp;&amp; j &lt;= mx; ++ j)\n            in_chunk[j] = cnt;\n    cnt --;\n    scanf(\"%d\", &amp;T);\n    for (int i = 1; i &lt;= T; ++ i)\n    {\n        scanf(\"%d%d\", &amp;Q[i].n, &amp;Q[i].k), Q[i].i = i;\n        lst[in_chunk[Q[i].k]].push_back(Q[i]);\n    }\n    for (int i = 1; i &lt;= cnt; ++ i) if (lst[i].size())\n        {\n            sort(lst[i].begin(), lst[i].end(), comp);\n            int val = 0, in = lst[i][0].n, ik = -1;\n            for (int j = 0; j &lt; lst[i].size(); ++ j)\n            {\n                while (in &lt; lst[i][j].n) val = (0ll + val + val + MOD - C(in ++, ik)) % MOD;\n                while (ik &lt; lst[i][j].k) val = (val + C(in, ++ ik)) % MOD;\n                while (ik &gt; lst[i][j].k) val = (val + MOD - C(in, ik --)) % MOD;\n                res[lst[i][j].i] = val;\n            }\n        }\n    for (int i = 1; i &lt;= T; ++ i) printf(\"%d\\n\", res[i]);\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u9762 There are n apples on a tree, numbered from 1 to n.  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-1"],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8UC2c-5V","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":5,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":373,"href":"https:\/\/haoyuan.info\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions\/373"}],"wp:attachment":[{"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haoyuan.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}