HDU 6333(莫队)

Problem B. Harvest of Apples

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


 

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

 

 

Source

2018 Multi-University Training Contest 4

 

挺丢人的,我做的时候一直以为这只是个纯数学题。。。。。。然后卡了4个小时- =

令s(n,m)为答案,打表/推导可得  s(n,m)=s(n-1,m-1)+s(n-1,m)

有了这个式子,意味着对于一个(n,m) 在已知s(n,m)的情况下,s(n,m+1)和s(n+1,m)是可以O(1)求得的

s(n+1,m)=s(n-1,m-1)+s(n-1,m)=2*s(n-1,m)-C(n-1,m)

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

那么,就可以套上莫队搞了。

#include
#define mp make_pair
#define fir first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const int maxn=1e5+10;
const ll mod=1e9+7;
const int maxm=1e6+10;
const double eps=1e-7;
const int inf=0x3f3f3f3f;
const double pi = acos(-1.0);
ll qpow(ll a,ll b,ll p)
{
    ll res=1;
    while (b)
    {
        if(b&1) res=res*a%p;
        b>>=1;
        a=a*a%p;
    }
    return res;
}
ll fac[maxn],facinv[maxn];
void init()
{
    fac[0]=fac[1]=facinv[0]=facinv[1]=1;
    for (int i=2; i=2;i--){
        facinv[i]=(facinv[i+1]*(i+1))%mod;
    }
}
ll c(int n,int m)
{
    if (m<0) return 0;
    if (n

 

你可能感兴趣的:(hdu)