MeetCode
Chinese Remainder (Two)
C
C++
Computer Mathematics
CSS
DSA
HTML
Java
JavaScript
NodeJS
PHP
Programming Interview
Python
ReactJS
SQL
Run
Submit
Dark
Problem Statement
Solutions
Submissions
Chinese Remainder (Two)
Hard
Computer Mathematics
Modular Arithmetic
28 views
Explanation
Complexity
Problem Statement
Solve x ≡ a (mod m), x ≡ b (mod n) when m and n are coprime.
Input Format
Four integers a m b n.
Output Format
One integer smallest x.
Example
Input
2 3 3 5
Output
8
Constraints
m,n coprime
Input / Output Format
Input Format
Four integers a m b n.
Output Format
One integer smallest x.
Constraints
m,n coprime
Examples
Input:
2 3 3 5
Output:
8
Example Solution (Public)
Computer Mathematics
Use CRT: find inv of m mod n, t=((b-a) mod n)*inv mod n, x=a+m*t, then mod lcm(m,n).
Official Solution Code
Wrap
Copy
Use CRT: find inv of m mod n, t=((b-a) mod n)*inv mod n, x=a+m*t, then mod lcm(m,n).
Please
login
to submit solutions.
Editor
C
C++
Computer Mathematics
CSS
DSA
HTML
Java
JavaScript
NodeJS
PHP
Programming Interview
Python
ReactJS
SQL
Test Cases
Output
Submission Result
Input
2 3 3 5
Expected Output
8
Compile Output
Error
Output
Please
login
to submit solutions.