7 23 is too large to fit in long long (assuming it is 64 bit). The value becomes truncated.
Edit: Oh, why didn't you say you want pow(b, e) % m instead of just pow(b, e) ? This makes things a lot easier, because in the end you don't need bigints. Just do all your m arithmetic mod. Pubby's solution works, but itβs faster here (O (log e) instead of O (e)).
unsigned int powmod(unsigned int b, unsigned int e, unsigned int m) { assert(m != 0); if (e == 0) { return 1; } else if (e % 2 == 0) { unsigned int squareRoot = powmod(b, e / 2, m); return (squareRoot * squareRoot) % m; } else { return (powmod(b, e - 1, m) * b) % m; } }
dan04 source share