Skip to content

Instantly share code, notes, and snippets.

@RinChanNOWWW
Created August 19, 2021 03:20
Show Gist options
  • Save RinChanNOWWW/8ae4dbc6889c14dbe237c216c6ccce80 to your computer and use it in GitHub Desktop.
Save RinChanNOWWW/8ae4dbc6889c14dbe237c216c6ccce80 to your computer and use it in GitHub Desktop.
快速幂运算
#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