forked from arnab2001/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codeforces 745 div 2 problem A solution.cpp
187 lines (140 loc) · 3.63 KB
/
Codeforces 745 div 2 problem A solution.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include<bits/stdc++.h>
using namespace std;
#define endl "\n" /*for interactive problems remove this line*/
/*
-for ordered set problems policy based data sucture header
//PBDS
*/
typedef long long ll;
ll mod = pow(10, 9) + 7;
typedef unsigned long long ull;
int gcdExtended(int a, int b, int *x, int *y);
// Function to find modulo inverse of b. It returns
// -1 when inverse doesn't
int modInverse(int b, int m)
{
int x, y; // used in extended GCD algorithm
int g = gcdExtended(b, m, &x, &y);
// Return -1 if b and m are not co-prime
if (g != 1)
return -1;
// m is added to handle negative x
return (x % m + m) % m;
}
int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b % a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
ll sumbincoef( int N, int k ) {
ll bincoef = 1, sum = 1;
int i;
for ( i = 1 ; i < k ; i++ ) {
ll val=((N - i + 1)%mod *modInverse( i,mod))%mod;
bincoef = (bincoef%mod * val)%mod;
if(i%2==0)
sum = (sum+bincoef)%mod;
}
return sum%mod;
}
const int N = 200000 + 10;
ll factorialNumInverse[N + 1];
// aay to precompute inverse of 1! to N!
ll naturalNumInverse[N + 1];
// aay to store factorial of first N numbers
ll fact[N + 1];
// Function to precompute inverse of numbers
void InverseofNumber(ll p)
{
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for (int i = 2; i <= N; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}
// Function to precompute inverse of factorials
void InverseofFactorial(ll p)
{
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// precompute inverse of natural numbers
for (int i = 2; i <= N; i++)
factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to N
void factorial(ll p)
{
fact[0] = 1;
// precompute factorials
for (int i = 1; i <= N; i++) {
fact[i] = (fact[i - 1] * i) % p;
}
}
// Function to return nCr % p in O(1) time
ll Binomial(ll N, ll R, ll p)
{
// n C r = n!*inverse(r!)*inverse((n-r)!)
ll ans = ((fact[N] * factorialNumInverse[R])
% p * factorialNumInverse[N - R])
% p;
return ans;
}
typedef unsigned long long ull;
typedef unsigned int ui;
/*IMP snippets
//PBDS -for ordered set problems policy based data sucture header
//binaryToDecimal
//decimalToBinary
//nCrModp
//is_prime
//segtree*/
int parent[N],ran[N];
void make_set(int v) {
parent[v] = v;
ran[v]=0;
}
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v]=find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b){
if(ran[a]<ran[b])swap(a,b);
parent[b] = a;
if(ran[a]==ran[b])ran[a]++;
}
}
int main()
{ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll p=mod;
InverseofNumber(p);
InverseofFactorial(p);
factorial(p);
int t=1;
cin>>t;
while(t>0)
{
int n;
cin>>n;
ll ans=fact[2*n];
ans=ans*naturalNumInverse[2];
cout<<ans%mod<<endl;
t--;
}
}