矩阵乘法是可以分块的,而且幂的和也是具有线性的。

不难得到 Si = Si-1+A*Ai-1,Ai = A*Ai-1。然后矩阵快速幂就可以了。

/*********************************************************
* ------------------ *
* author AbyssalFish *
**********************************************************/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<ctime>
using namespace std; typedef long long ll;
typedef vector<int> row;
typedef vector<row> mat; int n, k, M; mat Mul;
mat &operator *(mat &A, mat& B)
{
mat &R = Mul;
R.assign(n,row(n));
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
for(int k = ; k < n; k++){
R[i][j] = (R[i][j] +A[i][k]*B[k][j])%M; }
}
} return R;
} //#define LOCAL
#ifdef LOCAL
void censor(mat &B)
{
for(auto r: B){
for(int c: r)
cout<<c<<' ';
cout<<endl;
}
}
#endif mat operator ^(mat A,int q)
{
mat Re(n,row(n));
for(int i = ; i < n; i++) Re[i][i] = ;
while(q){
if(q&) Re = Re*A;
A = A*A;
q >>= ;
}
return Re;
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int nn; scanf("%d%d%d",&nn,&k,&M);
n = *nn;
mat A(nn,row(nn));
for(int i = ; i < nn; i++){
for(int j = ; j < nn; j++){
scanf("%d",&A[i][j]);
}
}
mat B(n,row(n));
for(int i = ; i < nn; i++) {
B[i][i] = ;
copy(A[i].begin(),A[i].end(),B[i].begin()+nn);
copy(A[i].begin(),A[i].end(),B[i+nn].begin()+nn);
}
B = B^k;
for(int i = ; i < nn; i++){
for(int j = ; j < nn; j++){
printf("%d%c",B[i][j+nn],j==nn-?'\n':' ');
}
}
#ifdef LOCAL
cout<<"rum time:"<<clock()<<"ms"<<endl;
#endif // LOCAL
return ;
}

POJ 3233 Matrix Power Series (矩阵分块,递推)的更多相关文章

  1. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  2. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  3. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  4. POJ 3233 Matrix Power Series 矩阵快速幂

    设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...

  5. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

  6. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  7. POJ 3233 Matrix Power Series(矩阵等比求和)

    题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...

  8. 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series

    poj 1575  Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...

  9. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  10. POJ 3233 Matrix Power Series (矩阵乘法)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 11954   Accepted:  ...

随机推荐

  1. 【leetcode 114. 二叉树展开为链表】解题报告

    思路:递归,将左子树变成单链表形式,再将右子树变成单链表形式,最后将左子树单链表的末端连接到右子树单链表表头,将根节点的左孩子置空 void flatten(TreeNode* root) { if ...

  2. spring boot jpa 使用<S extends T> List<S> findAll(Example<S> example)查询数据

    直接上代码 //查询条件对象 TinventivePrinciple time = new TinventivePrinciple(); //设置需要查询的条件(赋值) time.setIsTime( ...

  3. Head First Python 1-4章学习感悟

    一.学习知识总结 (1)迭代         range(起始值,结束值,步长):包含起始值,不包含结束值,步长为正数 (2)导入模块 from datetime import datetime (3 ...

  4. 在 windows 下搭建 IDEA + Spark 连接 Hive 的环境

    为了开发测试方便,想直接在 IDEA 里运行 Spark 程序,可以连接 Hive,需不是打好包后,放到集群上去运行.主要配置工作如下: 1. 把集群环境中的 hive-core.xml, hdfs- ...

  5. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  6. SAP 权限层次

    此文可是没有维护过权限的人对权限有一个整体的认知,转来加以记忆. 一是系统权限 主要是对模块为单位的权限划分,具体就是用户对该模块可见不可见,能不能对该模块进行再授权的操作.表现在用户界面就是用户登录 ...

  7. DNS解析工具--nslookup和dig使用

    1.nslookup使用 [root@master ~]# nslookup> server 8.8.8.8    #指定域名服务器Default server: 8.8.8.8Address: ...

  8. AT2657 Mole and Abandoned Mine

    传送门 好神的状压dp啊 首先考虑一个性质,删掉之后的图一定是个联通图 并且每个点最多只与保留下来的那条路径上的一个点有边相连 然后设状态:\(f[s][t]\)代表当前联通块的点的状态为\(s\)和 ...

  9. Apache服务器配置虚拟域名

    我在别处发的帖子 http://www.52pojie.cn/thread-599829-1-1.html

  10. docker镜像的分层结构三

    docker的镜像分层 docker里的镜像绝大部分都是在别的镜像的基础上去进行创建的,也就是使用镜像的分层结构. 实验 比如说使用dockerfile去创建一个最简单的hello镜像.创建好对应的d ...