题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))(A + A^2 + A^3 + ... + A^(m/2)),然后依次计算下去,就可以分解,logn的复杂度分解,注意要分奇偶. 另一种是直接构造矩阵,,然后就可以用辞阵快速幂计算了,注意要用分块矩阵的乘法. 代码如下: 倍增法: #pragma comment(linker, "/STACK:10…
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\(E\). 下面讨论\(k > 0\)的情况: 方法一 设答案为\(S_k(k > 0)\) 把矩阵增广一下 \(\begin{bmatrix} A & O \\ E & E \end{bmatrix} \begin{bmatrix} A^n\\ S_{n-1} \end{bmat…
题目链接:https://vjudge.net/problem/UVA-10518 题解: 问:求斐波那契数f[n]的时候调用了多少次f[n] = f[n-1] + f[n-2],没有记忆化,一直递归到f[0].f[1],其中f[0].f[1]也算调用了一次. 设求f[n]调用了S[n]次,则可知: S[n] = S[n-1] + S[n-2] + 1.构造矩阵求解即可. 代码如下: #include <iostream> #include <cstdio> #include &l…
题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #include <string.h> long long n; int b; struct state { int s[2][2]; state(int a = 0, int b = 0, int c = 0, int d = 0) { s[0][0] = a, s[0][1] = b, s[1][0] =…
典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a): //POJ #include <cstdio> #include <iostream> using namespace std; ; struct matrix { ][]; }ans, base; matrix multi(matrix a, matrix b) { matrix…
题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cstring> #define ll long long #define mod int(1e9+9) struct jz { ll num[][]; jz(){ memset(num, , sizeof(num)); } jz operator*(const jz&p)const { jz ans…
第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; ; int n;…
题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) ..... f(d),求 f(n) = a1*f(n-1) + a2*f(n-2) +....+ ad*f(n-d),计算f(n) % m. 析:很明显的矩阵快速幂,构造矩阵, ,然后后面的就很简单了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1…
题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 n 很大 自然想到是 矩阵快速幂 那么问题就是 怎么构造矩阵 我们想到的一种构造方法是 n = 2 时 n = 3 时 然后大概就能够发现规律了吧 .. AC代码 #include <cstdio> #include <cstring> #include <ctype.h>…
                  Yet another Number Sequence Let’s define another number sequence, given by the following function:f(0) = af(1) = bf(n) = f(n − 1) + f(n − 2), n > 1When a = 0 and b = 1, this sequence gives the Fibonacci Sequence. Changing the values…