POJ 3233 (矩阵)
题意:对于矩阵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 (矩阵)的更多相关文章
- 矩阵儿快速幂 - POJ 3233 矩阵力量系列
不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...
- Matrix Power Series POJ - 3233 矩阵幂次之和。
矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...
- poj 3233(矩阵高速幂)
题目链接:http://poj.org/problem?id=3233. 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点.本题k的值非常大.所以要用 ...
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- poj 3233 矩阵快速幂+YY
题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...
- 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+ ...
- POJ - 3233 矩阵套矩阵
题意:给你矩阵\(A\),求\(S=\sum_{i=1}^{k}A^i\) 构造矩阵 \[ \begin{bmatrix} A & E \\ 0 & E\\ \end{bmatrix} ...
- Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
题目链接 请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...
- POJ 3233 矩阵快速幂&二分
题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...
随机推荐
- Codeforces 837E. Vasya's Function
http://codeforces.com/problemset/problem/837/E 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...
- 集合Collection总览
前言 声明,本文使用的是JDK1.8 从今天开始正式去学习Java基础中最重要的东西--->集合 无论在开发中,在面试中这个知识点都是非常非常重要的,因此,我在此花费的时间也是很多,得参阅挺多的 ...
- EasyUI 动态创建对话框Dialog
// 拒绝审批通过 function rejectApproval() { // 创建填写审批意见对话框 $("<div id='reject-comment'> </di ...
- istio入门(05)istio的架构概念2
- NOPI实现导入导出泛型List,支持自定义列
概述 业务上需要自定义列的Excel的导入导出,在网上看了好多资料,很多都是有Bug而且都是支持Excel和DataTable的转换,所以自己总结了一下,应用.NET平台上的NPOI封装了支持自定义列 ...
- 安卓手机USB共享网络给PC上网
开端 哈哈,最近我又发现了一个校园网的漏洞,但是只能手机连接,于是就想手机连接之后通过usb共享给电脑上网. 在手机上连接校园网WiFi,开启USB网络共享并且连接电脑之后,却发现电脑十分的卡顿!CP ...
- 一、Python3.6+PyQt5 安装
一.安装PyQt5 方法一:使用pip3工具直接安装 直接在命令行中输入: Python 3.x pip3 install PyQt5 pip3 install PyQt5-tools Python ...
- DIY一个超简单的画图程序
编译环境:VS2017+Easy_X 最近笔者一直在翻阅Easy_X的帮助手册,学习到了一些关于获取鼠标状态消息函数的知识,感觉收获颇大,于是想试验一番,将所学知识运用出来.先补充一下在Easy_X中 ...
- ST-LINK V2 DIY笔记(一)
最近一段时间调试STM32板子的时候,都是用JLINK+杜邦线,或者拿官方板子当STLINK用,可以用,但是体积比较大,有时候觉得比较麻烦.正好前一阵手头项目少,就想DIY一个STLINK. 图是网上 ...
- 给工作添点乐趣--- 为Idea编译时打印图案
控制台输出图案--banner 之前用vs开发时,也可以自己设置编译时控制台打印出的图案,我们管它叫banner. 给idea设置banner 先见一下效果图 还有重磅的 好了.其实设置这个很简单.分 ...