-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmath.cpp
126 lines (102 loc) · 2.89 KB
/
math.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
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
// template starts
using namespace __gnu_pbds; //required
using namespace std;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// ordered_set <int> s;
// s.find_by_order(k); returns the (k+1)th smallest element
// s.order_of_key(k); returns the number of elements in s strictly less than k
#define MOD (1000000000+7) // change as required
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(), x.end()
#define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;
#define input(vec,N) for(int i = 0; i < (N); i++) cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;
#define leftmost_bit(x) (63-__builtin_clzll(x))
#define rightmost_bit(x) __builtin_ctzll(x)
#define set_bits(x) __builtin_popcountll(x)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long int ll;
// start of highly risky #defines
#define int ll // disable when you want to make code a bit faster
#define endl '\n' // disable when dealing with interactive problems
// End of highly risky #defines
ll power(ll a, ll b, ll m){
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll inv(ll a, ll p){
return power(a,p-2,p);
}
const int MAX = 2e5 + 1;
vector<ll> fact, invfact;
ll nCr(int n, int r){
if(n < r || n < 0 || r < 0) return 0;
ll ans = (fact[n]*invfact[r])%MOD;
ans *= invfact[n-r];
ans %= MOD;
return ans;
}
vector<vector<ll> > multiply(vector<vector<ll> > &a, vector<vector<ll> > &b, ll p){
int r1,c1,r2,c2;
r1 = a.size(); c1 = a[0].size();
r2 = b.size(); c2 = b[0].size();
vector<vector<ll> > result(r1,vector<ll>(c2,0));
if(c1 != r2) return result;
for(int i = 0; i < r1; i++){
for(int j = 0; j < c2; j++){
for(int k = 0; k < c1; k++){
result[i][j] += a[i][k]*b[k][j];
result[i][j] %= p;
}
}
}
return result;
}
vector<vector<ll> > power(vector<vector<ll> >&a,ll n, ll p){
int r = a.size();
vector<vector<ll> > result(r,vector<ll>(r,0));
for(int i = 0; i < r; i++) result[i][i] = 1;
while(n > 0){
if(n%2 == 1){
result = multiply(result,a,p);
}
n /= 2;
a = multiply(a,a,p);
}
return result;
}
// template ends here
void solve(){
// code starts from here
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
fact.resize(MAX);
invfact.resize(MAX);
fact[0] = invfact[0] = 1;
for(int i = 1; i < MAX; i++){
fact[i] = (fact[i-1]*i)%MOD;
invfact[i] = inv(fact[i], MOD);
}
int T;
cin >> T;
//T = 1;
while(T--){
solve();
}
return 0;
}