2018杭电多校第四场 1002 Harvest of Apples 莫队大法好~

Problem B. Harvest of Apples

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1169    Accepted Submission(s): 443

Problem Description

There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.

Input

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).

Output

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

Sample Input

2

5 2

1000 500

Sample Output

16

924129523

 

莫队大法好~

先找到状态转移

用s(n,m)表示C(n,0)+C(n,1)+...+C(n,m)  那么

s(n,m)=2*s(n-1,m)-c(n-1,m);

s(n,m)=s(n,m-1)+c(n,m);

也就是说知道一个s(n,m)就能O(1)求出s(n+1,m),s(n-1,m),s(n,m+1),s(n,m-1)
 

然后瞎文明搞

注意逆元要先预处理出来

然后,就过了(当然是赛后,赛时还不会莫队,弱小可怜无助还特别能wa)

 

AC代码:

#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 1e5+10;
const long long ha = 1e9+7;
struct node{
    int pos, n, m;
    bool friend operator < (node na, node nb){
        return (na.m md[1000];
long long quickm(long long a, long long b){
    long long ans=1;
    while(b){
        if (b&1){
            ans=ans*a%ha;
        }
        b>>=1;
        a*=a;   a%=ha;
    }
    return ans;
}
void init()
{
    c[1]=1;
    for(int i=2;i<=maxn;i++)
        c[i]=c[i-1]*i%ha;
    inv[100000]=quickm(c[100000],ha-2);
    for (int i=100000-1;i>=1;--i){
        inv[i]=inv[i+1]*((long long)(i+1))%ha;
    }
    return ;
}
long long C(long long n, long long m){
    if (n==m)
        return 1;
    if (m==0)
        return 1;
    return (((c[n]*inv[m])%ha)*inv[n-m])%ha;
}
void read(){
    scanf("%d",&T);
    block=sqrt(maxn);
    int j,k;
    for (int i=0;i md[i][j].n){
                nans = (nans+C(nn-1,nm))*inv[2]%ha;
                nn--;
            }
            while (nm < md[i][j].m){
                nm++;
                nans = ( nans + C(nn,nm) )% ha;
            }
            ans[md[i][j].pos]=nans;
        }
    }
    for (int i=0;i

 

你可能感兴趣的:(莫队,暴力)