-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrt.sublime-snippet
54 lines (48 loc) · 1.24 KB
/
crt.sublime-snippet
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
<snippet>
<content><![CDATA[
int x,y,GCD;
void extendedEuclid(int a,int b){
// To calculate Ax + By = C where C is a mulitple of divior of A and B (K*gcd(A,B))
if(b==0){
x = 1; y = 0;GCD =a;
return;
}
extendedEuclid(b,a%b);
int x1 = y;
int y1 = x - (a/b)*y;
x = x1;
y = y1;
// Multiply by K to get the final answer
}
int modularInverse(int a,int m){
extendedEuclid(a,m);
return (x+m)%m;
}
int CRT(vector<int> num, vector<int> rem){
int prod=1ll;
vector<int> pp;
pp.clear();
for(int i=0;i<num.size();i++){
prod*=num[i];
}
for(int i=0;i<num.size();i++){
pp.push_back(prod/num[i]);
}
vector<int> inv;
inv.clear();
for(int i=0;i<pp.size();i++){
inv.pb(modularInverse(pp[i],num[i]));
}
int ans=0ll;
for(int i=0;i<inv.size();i++){
ans+=(((inv[i]*rem[i])%prod)*(pp[i]%prod))%prod;
ans%=prod;
}
return ans;
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>crt</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>