From 691cbdb1f514f5bbd295b97c5d8123f63a9e62fe Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Wed, 27 May 2020 15:06:36 -0400 Subject: [PATCH] fix code for generic types --- math/extended_euclid_algorithm.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/math/extended_euclid_algorithm.cpp b/math/extended_euclid_algorithm.cpp index 3db14802f..829db4336 100644 --- a/math/extended_euclid_algorithm.cpp +++ b/math/extended_euclid_algorithm.cpp @@ -4,25 +4,27 @@ // This is also used in finding Modular multiplicative inverse of a number. // (A * B)%M == 1 Here B is the MMI of A for given M, // so extendedEuclid (A, M) gives B. +template +void extendedEuclid(T A, T B, T *GCD, T2 *x, T2 *y) { + if (B > A) std::swap(A, B); // Ensure that A >= B -int d, x, y; -void extendedEuclid(int A, int B) { if (B == 0) { - d = A; - x = 1; - y = 0; + *GCD = A; + *x = 1; + *y = 0; } else { - extendedEuclid(B, A%B); - int temp = x; - x = y; - y = temp - (A/B)*y; + extendedEuclid(B, A % B, GCD, x, y); + T2 temp = *x; + *x = *y; + *y = temp - (A / B) * (*y); } } int main() { - int a, b; + uint32_t a, b, gcd; + int32_t x, y; std::cin >> a >> b; - extendedEuclid(a, b); - std::cout << x << " " << y << std::endl; + extendedEuclid(a, b, &gcd, &x, &y); + std::cout << gcd << " " << x << " " << y << std::endl; return 0; }