Skip to content

Instantly share code, notes, and snippets.

@VardanGrigoryan
Created January 28, 2021 13:05
Show Gist options
  • Save VardanGrigoryan/489e8ebdb09ac642af84a847865c9f5a to your computer and use it in GitHub Desktop.
Save VardanGrigoryan/489e8ebdb09ac642af84a847865c9f5a to your computer and use it in GitHub Desktop.
/*
* A fast exponentiation.
* The algo has O(log(m)) time complexity, additionally it gets a boost because of compile time.
*/
template <int n, int mod, int debug_info, int m>
struct fast_exp {
static constexpr int value = (m % 2 == 0) ? fast_exp<(n * n) % mod, mod, debug_info, m / 2>::value : (n * fast_exp<(n * n) % mod, mod, debug_info, (m - 1) / 2>::value) % mod;
static constexpr void run_debug() {
static_assert(value == debug_info, "failed");
}
};
template <int n, int mod, int debug_info>
struct fast_exp<n, mod, debug_info, 0> {
static constexpr int value = 1;
};
@VardanGrigoryan
Copy link
Author

cp_fast_exp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment