fast power && matrix fast power

The first time I came into contact with Kuai Mi was when I was training in high school. At that time, I couldn't understand life and death, but now I find it very simple when I pick it up again.

        As we all know, if the computer wants to get a^{n}the result, the most basic idea is to perform n-1 multiplications, but this is undoubtedly a huge waste of time resources. After thinking about it, we can solve it by continuous decomposition.

        For example 3^{11}, we can split into 3^{11}=3^{1}*3^{2}*3^{8}so that the number of multiplications becomes two. But why do we split it like this? We can notice that the split exponents are all different powers of 2, corresponding to the binary representation of the exponent 11, which is 1011. Therefore, the principle we need to remember is that any power of an integer is According to its binary representation, it can be split into a product representation as above.

        We assume that the initial coefficient base is the base number itself , and the initial answer ans is 1 , then convert the exponent into binary form , and take the last digit each time, if it is 1, ans should be multiplied by the current coefficient , and then multiply the coefficient by itself to get The coefficient of the next time ; if it is 0, there is no need to multiply the coefficient, only the coefficient needs to be multiplied by itself.

public int QuickPow(int base, int power) {
        int ans = 1;
        while (power != 0) {
            if ((power & 1) == 1) {//power&1 其实就是得到power二进制表示下的最后一位
                ans *= base;
            base *= base;
            power = power >> 1;
            //power>>1 实现的操作是power左移一位,实际表现为丢弃二进制的最后一位
        return ans;

The difference between matrix fast power and ordinary fast power is that

        Multiplication of numbers ---> matrix multiplication

        The initial value is 1 --> the initial value is the identity matrix

So you can convert ordinary fast powers to matrix fast powers just by knowing how to do matrix multiplication

Matrix multiplication (if you don't know it, you can use Baidu)

\begin{bmatrix} a & b\\c & d \end{bmatrix} * \begin{bmatrix} x & y\\m & n \end{bmatrix}= \begin{bmatrix} ax+bm & ay+bn\\cx+dm & cy+dn \end{bmatrix} 

The matrix fast power code is as follows (take the second-order matrix as an example)

    public int[][] pow(int[][] base, int power) {
        int[][] res = {
   {1, 0}, {0, 1}};//单位矩阵
        while (power > 0) {
            if ((power & 1) == 1) {//若power二进制格式的最后一位为1 表明需要这一位,则将目前结果与base相乘
                res = multiply(res, base);
            power >>= 1;//舍弃二进制格式下的最后一位
            base = multiply(base, base);//base自乘
        return res;

    public int[][] multiply(int[][] a, int[][] b) {//矩阵乘法
        int[][] c = new int[2][2];
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j];
        return c;

Over! Questions and corrections welcome! ! !

Guess you like