forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRT.cpp
47 lines (47 loc) · 900 Bytes
/
CRT.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
#include <bits/stdc++.h>
using namespace std;
int invMod(int a, int b)
{
int q , r = 1, t1 = 0, t2 = 1, t = 0, a0 = a;
while(r > 0)
{
q = a / b;
r = a % b;
t = t1 - t2 * q;
a = b;
b = r;
t1 = t2;
t2 = t;
}
if(t1 < 0)
t1 += a0;
return t1;
}
int calc(int n[], int r[], int k)
{
int product = 1, p = 1, num = 0;
for(int i = 0; i < k; i++)
product *= n[i];
for (int i = 0; i < k; i++)
{
p = product / n[i];
num += r[i] * invMod(n[i], p) * p;
}
return num % product;
}
int main()
{
cout<<"Number of values to be entered:\n";
int k;
cin>>k;
cout<<"Enter numbers with remainder:\n";
int n[k];
int r[k];
for(int i = 0; i < k; i++)
{
cin>>n[i];
cin>>r[i];
}
printf("The smallest number is %d", calc(n, r, k));
return 0;
}