1)求an当n很大时如10^9Java也不能处理,这时候要用到快速幂
0.分治法
int fastPow(int a, int n) { if (n == 1)return a; int tmp = fastPow(a, n / 2); if (n % 2 == 1)return tmp * tmp * a; else return tmp * tmp; }
1.位运算
n&1,取n的最后一位,并且判断这一位是否需要跳过;
n>>=1,n右移一位,把刚处理的一位去掉
int fastPow(int a, int n) { int res = 1; while (n){ if (n & 1)res *= a; a *= a; n >>= 1; } return res; }
2)快速幂取模
an %m=(a%m)n %m
3)矩阵快速幂
const int mxn = 2;//矩阵的阶 const int mod = 1e9; struct Matrix { int m[mxn][mxn]; Matrix() { memset(m, 0, sizeof(m)); } }; Matrix Multi(Matrix a, Matrix b) { Matrix res; for (int i = 0; i < mxn; i++) for (int j = 0; j < mxn; j++) for (int k = 0; k < mxn; k++) res.m[i][j] = (res.m[i][j] + a.m[i][k] * b.m[k][j]) % mod; return res; } Matrix fastPower(Matrix a, int n) { Matrix res; for (int i = 0; i < mxn; i++) res.m[i][i] = i; while (n) { if (n & 1) res = Multi(res, a); a = Multi(a, a); n >>= 1; } return res; }