diff options
Diffstat (limited to 'zmodn.h')
| -rw-r--r-- | zmodn.h | 8 |
1 files changed, 5 insertions, 3 deletions
| @@ -40,13 +40,15 @@ public: | |||
| 40 | bool operator!=(const Zmod& z) const { return value != z.value; } | 40 | bool operator!=(const Zmod& z) const { return value != z.value; } |
| 41 | 41 | ||
| 42 | std::optional<Zmod> inverse() const { | 42 | std::optional<Zmod> inverse() const { |
| 43 | auto [g, a, _] = extended_gcd(value, N); | 43 | auto [g, a, b] = extended_gcd(value, N); |
| 44 | return g == 1 ? Zmod(a) : std::optional<Zmod>{}; | 44 | return g == 1 ? Zmod(a) : std::optional<Zmod>{}; |
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | std::optional<Zmod> operator/(const Zmod& d) const { | 47 | std::optional<Zmod> operator/(const Zmod& d) const { |
| 48 | auto i = d.inverse(); | 48 | auto [g, x, y] = extended_gcd(value, d.toint()); |
| 49 | return i ? (*this) * i.value() : i; | 49 | auto [a, b] = std::pair<Zmod, Zmod>{value / g, d.toint() / g}; |
| 50 | auto i = b.inverse(); | ||
| 51 | return i ? a * i.value() : i; | ||
| 50 | } | 52 | } |
| 51 | 53 | ||
| 52 | std::optional<Zmod> operator/=(const Zmod& d) { | 54 | std::optional<Zmod> operator/=(const Zmod& d) { |
