Anonymous user
Chinese remainder theorem: Difference between revisions
→{{header|C++}}
(added Crystal implementation (translation of Ruby implementation)) |
|||
Line 341:
=={{header|C++}}==
<lang cpp>
#include <iostream>
#include <numeric>
#include <vector>
#include <execution>
template<typename _Ty> _Ty mulInv(_Ty a, _Ty b) {
▲ int x0 = 0;
▲ int x1 = 1;
if (b == 1) {
Line 358 ⟶ 357:
while (a > 1) {
a = b;
b = amb;
x1 = x0;
x0 = xqx;
Line 375 ⟶ 374:
}
for (int i = 0; i < n.size(); i++) {
sm += a[i] * mulInv(p, n[i]) * p;
}
return sm % prod;
}</lang>
{{out}}
|