Created
August 19, 2021 03:20
-
-
Save RinChanNOWWW/8ae4dbc6889c14dbe237c216c6ccce80 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
#include <type_traits> | |
template <typename T, typename P> | |
T qpow( | |
T a, | |
typename std::enable_if<std::is_integral<P>::value, P>::type n, | |
T init) { | |
T ans = init; | |
while (n) { | |
if (n & 1) { | |
ans = ans * a; | |
} | |
n >>= 1; | |
a = a * a; | |
} | |
return ans; | |
} | |
// usage | |
#include <iostream> | |
int main() { | |
int res = qpow<int, int>(2, 11, 1); | |
std::cout << res << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment