Problem B. Harvest of Apples
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2177 Accepted Submission(s): 847
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
Recommend
chendu | We have carefully selected several similar problems for you: 6343 6342 6341 6340 6339
分析:将n分成block块预处理一下。由杨辉三角推得以下公式。
下面就是针对以上式子进行分块了。
1、先说一下分块思想。首先,如果不分块预处理的话那么可以暴力1~n,逐个求和。分块就是不爆力1~n,我们每隔block(block一般为根号下n)就暴力求一次求和。对于当前n我们找一个离着n最近的已经被分块暴力出的答案,由这个答案向n转移。这样复杂度就降低了。当然对于小于block的n直接暴力求就可以了。
2、用数组sum[i][j]代表C(n,j)的前缀和。i是从block开始加每次增加block。
3、用d数组预处暴力求得n<block的时候答案。
4、分情况讨论,n<block直接输出d[n][m],n%block==0直接输出sum[n][m]。其它情况,先找到里n最近的已经被预处理出的值,即sum[n/block][m]。然后根据上图中第二行推得的公式递推至n得到答案。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int n,m,block=333;
int fac[MAX+3];
int inv[MAX+3];
int sum[100010/333+1][MAX];
int d[400][400];
void init()
{
fac[0]=1;
for(int i=1;i<=MAX;i++) fac[i]=fac[i-1]*1ll*i%mod;
inv[MAX]=qpow(fac[MAX],mod-2);
for(int i=MAX-1;~i;i--) inv[i]=inv[i+1]*1ll*(i+1)%mod;
}
int C(int n,int m)
{
if(m>n) return 0;
if(m==n || m==0) return 1;
return fac[n]*1ll*inv[n-m]%mod*inv[m]%mod;
}
void fk()
{
for(int i=block;i<MAX;i+=block)
{
ll temp=0;
for(int j=0;j<MAX;j++)
{
temp=(temp+C(i,j))%mod;
sum[i/block][j]=temp;
}
}
for(int i=1;i<block;i++)
{
ll temp=0;
for(int j=0;j<=i;j++)
{
temp=(temp+C(i,j))%mod;
d[i][j]=temp;
}
}
}
int main()
{
int t;
init();
fk();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
ll ans=0;
if(n<block) ans=d[n][m];
else if(n%block==0) ans=sum[n/block][m];
else
{
ans=2*sum[n/block][m]%mod;
ans=(ans-C(n/block*block,m))%mod;
for(int i=n/block*block+2;i<=n;i++)
{
ans=2*ans%mod-C(i-1,m);
if(ans<0) ans+=mod;
}
}
printf("%lld\n",(ll)(ans+mod)%mod);
}
return 0;
}