Created
January 28, 2021 13:05
-
-
Save VardanGrigoryan/489e8ebdb09ac642af84a847865c9f5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
}; |
Author
VardanGrigoryan
commented
Jan 28, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment