Problem B. Harvest of Apples
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1039 Accepted Submission(s): 390
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
题意:给出多次询问,每次询问 给出 n 和 m,求 C(n,0) + C(n,1) + ... + C(n,m)
思路:首先要求的这个东西记为 S(n,m),大佬们给出的公式 S(n,m) = S(n,m-1) + C(n,m),S(n,m) = 2*S(n-1,m) - C(n-1,m)
询问是可以离线的,可以用莫队来优化时间
两个指针 n 和 m 分别表示 S(n,m) 里的 n 和 m
那么左指针右移相当于求 S(n+1,m) = 2 * S(n,m) - C(n,m) 可以转化为----> S(n,m) = 2 * S(n - 1,m) - C(n-1,m)
右指针右移:S(n,m+1) = S(n,m) + c(n,m+1) ------> S(n,m) = S(n,m-1) + S(n,m)
左指针左移:S(n-1,m) = (S(n,m) + C(n-1,m)) / 2
右指针左移:S(n,m-1) = S(n,m) - C(n,m)
由于这里涉及到了除法和取模,所以要用到逆元,预处理做阶乘表 fac[] 的时候,也要做一个阶乘的逆元表 inv[]
所以 C(n,m) = n! / ((n - m)! * m!) = fac[n] * inv[n - m] % mod * inv[m] % mod
上面左指针左移的地方有 / 2,所以还要弄一个 2 的逆元
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
ll fac[maxn]; //阶乘
ll inv[maxn]; //逆元
ll inv2; // 除 2 的逆元
ll ans[maxn]; //答案
ll var; // 活动变量和
int pos[maxn]; //分块
struct node{
int l,r,id;
}nod[maxn];
bool cmp(node a,node b){
if(pos[a.l] == pos[b.l]){
return a.r < b.r;
}
return a.l < b.l;
}
ll Pow(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(){
// 预处理阶乘
fac[0] = fac[1] = 1;
for(int i = 2;i < maxn;i++){
fac[i] = i * fac[i - 1] % mod;
}
//求阶乘的逆元
inv[maxn - 1] = Pow(fac[maxn - 1],mod - 2);
for(int i = maxn - 2;i >= 0;i--){
inv[i] = inv[i + 1] * (i + 1) % mod;
}
inv2 = Pow(2,mod - 2);
}
ll Comb(int n,int m){ //求 C(n,m) = n! / ((n - m)! * m!)
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
void addL(int n,int m){ // s(n,m) = 2 * s(n-1,m) - c(n-1,m)
var = (2 * var % mod - Comb(n-1,m) + mod) % mod;
}
void addR(int n,int m){ // s(n,m) = s(n,m-1) + c(n,m)
var = (var + Comb(n,m)) % mod;
}
void delL(int n,int m){ //s(n-1,m) = (s(n,m) + c(n-1,m)) / 2
var = (var + Comb(n - 1,m)) % mod * inv2 % mod;
}
void delR(int n,int m){ // s(n,m-1) = s(n,m) - c(n,m)
var = (var - Comb(n,m) + mod) % mod;
}
int main(){
int T;
scanf("%d",&T);
int b = sqrt(T);
init();
for(int i = 1;i <= T;i++){
scanf("%d %d",&nod[i].l,&nod[i].r);
pos[i] = i / b;
nod[i].id = i;
}
sort(nod + 1,nod + 1 + T,cmp);
int l = 1,r = 1;
var = 2; // c(1,0) + c(1,1) = 2
for(int i = 1;i <= T;i++){
while(l < nod[i].l){
addL(++l,r);
}
while(r < nod[i].r){
addR(l,++r);
}
while(l > nod[i].l){
delL(l--,r);
}
while(r > nod[i].r){
delR(l,r--);
}
ans[nod[i].id] = var;
}
for(int i = 1;i <= T;i++){
printf("%lld\n",ans[i]);
}
return 0;
}