整数快速幂:

分解成二进制形式易得程序

int fastpow(int base,int n,int mod){int ans=1;while(n){if(n&1) ans*=base%mod;base*=base;n>>=1;}return ans%mod;
}

快速幂复杂度是O(logn),不用快速幂是O(n)

矩阵快速幂:
把整数乘法改成矩阵乘法,原理一样

struct Mat{double m[maxn+5][maxn+5];Mat(){memset(m,0,sizeof m);//构造函数初始化为0阵}void build(){//建单位矩阵for(int i=0;i<=maxn;i++){m[i][i]=1;}}
};
Mat operator*(Mat &a,Mat &b){Mat t;for(int i=1;i<=N;i++){for(int j=1;j<=N;j++){for(int k=1;k<=N;k++){t.m[i][j]+=a.m[i][k]*b.m[k][j];}}}return t;
}void ksm(int n){//和整数快速幂格式完全一样ans.build();while(n>0){if(n&1){ans=ans*a;}a=a*a;n>>=1;}
}

模板题:
https://codeforces.com/gym/102966/problem/C
C. CLETS Patrols
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The Corporation Of Ludicrously Evil Topography and Scouring (CLETS) is working on a new way to organize their henchmen’s patrols so that they will be impossible to predict for a certain handsome british spy that would try to infiltrate their base. To that end, when a henchman reaches a post, a computer will randomly pick to which post the henchman should go next.

By some classified means, we have acquired the probability distributions that the computers use to choose where to send the henchmen next. Your task is to use this information to determine, for every post, the probability that a guard will be there after M steps, so that our friend, the handsome british spy, can plan the safest route to the CLETS base.

A step is defined as moving from one post to another, or waiting in the same post, should the computer decide that. The Guard start their shift on the first post.

Input
The fist line contains two integers separated by a space, N and M where (1≤N≤200) and (1≤M≤10000): the number of posts, and the number of steps we are interested in. The next N lines contain exactly N numbers separated by a space where the j-th number on line i+1 represents the probability that the computer will send a guard to the j-th post from the i-th post.

Output
Print N lines; in the i-th line, print the probability that a guard will be in post number i after M steps.

Your answer is considered correct if its absolute or relative error doesn’t exceed 10−4.

Example
inputCopy
2 2
0.75 0.25
0.5 0.5
outputCopy
0.6875
0.3125

题意:给出一个有向完全图<V,E>,V中有N个顶点,从1号顶点出发跳转M次,每次从顶点vi跳转到顶点vj的概率为<vi,vj>,求最终落在各个顶点的概率。
思路:通过观察找到矩阵的递推关系(step=1,2,3…),通过递推关系找到通项公式,然后用快速幂解出。

#include <bits/stdc++.h>
const int maxn=250;
using namespace std;
int N,M;
struct Mat{double m[maxn+5][maxn+5];Mat(){memset(m,0,sizeof m);}void build(){for(int i=0;i<=maxn;i++){m[i][i]=1;}}
}a,a2,ans;
Mat operator*(Mat &a,Mat &b){Mat t;for(int i=1;i<=N;i++){for(int j=1;j<=N;j++){for(int k=1;k<=N;k++){t.m[i][j]+=a.m[i][k]*b.m[k][j];}}}return t;
}void ksm(int n){ans.build();while(n>0){if(n&1){ans=ans*a;}a=a*a;n>>=1;}
}int main()
{//cout << "Hello world!" << endl;cin>>N>>M;for(int i=1;i<=N;i++){for(int j=1;j<=N;j++){cin>>a.m[i][j];a2.m[i][j]=a.m[i][j];}}ksm(M-1);for(int i=1;i<=N;i++){double temp=0;for(int j=1;j<=N;j++){temp+=ans.m[1][j]*a2.m[j][i];}printf("%.4f\n",temp);}return 0;
}

矩阵快速幂(推导+模板+例题详解)相关推荐

  1. 【洛谷P3390】 矩阵快速幂(模板)

    贴一下矩阵快速幂的模板 #include<iostream> #include<cstdio> #include<cstring> #include<stri ...

  2. 快速幂+矩阵快速幂(总结+例题)

    1.快速幂 以求a的b次方来介绍: 首先把b转换成二进制数 该二进制数第i位的权为  2^i - 1 . 比如 : 11的二进制是1011 11 = 2³×1 + 2²×0 + 2¹×1 + 2º×1 ...

  3. 【面试相关】python实现快速幂取余算法详解

    假设我们要计算 2102^{10}210 对1000取模的结果,可以很简单的得到24.但是如果要求 210002^{1000}21000 对1000取模的结果,常规方法就行不通了,因为常规的变量无法容 ...

  4. 【网格图软判决译码】基于比特级的MAP译码(Bitwise MAP Decoding)算法原理推导和例题详解(intrinsic和extrinsic值)

    Bitwise MAP Decoding算法详解 C C C表示一个(n,k)码,生成矩阵为 G G G,编码如下 Encoding u =

  5. E. Product Oriented Recurrence (矩阵快速幂新模板)

    E. Product Oriented Recurrence time limit per test 1 second memory limit per test 256 megabytes inpu ...

  6. 矩阵快速幂以及其优化【华东交大课程】

    矩阵快速幂以及其优化[华东交大课程] 快速幂基础:C++快速幂_Kicamon的博客-CSDN博客 矩阵快速幂就是在快速幂的基础上结合矩阵运算的用法,其用途较为广泛,可以很大程度上优化代码. 一.矩阵 ...

  7. HDU 2256(矩阵快速幂)

    传送门 题面: Problem of Precision Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  8. 洛谷P3758/BZOJ4887 [TJOI2017] 可乐 [矩阵快速幂]

    洛谷传送门,BZOJ传送门 可乐 Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 299  Solved: 207 Description 加里敦星球的 ...

  9. hdu 2842 Chinese Rings 矩阵快速幂

    分析: 后面的环能不能取下来与前面的环有关,前面的环不被后面的环所影响.所以先取最后面的环 设状态F(n)表示n个环全部取下来的最少步数 先取第n个环,就得使1~n-2个环属于被取下来的状态,第n-1 ...

  10. 又见斐波那契~矩阵快速幂入门题

    链接:https://www.nowcoder.com/acm/contest/105/G 来源:牛客网 题目描述 这是一个加强版的斐波那契数列. 给定递推式 求F(n)的值,由于这个值可能太大,请对 ...

最新文章

  1. Java除法结果带小数、进一法的实现 Java问题通用解决代码
  2. ffmpeg命令行map参数的使用
  3. 突发!Windows XP源代码泄露
  4. 结对开发项目:求整数数组中连续子数组和的最大值
  5. HDU - 2196 Computer(树形dp)
  6. 解决后退,清空验证码(其它文本框保留)
  7. 二分查找非递归方式实现
  8. 谷粒商城高级篇爬坑笔记--错误异常信息乱码问题
  9. 路由器与交换机怎么插线_网络设备:中继器、集线器、网桥、交换机、路由器、网关的超全总结!...
  10. 安装Ubuntu前三件重要的事情
  11. 最简单开启三星a6sUSB调试模式的方法
  12. Racket Cheat Sheet
  13. 虚拟机中的ubuntu怎么设置1920X1080分辨率
  14. (专升本)PowerPnt(PowerPoint 2010的其他功能)
  15. 使用Google、百度等搜索引擎在指定网站内搜索指定内容
  16. 使用Phaser和HTML5特性检测移动设备旋转重力方向
  17. 大龄计算机考研 考研帮,以自己的亲身经历,献给那些大龄的考研朋友们,加油!...
  18. 华为开发者大会2020开幕,发布多项新开发者技术及系统
  19. 基于WPF的开发的知识点
  20. idea打字变成繁体字

热门文章

  1. 【STM32F042】使用NTC热敏电阻实现温度测量
  2. phpQuery的用法
  3. html中居中方法,HTML中的居中方法
  4. 万字详文阐释程序员修炼之道
  5. 高德 设置marker zoom_ZOOM云会议下载安装-ZOOM云视频会议最新版v5.3.53292
  6. 计算机辅助机械设计a卷,二维CAD工程师(机械设计)考试A卷
  7. 使用C#将RGB24转换为YUV420格式
  8. megacli通过盘符定位物理盘_Megaraid 磁盘定位
  9. 实现简易网易云音乐播放器
  10. 图说Python菜鸟版:第18章 JSON文件解析