-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncr.cpp
74 lines (66 loc) · 941 Bytes
/
ncr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
const int N=2e5+5;
const int MOD=1e9+7;
int n, m;
int fact[N], invfact[N];
int pow(int a, int b, int m)
{
int ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
int modinv(int k)
{
return pow(k, MOD-2, MOD);
}
void precompute()
{
fact[0]=fact[1]=1;
for(int i=2;i<N;i++)
{
fact[i]=fact[i-1]*i;
fact[i]%=MOD;
}
invfact[N-1]=modinv(fact[N-1]);
for(int i=N-2;i>=0;i--)
{
invfact[i]=invfact[i+1]*(i+1);
invfact[i]%=MOD;
}
}
int nCr(int x, int y)
{
if(y>x)
return 0;
int num=fact[x];
num*=invfact[y];
num%=MOD;
num*=invfact[x-y];
num%=MOD;
return num;
}
int32_t main()
{
IOS;
precompute();
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
int ans=nCr(n, 2) - nCr(m, 2) + MOD + 1;
ans%=MOD;
cout<<ans<<endl;
}
return 0;
}