-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchinese_theorm.cpp
More file actions
59 lines (39 loc) · 1.62 KB
/
chinese_theorm.cpp
File metadata and controls
59 lines (39 loc) · 1.62 KB
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
Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
We are given two arrays num[0..k-1] and rem[0..k-1]. In num[0..k-1], every pair is coprime (gcd for every pair is 1). We need to find minimum positive number x such that:
x % num[0] = rem[0],
x % num[1] = rem[1],
.......................
x % num[k-1] = rem[k-1]
Example:
Input: num[] = {3, 4, 5}, rem[] = {2, 3, 1}
Output: 11
Explanation:
11 is the smallest number such that:
(1) When we divide it by 3, we get remainder 2.
(2) When we divide it by 4, we get remainder 3.
(3) When we divide it by 5, we get remainder 1.
We strongly recommend to refer below post as a prerequisite for this.
Chinese Remainder Theorem | Set 1 (Introduction)
We have discussed a Naive solution to find minimum x. In this article, an efficient solution to find x is discussed.
The solution is based on below formula.
x = ( ∑ (rem[i]*pp[i]*inv[i]) ) % prod
Where 0 <= i <= n-1
rem[i] is given array of remainders
prod is product of all given numbers
prod = num[0] * num[1] * ... * num[k-1]
pp[i] is product of all divided by num[i]
pp[i] = prod / num[i]
inv[i] = Modular Multiplicative Inverse of
pp[i] with respect to num[i]
Example:
Let us take below example to understand the solution
num[] = {3, 4, 5}, rem[] = {2, 3, 1}
prod = 60
pp[] = {20, 15, 12}
inv[] = {2, 3, 3} // (20*2)%3 = 1, (15*3)%4 = 1
// (12*3)%5 = 1
x = (rem[0]*pp[0]*inv[0] + rem[1]*pp[1]*inv[1] +
rem[2]*pp[2]*inv[2]) % prod
= (2*20*2 + 3*15*3 + 1*12*3) % 60
= (40 + 135 + 36) % 60
= 11