maomao90's Library
A C++20 library for competitive programming.
Loading...
Searching...
No Matches
extended_gcd.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <concepts>
5
7
8namespace maomao90 {
9using namespace std;
10template <signed_integral T>
11 requires internal::type_traits::is_64bit_or_less_v<T>
12constexpr T inv_gcd(T x, T mod) {
13 using U = internal::type_traits::safely_multipliable_t<T>;
14 U a = mod, b = x, va = 0, vb = 1;
15 while (b) {
16 U k = a / b;
17 a -= k * b;
18 va -= k * vb;
19 swap(a, b);
20 swap(va, vb);
21 }
22 assert(a == 1); // gcd should be equal to 1
23 return (va % mod + mod) % mod;
24}
25} // namespace maomao90
Definition hashmap.hpp:8
constexpr T inv_gcd(T x, T mod)
Definition extended_gcd.hpp:12