顺手写了下矩阵类模板

  利用到矩阵乘法的交换律 (A*B)^n == A * (B*A)^n-1 *B

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <deque>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define INF 0x3f3f3f3f
#define MOD 6 using namespace std; int n,m; struct Matrix{
int n,m;
vector< vector<int> >a;
Matrix(){};
Matrix(const Matrix & T) : n(T.n),m(T.m)
{
a.resize(n);
for(int i=; i<n; i++)
{
a[i].resize(m);
for(int j=; j<m; j++)
a[i][j]=T.a[i][j];
}
}
Matrix(int N, int M)
{
n=N;
m=M;
a.resize(N);
for(int i=; i<N; i++)
a[i].resize(M);
}
Matrix & operator=(const Matrix &T)
{
n=T.n;
m=T.m;
a.resize(n);
for(int i=; i<n; i++)
{
a[i].resize(m);
for(int j=; j<m; j++)
a[i][j]=T.a[i][j];
}
return *this;
}
Matrix operator+(const Matrix &T) const
{
Matrix tmp(n,m);
for(int i=; i<n; i++)
for(int j=; j<m; j++)
tmp.a[i][j]=a[i][j]+T.a[i][j];
return tmp;
}
Matrix operator*(const Matrix &T) const
{
Matrix tmp(n,T.m);
for(int i=; i<n; i++)
for(int j=; j<T.m; j++)
for(int k=; k<m; k++)
tmp.a[i][j]=(tmp.a[i][j]+a[i][k]*T.a[k][j])%MOD;
return tmp;
}
void input(int N, int M)
{
n=N;
m=M;
a.resize(n);
for(int i=; i<n; i++)
{
a[i].resize(m);
for(int j=; j<m; j++)
scanf("%d",&a[i][j]);
}
}
void output()
{
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
Matrix pow_m(int N)//矩阵满足n=m
{
Matrix ret(n,n),tmp(*this);
for(int i=; i<n; i++)
ret.a[i][i]=;
while(N)
{
if(N&) ret=ret*tmp;
tmp=tmp*tmp;
N>>=;
}
return ret;
}
}; int main()
{
while(scanf("%d%d",&n,&m)!=EOF && n && m)
{
Matrix a,b,c,d;
a.input(n,m);
b.input(m,n);
c=b*a;
d=c.pow_m(n*n-);
d=a*d*b;
int ans=;
for(int i=; i<d.n; i++)
for(int j=; j<d.m; j++)
ans=ans+d.a[i][j];
printf("%d\n",ans);
}
return ;
}

HDU 4965 矩阵快速幂的更多相关文章

  1. hdu 4965 矩阵快速幂 矩阵相乘性质

    Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Jav ...

  2. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  3. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  4. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  5. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  6. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

  7. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  8. How many ways?? HDU - 2157 矩阵快速幂

    题目描述 春天到了, HDU校园里开满了花, 姹紫嫣红, 非常美丽. 葱头是个爱花的人, 看着校花校草竞相开放, 漫步校园, 心情也变得舒畅. 为了多看看这迷人的校园, 葱头决定, 每次上课都走不同的 ...

  9. HDU 5950 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. Java 8 VM GC Tuning Guide Charter3-4

    第三章 Generations One strength of the Java SE platform is that it shields the developer from the compl ...

  2. Visual Studio Code asp.net 5环境搭建技能Get

    启动准备阶段 预热 1 VS Code 官方地址:https://www.visualstudio.com/en-us/products/code-vs.aspx 2 安装Node.js :https ...

  3. Exception in thread "http-bio-8081-exec-3" java.lang.OutOfMemoryError: PermGen space

    前言: 在http://www.cnblogs.com/wql025/p/4865673.html一文中我曾描述这种异常也提供了解决方式,但效果不太理想,现在用本文的方式,效果显著. 目前此项目只能登 ...

  4. SQL Server 之 锁

    锁,是由锁管理器负责维护,其目的是保证事务的ACID,是平衡并发和数据安全的机制. 锁定粒度与并发性是成反比的,默认情况下,SQL Server Compact 4.0 对数据页使用行级锁定,对索引页 ...

  5. CSS学习------之简单图片切换

    最近一直在重温纯CSS,学习的时候真的才发现,css真的博大精深啊! 所以趁着学习的劲头,谢了个最简单的CSS图片切换! 先整理下思路: 首先我希望图片居中间,两边有个切换按钮,点击按钮的时候,可以实 ...

  6. 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面m个数。

    #include<stdio.h> #include<stdlib.h> int main() { setvbuf(stdout,NULL,_IONBF,); //使用Ecli ...

  7. C++ char*,const char*,string,int 的相互转换

    C++ char*,const char*,string,int 的相互转换   1. string转const char* string s ="abc";const char* ...

  8. AIZU 0009

    Prime Number Time Limit : 1 sec, Memory Limit : 65536 KB Japanese version is here Prime Number Write ...

  9. Java Socket编程readLine返回null,read返回-1的条件

    客户端正常关闭socket的时候,服务器端的readLine()方法会返回null,或者read()方法会返回-1

  10. JAVA多线程的问题以及处理【转】

    http://www.cnblogs.com/springcsc/archive/2009/12/03/1616394.html 12.4 多线程问题及处理          多线程编程为程序开发带来 ...