经典矩阵快速幂之一-----poj3233(矩阵套矩阵
题意:给你一个矩阵A,求S=A+A^2+A^3+...+A^k。
其实这个当时我看着毫无头绪,看了他们给的矩阵发现好!精!妙!
我们这样看
是不是有点思路!
没错!就是右上角,我们以此类推可以得到A+A^2+A^3+...+A^k+E,我们只要再减去个单位矩阵就好了。
但是!我矩阵里面怎么套矩阵!肿!么!办!其实很简单,一个n*n的矩阵,我们可以把它看成(2*n)*(2*n)的矩阵,就把他分成了四份,就如上图所示,就很简单了!
注意下小坑点:减了可能就负了,后面减完要加个mod(ง •_•)ง
- #include<cstdio>
- #include<cmath>
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #include<stack>
- #include<cstring>
- #include<queue>
- #include<set>
- #include<string>
- #include<map>
- #include <time.h>
- #define PI acos(-1)
- using namespace std;
- typedef long long ll;
- typedef double db;
- const int maxn = ;
- const int N = ;
- const ll maxm = 1e7;
- const int INF = 0x3f3f3f;
- const ll inf = 1e15 + ;
- const db eps = 1e-;
- ll n, k, mod;
- struct Matrix{
- ll mat[maxn][maxn];
- Matrix operator*(const Matrix& m)const{
- Matrix tmp;
- for (int i = ; i <= n; i++) {
- for (int j = ; j <= n; j++) {
- tmp.mat[i][j]=;
- for (int k = ; k <= n; k++) {
- tmp.mat[i][j] += mat[i][k]*m.mat[k][j]%mod;
- tmp.mat[i][j]+=mod;
- tmp.mat[i][j] %= mod;
- }
- }
- }
- return tmp;
- }
- };
- int Pow(Matrix &m, int k) {
- Matrix ans;
- memset(ans.mat , , sizeof(ans.mat));
- for (int i=; i<=n; i++) {
- ans.mat[i][i]=;
- ans.mat[i+n][i+n]=;
- }
- n*=;
- while(k){
- if(k&)
- ans = ans*m;
- k >>= ;
- m = m*m;
- }
- n/=;
- for (int i=; i<=n; i++) {
- ans.mat[i][i+n]--;
- ans.mat[i][i+n]+=mod;
- ans.mat[i][i+n]%=mod;
- }
- for (int i=; i<=n; i++) {
- for (int j=; j<=n; j++) {
- if (j==n) printf("%d\n", ans.mat[i][j+n]);
- else printf("%d ", ans.mat[i][j+n]);
- }
- }
- }
- void solve() {
- Matrix m; memset(m.mat, , sizeof(m.mat));
- scanf("%lld%lld%lld", &n, &k, &mod);
- for (int i = ; i <= n; i++) {
- for (int j = ; j <= n; j++) {
- scanf("%lld", &m.mat[i][j]);
- m.mat[i][i+n]=;
- m.mat[i+n][i+n]=;
- }
- }
- k++;
- Pow(m, k);
- }
- int main() {
- int t = ;
- //freopen("in.txt", "r", stdin);
- // scanf("%d", &t);
- while(t--)
- solve();
- return ;
- }
经典矩阵快速幂之一-----poj3233(矩阵套矩阵的更多相关文章
- Luogu P3390 【模板】矩阵快速幂&&P1939 【模板】矩阵加速(数列)
补一补之前的坑 因为上次关于矩阵的那篇blog写的内容太多太宽泛了,所以这次把一些板子和基本思路理一理 先看这道模板题:P3390 [模板]矩阵快速幂 首先我们知道矩阵乘法满足结合律而不满足交换律的一 ...
- 矩阵快速幂——将运算推广到矩阵上HDU 1575
/* 本题的思路比较简单,就是将递推公式写出来,然后表达成为一个矩阵的形式 最后通过计算就可以得到一个符合题目要求的矩阵, 然后就是将矩阵上面所有的对角线元素相加 得到的结果即为所求的目标 */ #i ...
- 矩阵快速幂+二分 poj3233
#include <iostream> #include <cstdio> #include <string> #include <cstring> # ...
- hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)
题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- Count Numbers(矩阵快速幂)
Count Numbers 时间限制: 8 Sec 内存限制: 128 MB提交: 43 解决: 19[提交] [状态] [讨论版] [命题人:admin] 题目描述 Now Alice want ...
- hdu 5451 Best Solver 矩阵循环群+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=5451 题意:给定x 求解 思路: 由斐波那契数列的两种表示方法, 之后可以转化为 线性表示 F[n] = ...
- 矩阵快速幂---BestCoder Round#8 1002
当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?可以用矩阵快速幂来加速计算.我们可以用矩阵来表示数列递推公式比如fibonacci数列 可以表示为 [f(n) f(n-1)] = [f(n ...
- P3390 【模板】矩阵快速幂
题目背景 矩阵快速幂 题目描述 给定n*n的矩阵A,求A^k 输入输出格式 输入格式: 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵第i行第j列的元素 输出格式: 输出A^k ...
随机推荐
- 每月IT摘录201810
技术 1.Redis.对于单机实例,我们采用原生主从(Master-Slave)模式实现高可用,常规模式下对外仅暴露 Master 节点.由于使用原生 Redis,所以单机实例支持所有 Redis 指 ...
- 扩展欧几里得 hdu 1576
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 不知道扩展欧几里得的同学可以参考:https://blog.csdn.net/zhjchengf ...
- Angular之模版引用变量
A template reference variable is often a reference to a DOM element within a template. It can also b ...
- 【mysql】分区表
分区表是什么? 分区表可以按照事先创建的规则,对mysql的记录进行分组,每一个组具有一个独立的逻辑单元来存储该组的数据.典型的如:按照创建时间的年份分组,按照id的顺序分组(每1000万条数据分一个 ...
- SpringBoot使用@Value从yml文件取值为空--注入静态变量
SpringBoot使用@Value从yml文件取值为空--注入静态变量 1.application.yml中配置内容如下: pcacmgr: publicCertFilePath: ...
- CSS-calc 兼容写法
width: 90%;/*写给不支持calc()的浏览器*/ width:-moz-calc(100% - (10px + 5px) * 2); width:-webkit-calc(100% - ( ...
- SQL删除重复数据只保留一条数据
1.表结构与数据: CREATE TABLE tablezzl( id int, name ) ); 2.查询出重复的数据: 3.查询出要保留的重复数据: 4.最终的SQL: DELETE FROM ...
- devexpress WinForms MVVM
WinForms MVVM This section is dedicated to the Model-View-ViewModel (MVVM) architectural pattern. Yo ...
- "cni0" already has an IP address different from 10.244.2.1/24。 Error while adding to cni network: failed to allocate for range 0: no IP addresses available in range set: 10.244.2.1-10.244.2.254
"cni0" already has an IP address different from 10.244.2.1/24. Error while adding to cni n ...
- Centos7 开机启动命令行模式
1.在图形界面下单击鼠标右键,选择“Konsole”: 2. 获取当前系统启动模式,输入:systemctl get-default 3.查看配置文件, cat /etc/inittab 4.通过以上 ...