Skip to content

Commit

Permalink
added modulo exponentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
Enriquefft committed Jul 14, 2024
1 parent 177f078 commit d4541a3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 53 deletions.
32 changes: 32 additions & 0 deletions src/ModuloExponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <cstdint>
using ll = intmax_t;
using ull = uintmax_t;

constexpr ll MODULO = 1000000007;

// exponent is very large
// modulo is prime (1e9 + 7)
// base is always 2

/// @brief Binary Exponentiation with modulo
/// @param expo exponent
/// @param base base (default: 2)
/// @param modulo modulo
template <bool USE_FERMAT_LITTLE_THEOREM = false>
constexpr auto bin_mod_exp(ll expo, ll base = 2, ll modulo = MODULO) -> ll {

if constexpr (USE_FERMAT_LITTLE_THEOREM) {
expo %= (modulo - 1);
}

ll res = 1;

while (expo > 0) {
if (expo & 1LL) {
res = (res * base) % modulo;
}
base = (base * base) % modulo;
expo >>= 1LL;
}
return res;
}
53 changes: 0 additions & 53 deletions src/main.cpp

This file was deleted.

0 comments on commit d4541a3

Please sign in to comment.