diff options
| -rw-r--r-- | zmodn.h | 22 |
1 files changed, 3 insertions, 19 deletions
| @@ -46,26 +46,10 @@ private: | |||
| 46 | int64_t int64; | 46 | int64_t int64; |
| 47 | }; | 47 | }; |
| 48 | 48 | ||
| 49 | void swapdiv(int64_t& oldx, int64_t& x, int64_t q) { | ||
| 50 | int64_t tmp = x; | ||
| 51 | x = oldx - q*tmp; | ||
| 52 | oldx = tmp; | ||
| 53 | } | ||
| 54 | |||
| 55 | std::tuple<int64_t, int64_t, int64_t> extended_gcd(int64_t a, int64_t b) { | 49 | std::tuple<int64_t, int64_t, int64_t> extended_gcd(int64_t a, int64_t b) { |
| 56 | int64_t oldr = a; | 50 | if (b == 0) return {a, 1, 0}; |
| 57 | int64_t r = b; | 51 | auto [g, x, y] = extended_gcd(b, a%b); |
| 58 | int64_t olds = 1; | 52 | return {g, y, x - y*(a/b)}; |
| 59 | int64_t s = 0; | ||
| 60 | int64_t oldt = 0; | ||
| 61 | int64_t t = 1; | ||
| 62 | while (r != 0) { | ||
| 63 | auto q = oldr / r; | ||
| 64 | swapdiv(oldr, r, q); | ||
| 65 | swapdiv(olds, s, q); | ||
| 66 | swapdiv(oldt, t, q); | ||
| 67 | } | ||
| 68 | return {oldr, olds, oldt}; | ||
| 69 | } | 53 | } |
| 70 | 54 | ||
| 71 | #endif | 55 | #endif |
