学习博客:https://www.cnblogs.com/cmmdc/p/6936196.html

https://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html

首先看看整数快速幂的用处:

如果我们要求x^19,一般思想是for循环来做,这样的话要循环19次,如果次方数太大的话,显然是很要时间的,如果用矩阵快速幂的方法的话,可以接着看:

19的二进制表示=10011;  x^19=x^16*x^2*x^1,看看16,2,1是怎么来的,其实就是19的二进制上面的1对应的值,所以依靠这个我们可以超级快的求出来x^19次方

下面看代码的解释:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
const int maxn=1e4+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int Quickpow(int x,int n)
{int ans=1,res=x;while(n)//这里看不太懂的话自己模拟一下
    {if(n&1){ans*=res;}res*=res;n>>=1;}return ans;
}
int main()
{int n,x;cin>>x>>n;//求x^n次方cout<<Quickpow(x,n)<<endl;return 0;
}

下面看看矩阵快速幂:

讲解一道题目来理解:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575

Tr A

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7332    Accepted Submission(s): 5384

Problem Description
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
Input
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
Output
对应每组数据,输出Tr(A^k)%9973。
Sample Input
2 2 2 1 0 0 1 3 99999999 1 2 3 4 5 6 7 8 9
Sample Output
2 2686
Author
xhd
Source
HDU 2007-1 Programming Contest
Recommend
linle   |   We have carefully selected several similar problems for you:  1757 1588 2256 2604 2254 
思路: 没什么思路,其实就是一道矩阵快速幂的裸题,具体看代码:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=9973;
const int maxn=1e4+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
ll k;
struct matrix
{int a[15][15];
}ans,res;
void init()
{memset(ans.a,0,sizeof(ans.a));for(int i=0;i<n;i++){ans.a[i][i]=1;//相当于整数快速幂里面的初值1 ,这里是一个单位矩阵for(int j=0;j<n;j++){cin>>res.a[i][j];//初值
        }}
}
matrix multiply(matrix x,matrix y)
{matrix temp;memset(temp.a,0,sizeof(temp.a));for(int i=0;i<n;i++){for(int j=0;j<n;j++){for(int l=0;l<n;l++){temp.a[i][j]+=x.a[i][l]*y.a[l][j];if(temp.a[i][j]>mod) temp.a[i][j]%=mod;}}}return temp;
}
void Quickpow()
{while(k){if(k&1){ans=multiply(ans,res);//这里跟整数快速幂几乎一样
        }res=multiply(res,res);k>>=1;}
}
int main()
{int t;cin>>t;while(t--){ll sum=0;cin>>n>>k;init();Quickpow();for(int i=0;i<n;i++)sum+=ans.a[i][i];sum%=mod;cout<<sum<<endl;}return 0;
}

下面看一道应用题:

题目链接:http://poj.org/problem?id=3070

Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19383   Accepted: 13413

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

Output

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875

Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

.

Source

Stanford Local 2006
题目大意:就是求斐波那契数列
思路:首先我们要构造一个矩阵快速幂,一般把一道题转化为矩阵快速幂分为两步:
第一步:写出递推式,在这里是a[n]=a[n-1]+a[n-2]
第二步:把递推式转化为矩阵快速幂,那么我们要构造一个矩阵的n次方
,简写成T * a[n-1]=a[n],T矩阵就是那个2*2的常数矩阵,而

这里就是个矩阵乘法等式左边:1*f(n-1)+1*f(n-2)=f(n);1*f(n-1)+0*f(n-2)=f(n-1);

这里还是说一下构建矩阵递推的大致套路,一般a[n]与a[n-1]都是按照原始递推式来构建的,当然可以先猜一个a[n],主要是利用矩阵乘法凑出矩阵T,第一行一般就是递推式,后面的行就是不需要的项就让与其的相乘系数为0。矩阵T就叫做转移矩阵(一定要是常数矩阵),它能把a[n-1]转移到Aa[n];然后这就是个等比数列,直接写出通项:此处A1叫初始矩阵。所以用一下矩阵快速幂然后乘上初始矩阵就能得到a[n],这里a[n]就两个元素(两个位置),根据自己设置的a[n]对应位置就是对应的值,按照上面矩阵快速幂写法,ans.a[0][0]=f(n)就是我们要求的。

注意构造的时候要构造为一个方阵,就是在原来构造的基础上补上0

看代码:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=10000;
const int maxn=1e9+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
int n;
struct matrix
{ll a[3][3];
}ans,res;
void init()
{res.a[0][0]=1;res.a[0][1]=1;res.a[1][0]=1;res.a[1][1]=0;ans.a[0][0]=1;ans.a[0][1]=0;ans.a[1][0]=0;ans.a[1][1]=1;
}
matrix multiply(matrix x,matrix y)
{matrix temp;memset(temp.a,0,sizeof(temp.a));for(int i=0;i<2;i++){for(int j=0;j<2;j++){for(int k=0;k<2;k++){temp.a[i][j]=(temp.a[i][j]+x.a[i][k]*y.a[k][j])%mod;}}}return temp;
}
void Quickpow()
{while(n){if(n&1){ans=multiply(ans,res);}res=multiply(res,res);n>>=1;}
}
int main()
{while(cin>>n){if(n==-1) break;if(n==0) cout<<0<<endl;else if(n==1) cout<<1<<endl;else{n--;init();Quickpow();cout<<ans.a[0][0]<<endl;}}return 0;
}

给一些简单的递推式
1.f(n)=a*f(n-1)+b*f(n-2)+c;(a,b,c是常数)

2.f(n)=c^n-f(n-1) ;(c是常数)

转载于:https://www.cnblogs.com/caijiaming/p/9647709.html

矩阵快速幂的学习(系统的学习)相关推荐

  1. 快速幂与矩阵快速幂学习笔记

    首先附上我学习快速幂的链接 https://blog.csdn.net/qq_19782019/article/details/85621386 并从中摘抄了一些有用的东西记录下来作为总结 1.&qu ...

  2. 矩阵快速幂 学习笔记

    据说,矩阵快速幂在递推式优化上相当神奇,而且效率很高... 两矩阵相乘,朴素算法的复杂度是O(N^3).如果求一次矩阵的M次幂,按朴素的写法就是O(N^3*M).既然是求幂,不免想到快速幂取模的算法, ...

  3. 数字迷阵(矩阵快速幂+结论题)

    数字迷阵(矩阵快速幂+结论题) 题目描述 小可可参观科学博物馆时,看到一件藏品,上面有密密麻麻的数字,如下所示:    1   2   3   5    8    13   21   34   55 ...

  4. 一文彻底搞懂快速幂(原理、实现、矩阵快速幂)

    前言 大家好,我是bigsai,之前有个小老弟问到一个剑指offer一道相关快速幂的题,这里梳理一下讲一下快速幂! 快速幂是什么? 顾名思义,快速幂就是快速算底数的n次幂.你可能疑问,求n次幂算n次叠 ...

  5. 40行代码AC_HDU 1575 TrA 矩阵快速幂(附快速幂+矩阵快速幂的讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 心路历程 1.开始看成求主对角线元素和的n次幂了,用快速幂解得.结果压根不对,又仔细看 ...

  6. 洛谷P3390 【模板】矩阵快速幂

    传送门 从今天开始学习矩阵快速幂.jpg 1 //minamoto 2 #include<iostream> 3 #include<cstdio> 4 #include< ...

  7. hdu 6395Sequence【矩阵快速幂】【分块】

    Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  8. [转]快速矩阵快速幂

    大学生程序代写 矩阵 快速幂 出处:http://www.cnblogs.com/yan-boy/archive/2012/11/29/2795294.html 矩阵的快速幂是用来高效地计算矩阵的高次 ...

  9. AtCoder abc256全题解(区间合并模板、矩阵快速幂优化dp、线段树……)

    文章目录 A B C-枚举 D-区间合并模板 E-图论建模,函数图的性质 题意 思路 代码 F-树状数组 题意 思路 代码 G-矩阵快速幂优化dp H-线段树 思路 实现 传送门 本文CSDN 本文j ...

最新文章

  1. goland/go语言项目--本地包的导入(将项目添加至GOPATH中)(基于macOS)
  2. as本地仓库更改_Android Studio 之 Gradle与Project Structure详解
  3. Matlab Robotic Toolbox V9.10工具箱(三):轨迹规划
  4. php中的round是什么,phpround函数怎么用
  5. php rsa加密实例,关于PHP语言的RSA加密实例讲解
  6. lambda :: -_无需再忙了:Lambda-S3缩略图,由SLAppForge Sigma钉牢!
  7. Holedox Moving
  8. Python:字典列表字符串方法测试
  9. virtualbox安装android6.0并设置分辨率为1920x1080x32
  10. cv mat保存图片_(七)神秘的Mat
  11. (转)用Ajax技术让IE Web Control Tree View实现大数据量读取
  12. mysql 子查询 博客_mysql——多表——子查询——示例
  13. java 线程状态_Java线程的状态
  14. 人脸识别 android demo,Android集成人脸识别demo分享
  15. 技巧篇:常用的vba代码汇总
  16. stm32正常运行流程图_stm32初始化流程图解析
  17. 记账之后的收支明细,如何以收支图表的形式呈现
  18. 洛谷P2530 [SHOI2001]化工厂装箱员
  19. win7计算机磁盘清理,win7电脑清理磁盘的操作过程
  20. PCI-E 5.0标准正式公布!速度再次翻番达32GT/s

热门文章

  1. 2021年终总结模板.pptx(附下载链接)
  2. ubuntu中使用不同版本cuda(转)
  3. 『优势特征知识蒸馏』在淘宝推荐中的应用
  4. 【论文】基于特定实体的文本情感分类总结(PART II)
  5. 宁波大学计算机网络实验五,宁波大学计算机网络实验四.doc
  6. 决策树算法python实现_决策树之python实现ID3算法(例子)
  7. 三维图像处理_【图像处理】用于三维物体检测的三维骨干网络
  8. 独立站现在好不好做?个人适合做跨境电商独立站吗?
  9. Redis基础(十二)——缓存读写策略
  10. call and apply