Harvest of Apples

來源:互聯網
上載者:User

標籤:pos   ref   first   add   red   分享圖片   ++   gif   ide   

問題 B: Harvest of Apples時間限制: 1 Sec  記憶體限制: 128 MB
提交: 18  解決: 11
[提交] [狀態] [討論版] [命題人:admin]題目描述There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.

 

輸入The 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).

 

輸出For each test case, print an integer representing the number of ways modulo 109+7.

 

範例輸入
25 21000 500

 

範例輸出
16924129523

 莫隊(分塊),組合數學。

AC代碼:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn=1e5+10;const int mod=1e9+7;ll fac[maxn],inv[maxn],ans[maxn];ll rev2,res;int pos[maxn];ll qpow(ll b,int n){    ll res=1;    while(n)    {        if(n&1) res=res*b%mod;        b=b*b%mod;        n>>=1;    }    return res;}ll Comb(int n,int k){    return fac[n]*inv[k]%mod*inv[n-k]%mod;}void pre(){    rev2=qpow(2,mod-2);    fac[0]=fac[1]=1;    for(int i=2; i<maxn; ++i) fac[i]=i*fac[i-1]%mod;    inv[maxn-1]=qpow(fac[maxn-1],mod-2);    for(int i=maxn-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;}struct Query{    int L,R,id;    bool operator <(const Query& p) const    {        if(pos[L]==pos[p.L]) return R<p.R;        return L<p.L;    }} Q[maxn];inline void addN(int posL,int posR){    res=(2*res%mod-Comb(posL-1,posR)+mod)%mod;}inline void addM(int posL,int posR){    res=(res+Comb(posL,posR))%mod;}inline void delN(int posL,int posR){    res=(res+Comb(posL-1,posR))%mod*rev2%mod;}inline void delM(int posL,int posR){    res=(res-Comb(posL,posR)+mod)%mod;}int main(){    int T,curL,curR;    int block=(int)sqrt(1.0*maxn);    pre();    scanf("%d",&T);    for(int i=1;i<=T;++i)    {        scanf("%d %d",&Q[i].L,&Q[i].R);        pos[i]=i/block;        Q[i].id=i;    }    sort(Q+1,Q+T+1);    res=2;    curL=1,curR=1;    for(int i=1;i<=T;++i)    {        while(curL<Q[i].L) addN(++curL,curR);        while(curR<Q[i].R) addM(curL,++curR);        while(curL>Q[i].L) delN(curL--,curR);        while(curR>Q[i].R) delM(curL,curR--);        ans[Q[i].id] = res;    }    for(int i=1;i<=T;++i) printf("%lld\n",ans[i]);    return 0;}
View Code

 

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.