// (a+bi)(a+bi) = (a*a - b*b) + 2ab*i void power2(int32_t a, int32_t b, int64_t k, int32_t m) { Complex x(a,b,m); int64_t n = k; while ((n & 1) == 0) { x.square(); n >>= 1; } Complex P = x; n >>= 1; while (n > 0) { x.square(); if ((n & 1) != 0) P.multiply(x); n >>= 1; } cout << P << endl; }
void Complex::power(int n) { Complex tmp = *this; for (int i = 0; i < n; i++) tmp.multiply(*this); *this = tmp; }