gonghr

快速幂

引入

快速幂是用来解决求幂运算的高效方式。

例如我们要求 x90 次方,一般的方法可以通过一个循环,每次乘一个 x,循环 90 次之后就可以得到答案,时间复杂度为 O(n),效率较低。而通过快速幂,我们可以在 O(log(n)) 的时间复杂度内完成该运算。

具体方法

我们可以通过二进制的视角来看待幂运算。

要计算的是 $ \mathrm{x}^{\mathrm{n}} $,把 n 以二进制的形式展开。

\[\mathrm{n}=\,\,\left( ...\mathrm{b}_3\mathrm{b}_2\mathrm{b}_1\mathrm{b}_0 \right) _2\,\,\left( \mathrm{b}_{\mathrm{i}}\text{为}0\text{或}1 \right) \]

\[\mathrm{n}=\mathrm{b}_02^0+\mathrm{b}_12^1+\mathrm{b}_22^2+\mathrm{b}_32^3...=1\times \mathrm{b}_0+2\times \mathrm{b}_1+4\times \mathrm{b}_2+8\times \mathrm{b}_3+... \]

\[\mathrm{x}^{\mathrm{n}}=\mathrm{x}^{\mathrm{b}_02^0+\mathrm{b}_12^1+\mathrm{b}_22^2+\mathrm{b}_32^3...}=\mathrm{x}^{\mathrm{b}_02^0}\times \mathrm{x}^{\mathrm{b}_12^1}\times \mathrm{x}^{\mathrm{b}_22^2}\times \mathrm{x}^{\mathrm{b}_32^3}\times ... \]

\[\mathrm{b}_{\mathrm{i}}\text{为}0\text{或}1\text{,故}\left\{ \frac{\mathrm{x}^{\mathrm{b}_{\mathrm{i}}}=1\left( \mathrm{b}_{\mathrm{i}}=0 \right)}{\mathrm{x}^{\mathrm{b}_{\mathrm{i}}}=\mathrm{x}\left( \mathrm{b}_{\mathrm{i}}=1 \right)} \right. \]

所以,只需要使用一个循环求 n 的二进制的每一位,每次一循环中,如果该二进制位为 0,则不需要乘;如果该二进制位为 1,则需要乘 x。且每一次循环中都执行 x *= x,可以一次获取 x 的不同幂次。

代码实现

public static double getPower(double x, int n) {
      if(x == 0) return 0;
      if(n < 0) {     // x^(-a) = (1/x)^a
          x = 1/x;
          n = -n;
      }
      double res = 1.0;
      while(n > 0) {
          if((n & 1) == 1) {
              res *= x;
          }
          x *= x;
          n >>= 1;
      }
      return res;
}

题目

Pow(x, n)

50. Pow(x, n)

实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,xn )。

 

示例 1:

输入:x = 2.00000, n = 10
输出:1024.00000
示例 2:

输入:x = 2.10000, n = 3
输出:9.26100
示例 3:

输入:x = 2.00000, n = -2
输出:0.25000
解释:2-2 = 1/22 = 1/4 = 0.25
 

提示:

-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104

class Solution {
    public double myPow(double x, int n) {
        long exp = n;              // 特殊处理:补码表示的负数最小值的相反数超过 Integer 表示范围,故提高数据表示范围
        if(x == 0.0) return 0.0; 
        if(n < 0) {
            x = 1/x;
            exp = -exp;
        }
        double res = 1.0;
        while(exp > 0) {
            if((exp & 1) == 1) res *= x;
            x *= x;
            exp >>= 1;
        }
        return res;
    }
}

矩阵快速幂

斐波那契数列

LeetCode 剑指 Offer 10- I. 斐波那契数列

写一个函数,输入 n ,求斐波那契(Fibonacci)数列的第 n 项(即 F(N))。斐波那契数列的定义如下:

F(0) = 0,   F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
斐波那契数列由 0 和 1 开始,之后的斐波那契数就是由之前的两数相加而得出。

答案需要取模 1e9+7(1000000007),如计算初始结果为:1000000008,请返回 1。

 

示例 1:

输入:n = 2
输出:1
示例 2:

输入:n = 5
输出:5
 

提示:

0 <= n <= 100

解:找到一种递推关系,满足矩阵乘法。

f(n) = f(n - 1) + f(n - 2),将其依赖的状态存成列向量

\[\left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n}-1 \right)\\ \mathrm{f}\left( \mathrm{n}-2 \right)\\ \end{array} \right] \]

目标值 f(n) 所在矩阵为:

\[\left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n} \right)\\ \mathrm{f}\left( \mathrm{n}-1 \right)\\ \end{array} \right] \]

下面关键就是找到这两个矩阵直接满足的一个关系,知道系数矩阵 mat

\[\left\{ \begin{array}{c} \mathrm{f}\left( \mathrm{n} \right) =1\times \mathrm{f}\left( \mathrm{n}-1 \right) +1\times \mathrm{f}\left( \mathrm{n}-2 \right)\\ \mathrm{f}\left( \mathrm{n}-1 \right) =1\times \mathrm{f}\left( \mathrm{n}-1 \right) +0\times \mathrm{f}\left( \mathrm{n}-2 \right)\\ \end{array} \right. \]

\[\left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n} \right)\\ \mathrm{f}\left( \mathrm{n}-1 \right)\\ \end{array} \right] =\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] \left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n}-1 \right)\\ \mathrm{f}\left( \mathrm{n}-2 \right)\\ \end{array} \right] \]

则令

\[\mathrm{mat}=\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] \]

我们就成功找到了系数矩阵。

下面可以求得递推关系式:

\[\left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n} \right)\\ \mathrm{f}\left( \mathrm{n}-1 \right)\\ \end{array} \right] =\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] \left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n}-1 \right)\\ \mathrm{f}\left( \mathrm{n}-2 \right)\\ \end{array} \right] =\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] ^2\left[ \begin{array}{c} \mathrm{f}\left( \mathrm{n}-2 \right)\\ \mathrm{f}\left( \mathrm{n}-3 \right)\\ \end{array} \right] =\left[ \begin{matrix} 1& 1\\ 1& 0\\ \end{matrix} \right] ^{\mathrm{n}-1}\left[ \begin{array}{c} \mathrm{f}\left( 1 \right)\\ \mathrm{f}\left( 0 \right)\\ \end{array} \right] =\mathrm{mat}^{\mathrm{n}-1}\times \left[ \begin{array}{c} 1\\ 0\\ \end{array} \right] \]

对于 mat 可以通过快速幂求得结果。

class Solution {
    int mod = (int)1e9+7;
    public int fib(int n) {
        if(n <= 1) return n;
        long[][] mat = new long[][]{
            {1, 1},
            {1, 0}
        };
        long[][] ans = new long[][]{
            {1},
            {0}
        };
        int count =  n - 1;
        while(count > 0) {
            if((count & 1) == 1) ans = mul(mat, ans); // 注意矩阵乘法顺序,不满足交换律
            mat = mul(mat, mat);
            count >>= 1; 
        }
        return (int)(ans[0][0] % mod);
    }
    public long[][] mul(long[][] a, long[][] b) {
        // 矩阵乘法,新矩阵的行数 = a的行数rowa,列数 = b的列数colb
        // a矩阵的列数 = b矩阵的行数 = common
        int rowa = a.length, colb = b[0].length, common = b.length;
        long[][] ans = new long[rowa][colb];
        for (int i = 0; i < rowa; i++) {
            for (int j = 0; j < colb; j++) {
                for (int k = 0; k < common; k++) {
                    ans[i][j] += a[i][k] * b[k][j];
                    ans[i][j] %= mod;
                }
            }
        }
        return ans;
    }
}

第 N 个泰波那契数

LeetCode 1137. 第 N 个泰波那契数

泰波那契序列 Tn 定义如下: 

T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2

给你整数 n,请返回第 n 个泰波那契数 Tn 的值。

 

示例 1:

输入:n = 4
输出:4
解释:
T_3 = 0 + 1 + 1 = 2
T_4 = 1 + 1 + 2 = 4
示例 2:

输入:n = 25
输出:1389537
 

提示:

0 <= n <= 37
答案保证是一个 32 位整数,即 answer <= 2^31 - 1。


解:

f(i) 依赖于 f(i - 1)、f(i - 2)、f(i - 3),故将其存为一个列向量

\[\left[ \begin{array}{c} \mathrm{f}\left( \mathrm{i}-1 \right)\\ \mathrm{f}\left( \mathrm{i}-2 \right)\\ \mathrm{f}\left( \mathrm{i}-3 \right)\\ \end{array} \right] \]

不难得知结果中 f(n) 满足的列向量为

\[\left[ \begin{array}{c} \mathrm{f}\left( n \right)\\ \mathrm{f}\left( n-1 \right)\\ \mathrm{f}\left( n-2 \right)\\ \end{array} \right] \]

下面求系数矩阵,

\[\left\{ \begin{aligned} \mathrm{f}\left( n \right) &=1\cdot \mathrm{f}\left( n-1 \right) +1\cdot \mathrm{f}\left( n-2 \right) +1\cdot \mathrm{f}\left( n-3 \right)\\ \mathrm{f}\left( n-1 \right) &=1\cdot \mathrm{f}\left( n-1 \right) +0\cdot \mathrm{f}\left( n-2 \right) +0\cdot \mathrm{f}\left( n-3 \right)\\ \mathrm{f}\left( n-2 \right) &=0\cdot \mathrm{f}\left( n-1 \right) +1\cdot \mathrm{f}\left( n-2 \right) +0\cdot \mathrm{f}\left( n-3 \right)\\ \end{aligned} \right. \Longrightarrow \left[ \begin{array}{c} \mathrm{f}\left( n \right)\\ \mathrm{f}\left( n-1 \right)\\ \mathrm{f}\left( n-2 \right)\\ \end{array} \right] =\left( \begin{matrix} 1& 1& 1\\ 1& 0& 0\\ 0& 1& 0\\ \end{matrix} \right) \left[ \begin{array}{c} \mathrm{f}\left( n-1 \right)\\ \mathrm{f}\left( n-2 \right)\\ \mathrm{f}\left( n-3 \right)\\ \end{array} \right] \]

\[\mathrm{mat}=\left( \begin{matrix}{} 1& 1& 1\\ 1& 0& 0\\ 0& 1& 0\\ \end{matrix} \right) \]

变换等式得到

\[\left[ \begin{array}{c} \mathrm{f}\left( n \right)\\ \mathrm{f}\left( n-1 \right)\\ \mathrm{f}\left( n-2 \right)\\ \end{array} \right] =\left( \begin{matrix} 1& 1& 1\\ 1& 0& 0\\ 0& 1& 0\\ \end{matrix} \right) \left[ \begin{array}{c} \mathrm{f}\left( n-1 \right)\\ \mathrm{f}\left( n-2 \right)\\ \mathrm{f}\left( n-3 \right)\\ \end{array} \right] =\left( \begin{matrix} 1& 1& 1\\ 1& 0& 0\\ 0& 1& 0\\ \end{matrix} \right) ^{n-2}\left[ \begin{array}{c} \mathrm{f}\left( 2 \right)\\ \mathrm{f}\left( 1 \right)\\ \mathrm{f}\left( 0 \right)\\ \end{array} \right] =\mathrm{mat}^{\mathrm{i}-2}\left[ \begin{array}{c} 1\\ 1\\ 0\\ \end{array} \right] \]

对于 mat 的幂运算可以使用快速幂

class Solution {
    public int tribonacci(int n) {
        if(n == 0) return 0;
        if(n == 1 || n == 2) return 1;
        int[][] mat = new int[][]{
            {1, 1, 1},
            {1, 0, 0},
            {0, 1, 0}
        };
        int[][] ans = new int[][]{
            {1},
            {1},
            {0}
        };
        int count = n - 2;
        while(count > 0) {
            if((count & 1) == 1) ans = mul(mat, ans);
            mat = mul(mat, mat);
            count >>= 1;
        }
        return ans[0][0];
    }
    public int[][] mul(int[][] a, int[][] b) {
        int rowa = a.length;
        int colb = b[0].length;
        int common = b.length;
        int[][] ans = new int[rowa][colb];
        for(int i = 0; i < rowa; i++) {
            for(int j = 0; j < colb; j++) {
                for(int k = 0; k < common; k++) {
                    ans[i][j] += a[i][k] * b[k][j];
                }
            }
        }
        return ans;
    }
}

统计元音字母序列的数目

LeetCode 1220.统计元音字母序列的数目

给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串:

字符串中的每个字符都应当是小写元音字母('a', 'e', 'i', 'o', 'u')
每个元音 'a' 后面都只能跟着 'e'
每个元音 'e' 后面只能跟着 'a' 或者是 'i'
每个元音 'i' 后面 不能 再跟着另一个 'i'
每个元音 'o' 后面只能跟着 'i' 或者是 'u'
每个元音 'u' 后面只能跟着 'a'
由于答案可能会很大,所以请你返回 模 10^9 + 7 之后的结果。

 

示例 1:

输入:n = 1
输出:5
解释:所有可能的字符串分别是:"a", "e", "i" , "o" 和 "u"。
示例 2:

输入:n = 2
输出:10
解释:所有可能的字符串分别是:"ae", "ea", "ei", "ia", "ie", "io", "iu", "oi", "ou" 和 "ua"。
示例 3:

输入:n = 5
输出:68
 

提示:

1 <= n <= 2 * 10^4

解:题目中给定的字符的下一个字符的规则如下:

字符串中的每个字符都应当是小写元音字母 (‘a’,‘e’,‘i’,‘o’,‘u’);

  • 每个元音 ‘a’ 后面都只能跟着 ‘e’;
  • 每个元音 ‘e’ 后面只能跟着 ‘a’ 或者是 ‘a’;
  • 每个元音 ‘i’ 后面不能再跟着另一个 ‘i’;
  • 每个元音 ‘o’ 后面只能跟着 ‘i’ 或者是 ‘u’;
  • 每个元音 ‘u’ 后面只能跟着 ‘a’;

以上等价于每个字符的前一个字符的规则如下:

  • 元音字母 ‘a’ 前面只能跟着 ‘e’,‘i’,‘u’;
  • 元音字母 ‘e’ 前面只能跟着 ‘a’,‘i’;
  • 每个元音 ‘i’ 前面只能跟着 ‘e’,‘o’;
  • 每个元音 ‘o’ 前面只能跟着 ‘i’;
  • 每个元音 ‘u’ 后面只能跟着 ‘o’,‘i’;

我们设 f[i][j] 代表当前长度为 i 且以字符 j 为结尾的字符串的数目,其中在此 j=0,1,2,3,4 分别代表元音字母 ‘a’,‘e’,‘i’,‘o’,‘u’

\[\left\{ \begin{aligned} f\left[ i \right] \left[ 0 \right] &=f\left[ i-1 \right] \left[ 1 \right] +f\left[ i-1 \right] \left[ 2 \right] +f\left[ i-1 \right] \left[ 4 \right]\\ f\left[ i \right] \left[ 1 \right] &=f\left[ i-1 \right] \left[ 0 \right] +f\left[ i-1 \right] \left[ 2 \right]\\ f\left[ i \right] \left[ 2 \right] &=f\left[ i-1 \right] \left[ 1 \right] +f\left[ i-1 \right] \left[ 3 \right]\\ f\left[ i \right] \left[ 3 \right] &=f\left[ i-1 \right] \left[ 2 \right]\\ f\left[ i \right] \left[ 4 \right] &=f\left[ i-1 \right] \left[ 2 \right] +f\left[ i-1 \right] \left[ 3 \right]\\ \end{aligned} \right. \]

结果为以下列向量的值求和

\[\left( \begin{array}{c} f\left[ n \right] \left[ 0 \right]\\ f\left[ n \right] \left[ 1 \right]\\ f\left[ n \right] \left[ 2 \right]\\ f\left[ n \right] \left[ 3 \right]\\ f\left[ n \right] \left[ 4 \right]\\ \end{array} \right) \]

依赖于

\[\left( \begin{array}{c} f\left[ n-1 \right] \left[ 0 \right]\\ f\left[ n-1 \right] \left[ 1 \right]\\ f\left[ n-1 \right] \left[ 2 \right]\\ f\left[ n-1 \right] \left[ 3 \right]\\ f\left[ n-1 \right] \left[ 4 \right]\\ \end{array} \right) \]

列出递推关系式

\[\left( \begin{array}{c} f\left[ n \right] \left[ 0 \right]\\ f\left[ n \right] \left[ 1 \right]\\ f\left[ n \right] \left[ 2 \right]\\ f\left[ n \right] \left[ 3 \right]\\ f\left[ n \right] \left[ 4 \right]\\ \end{array} \right) =\left[ \begin{matrix}{} 0& 1& 1& 0& 1\\ 1& 0& 1& 0& 0\\ 0& 1& 0& 1& 0\\ 0& 0& 1& 0& 0\\ 0& 0& 1& 1& 0\\ \end{matrix} \right] \left( \begin{array}{c} f\left[ n-1 \right] \left[ 0 \right]\\ f\left[ n-1 \right] \left[ 1 \right]\\ f\left[ n-1 \right] \left[ 2 \right]\\ f\left[ n-1 \right] \left[ 3 \right]\\ f\left[ n-1 \right] \left[ 4 \right]\\ \end{array} \right) =\left[ \begin{matrix}{} 0& 1& 1& 0& 1\\ 1& 0& 1& 0& 0\\ 0& 1& 0& 1& 0\\ 0& 0& 1& 0& 0\\ 0& 0& 1& 1& 0\\ \end{matrix} \right] ^{n-1}\left( \begin{array}{c} f\left[ 0 \right] \left[ 0 \right]\\ f\left[ 0 \right] \left[ 1 \right]\\ f\left[ 0 \right] \left[ 2 \right]\\ f\left[ 0 \right] \left[ 3 \right]\\ f\left[ 0 \right] \left[ 4 \right]\\ \end{array} \right) \]

得到系数矩阵

\[mat=\left[ \begin{matrix}{} 0& 1& 1& 0& 1\\ 1& 0& 1& 0& 0\\ 0& 1& 0& 1& 0\\ 0& 0& 1& 0& 0\\ 0& 0& 1& 1& 0\\ \end{matrix} \right] \]

对于系数矩阵使用矩阵快速幂

结果为

\[ans=\sum_{i=0}^4{f\left[ i \right] \left[ 0 \right]} \]

class Solution {
    long mod = 1_000_000_007;
    public int countVowelPermutation(int n) {
        
        long[][] mat =
        {
            {0, 1, 0, 0, 0}, 
            {1, 0, 1, 0, 0}, 
            {1, 1, 0, 1, 1}, 
            {0, 0, 1, 0, 1}, 
            {1, 0, 0, 0, 0}
        };
        long[][] ans = {
            {1},{1},{1},{1},{1}
        };
        int count = n - 1;

        while(count > 0) {
            if((count & 1) == 1) ans = mul(mat, ans);
            mat = mul(mat, mat);
            count >>= 1;
        }
        long res = 0;
        for(int i = 0; i < 5; i++) {
            res += ans[i][0];
        }
        return (int)(res % mod);
    }
    public long[][] mul(long[][] a, long[][] b) {
        int rowa = a.length;
        int colb = b[0].length;
        int common = b.length;
        long[][] ans = new long[rowa][colb];
        for(int i = 0; i < rowa; i++) {
            for(int j = 0; j < colb; j++) {
                for(int k = 0; k < common; k++) {
                    ans[i][j] += a[i][k] * b[k][j];
                    ans[i][j] %= mod;
                }
            }
        }
        return ans;
    }
}

相关文章: