题意:对于矩阵A,求A^1 + ...... + A^k

按照矩阵十大经典题的思路大致做了下。

在k为奇数时:  A^( k / 2+1)+ 1) * (A^1 + ....... A^(k/2)) + A^(k/2+1)

k为偶数时:(A^(k/2) + 1 )* (A^1 + ................A^(k/2))

但是超时了,应该是没二分的问题。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
typedef long long ll; const int maxn=5e5;
map<int,int>has;
struct Matrix
{
int xmap[30][30];
};
int siz;
Matrix mat;
int n,mod;
Matrix Mul(const Matrix &a,const Matrix &b)
{
Matrix c;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
c.xmap[i][j]=0;
for(int k=0; k<n; k++)
{
c.xmap[i][j]+=a.xmap[i][k]*b.xmap[k][j];
c.xmap[i][j]%=mod;
}
}
}
return c;
} Matrix Pow(int n)
{
if(n == 1)
return mat;
else if(n & 1)
{
return Mul(mat,Pow(n-1));
}
else
{
Matrix tmp = Pow(n>>1);
return Mul(tmp,tmp);
}
} Matrix Add(const Matrix &a,const Matrix &b)
{
Matrix c;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
c.xmap[i][j] = a.xmap[i][j] + b.xmap[i][j];
c.xmap[i][j]%=mod;
}
}
return c;
} Matrix solve(int k)
{
if(k == 1)
return mat;
Matrix tt;
Matrix tmp = solve(k/2);
if (k&1)
{
tt=Pow(k/2+1);
tmp=Add(tmp,Mul(tmp,tt));
tmp=Add(tt,tmp);
}
else
{
tt=Pow(k/2);
tmp=Add(tmp,Mul(tmp,tt));
}
return tmp;
} int main()
{
int k;
while(scanf("%d%d%d",&n,&k,&mod)!= EOF)
{
for(int i = 0; i < n; i++)
for(int j = 0 ; j < n; j++)
{
scanf("%d",&mat.xmap[i][j]);
mat.xmap[i][j] %= mod;
} Matrix ans = solve(k);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
printf("%d ",ans.xmap[i][j]);
printf("\n");
}
}
return 0;
}

  

POJ 3233 (矩阵)的更多相关文章

  1. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

  2. Matrix Power Series POJ - 3233 矩阵幂次之和。

    矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...

  3. poj 3233(矩阵高速幂)

    题目链接:http://poj.org/problem?id=3233. 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点.本题k的值非常大.所以要用 ...

  4. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

  5. poj 3233 矩阵快速幂+YY

    题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...

  6. POJ 3233 矩阵乘法

    题意:求解A+A^2+...+A^k 题解: 1)利用通和公式,原式=(A^k+1 - A)(A - O)^-1 时间复杂度O(n^3lgk) 2)递归求解,A+A^2+...+A^k=(A+A^2+ ...

  7. POJ - 3233 矩阵套矩阵

    题意:给你矩阵\(A\),求\(S=\sum_{i=1}^{k}A^i\) 构造矩阵 \[ \begin{bmatrix} A & E \\ 0 & E\\ \end{bmatrix} ...

  8. Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

    题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...

  9. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

随机推荐

  1. scrapy crawl xmlfeed spider

    from scrapy.spiders import XMLFeedSpider from myxml.items import MyxmlItem class XmlspiderSpider(XML ...

  2. New UWP Community Toolkit - ImageEx

    概述 UWP Community Toolkit  中有一个图片的扩展控件 - ImageEx,本篇我们结合代码详细讲解  ImageEx 的实现. ImageEx 是一个图片的扩展控件,包括 Ima ...

  3. python 学习笔记

    1. 关于两者详细解释,参考链接:www.crifan.com/python_re_search_vs_re_findall/ 代码图

  4. 使用Putty实现windows向阿里云的Linux云服务器上传文件

    1.首先获取PSCP工具 PuTTY小巧方便.但若需要向网络中的Linux系统上传文件,则可以使用PuTTY官方提供的PSCP工具来实现上传.PSCP是基于ssh协议实现. 可以点击这里下载 2.启动 ...

  5. java程序员最不愿意看到的十件事

     0.遍历结果集并构造对象如果你是个时髦的开发者而不是专业人员,显然你从某篇博客中读过有开发者遇到Hibernate的“性能问题”,因而认为ORM都不好,觉得手动编码“明显更好”.喜欢的话你当然可以用 ...

  6. centOs6.5配置jdk及其注意事项

    1.下载jdk1.7 百度云链接: https://pan.baidu.com/s/15vXLO2eV18eVvmt-R5jGnQ 密码: 1gd6 2.解压压缩包 通过终端在/usr/local下新 ...

  7. css3中的动画 @keyframes animation

    动画的运用比较重要.接下来我希望针对我自己学习遇到的问题,再总结一下这个属性的使用方法. 创建一个动画: @keyframes 动画名 {样式} 引用自己创建的动画: animation:动画名  时 ...

  8. 关于ZK框架的onScroll事件的问题

    由于我现在所在的公司用到的zk框架,遇到了一个需求frozen on top. 简单来说就是滚动超过范围后,希望有一块东西停留在滚动窗口的顶部. 一.zk框架 查看了zk的8.x版本,发现组件的支持的 ...

  9. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  10. Oracle12c:支持通过创建identity columen来实现创建自增列

    oracle12c之前如果需要创建自增列必须要通过sequence+trigger来实现.但是oracle12c已经可以像mysql,sqlserver一样通过identity column来设置自增 ...