longlongbinpow(longlong a, longlong b){ if (b == 0) return1; longlong res = binpow(a, b / 2); if (b % 2) return res * res * a; else return res * res; }
非递归
1 2 3 4 5 6 7 8 9 10
longlongbinpow(longlong a, longlong b){ longlong res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; }
longlongbinpow(longlong a, longlong b, longlong m){ a %= m; longlong res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; }