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.
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).
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
5 2
1000 500
Sample Output
16
924129523
924129523
Source
解析:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
const int MOD = 1e9+;
LL n, m, ans;
LL up[maxn], down[maxn], pos[maxn], inc[maxn], inv[maxn]; struct node
{
LL l, r;
int id;
}Node[maxn]; bool cmp(node a, node b)
{
return pos[a.l] == pos[b.l] ? (a.r < b.r) : (a.l < b.l);
} LL qp(LL a, LL b)
{
LL res = ;
while(b)
{
if(b & ) res = res * a % MOD;
a = a * a % MOD;
b >>= ;
}
return res;
} void init()
{
up[] = ;
down[] = ;
for(int i=; i<maxn; i++)
{
up[i] = up[i-] * i % MOD;
down[i] = qp(up[i], MOD - ) % MOD;
}
} LL C(LL n, LL m)
{
if(n < m) return ;
return up[n] * down[n-m] % MOD * down[m] % MOD;
} int main()
{
init();
int block = sqrt();
for(int i=; i<=; i++)
pos[i] = (i-)/block + ;
int T;
rd(T);
for(int i=; i<=T; i++)
{
rlld(Node[i].r), rlld(Node[i].l);
Node[i].id = i;
}
sort(Node + , Node + T + , cmp);
ans = ;
int tmp = qp(, MOD - );
for(int i=, l=, r=; i<=T; i++)
{
for(; r < Node[i].r; r++)
ans = ( * ans - C(r, l) + MOD) % MOD;
for(; r > Node[i].r; r--)
ans = (ans + C(r-, l)) * tmp % MOD;
for(; l < Node[i].l; l++)
ans = (ans + C(r, l+)) % MOD;
for(; l > Node[i].l; l--)
ans = (ans - C(r, l) + MOD) % MOD;
if(Node[i].l == Node[i].r)
{
inc[Node[i].id] = ;
} inc[Node[i].id] = ans;
}
for(int i=; i<=T; i++)
printf("%lld\n", inc[i]); return ;
}