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
6
#include "
library/internal/type_traits.hpp
"
7
8
namespace
maomao90
{
9
using namespace
std;
10
template
<
signed
_
int
egral T>
11
requires
internal::type_traits::is_64bit_or_less_v<T>
12
constexpr
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
maomao90
Definition
hashmap.hpp:8
maomao90::inv_gcd
constexpr T inv_gcd(T x, T mod)
Definition
extended_gcd.hpp:12
type_traits.hpp
library
math
extended_gcd.hpp
Generated by
1.13.2