hdu多校第4場 B Harvest of Apples(莫隊)

來源:互聯網
上載者:User

標籤:技術   space   sample   time   mes   while   class   for   info   

Problem B. Harvest of ApplesTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1600    Accepted Submission(s): 604Problem DescriptionThere are n apples on a tree, numbered from 1 to n.Count the number of ways to pick at most m apples. InputThe first line of the input contains an integer T (1≤T≤105) denoting the number of test cases.Each test case consists of one line with two integers n,m (1≤m≤n≤105). OutputFor each test case, print an integer representing the number of ways modulo 109+7. Sample Input25 21000 500 Sample Output16924129523 Source2018 Multi-University Training Contest 4 Recommendchendu   |   We have carefully selected several similar problems for you:  6343 6342 6341 6340 6339 

求C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);

設S(n,m)=C(n,0)+C(n,1)+C(n,2)+.....+C(n,m);

 

第一個式子易得,第二個式子:楊輝三角的 n,m=(n-1,m)+(n-1,m-1)

那麼就是這一行等於上一行的都用了2次,只有第最後一個用了一次

所以減去c(n-1,m)

#include<iostream>#include<stdio.h>#include<cmath>#include<algorithm>using namespace std;const int mod=1e9+7;#define ll long longconst int maxn=1e5+7;ll jiecheng[maxn],inv[maxn];ll ans[maxn];int block;ll qsm(ll a,ll b){    ll ans=1;    while(b){        if(b&1)            ans=ans*a%mod;        a=a*a%mod;        b>>=1;    }    return ans;}void init(){    jiecheng[1] = 1;    for(int i = 2; i < maxn; i++)        jiecheng[i] = jiecheng[i-1] * i % mod;    for(int i = 1; i < maxn; i++)        inv[i] = qsm(jiecheng[i], mod-2);}struct node{    int l,r;    int i;}modui[maxn];bool cmp(node a,node b){    if(a.l/block==b.l/block)        return a.r<b.r;    return a.l<b.l;}ll C(ll n,ll m){        if(m == 0 || m == n) return 1;    ll ans=1;    ans=(jiecheng[n]*inv[m])%mod*inv[n-m];    ans=ans%mod;    return ans;}int main(){    init();    block = sqrt(maxn);    int t;    scanf("%d",&t);    for(int i=0;i<t;i++)    {        scanf("%d%d",&modui[i].l,&modui[i].r);        modui[i].i=i;    }    sort(modui,modui+t,cmp);    int l=1,r=0;    int sum=1;    for(int i = 0; i < t; i++)    {        while(l < modui[i].l) sum = (2 * sum - C(l++, r) + mod) % mod;        while(l > modui[i].l) sum = ((sum + C(--l, r))*inv[2]) % mod;        while(r < modui[i].r) sum = (sum + C(l, ++r)) % mod;        while(r > modui[i].r) sum = (sum - C(l, r--) + mod) % mod;        ans[modui[i].i] = sum;    }    for(int i=0;i<t;i++)    {        printf("%lld\n",ans[i]);    }        return 0;}

 

hdu多校第4場 B Harvest of Apples(莫隊)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.