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为奇数时: 就按照这么搞 ...
随机推荐
- 06_Python的数据类型3元组,集合和字典_Python编程之路
上一节跟大家讲了Python的列表,当然不是完整的讲完,后续我们还会提到,这一节我们还是来讲Python的数据类型 首先要讲到的就是元组 元组其实拥有列表的一些特性,可以存储不同类型的值,但在某些方面 ...
- Microsoft Soft SQL Server 大数据----分区表性能测试
分区表 MSSQL有一个大数据储存方案,可以提高效率那就是分区表. 使用起来跟普通表没有区别.至于具体原理自己度娘吧. 真正性能的提高,是依赖于硬件的加入.也是就说,当把一个表设置成分区表,每一个分区 ...
- Mego(03) - ORM框架的新选择
前言 从之前的两遍文章可以看出ORM的现状. Mego(01) - NET中主流ORM框架性能对比 Mego(02) - NET主流ORM框架分析 首先我们先谈下一个我们希望的ORM框架是什么样子的: ...
- axure 预览"HTTP/1.1 302 Found"
使用Axure编辑原型时,点击预览出现"HTTP/1.1 302 Found" 第一想到的就是重新安装Axure和检查原型文件是否损坏,验证后证明前Axure和.rp文件都是完好的 ...
- SpringCloud的微服务网关:zuul(理论)
参考链接:https://springcloud.cc/spring-cloud-dalston.html 一.概念与定义 1.为什么要引入API网关 后期维护:路由规则和服务实例列表困难 系统架构: ...
- 【iOS】UIViewController基类的实现
继承是面向对象编程语言的三大特性之一,写好基类会给App的开发带来极大的方便.在iOS开发中,一般一个页面就对应一个ViewController,ViewController在开发中用的也很多,写一个 ...
- css回顾之左侧宽度自适应布局
目标: <!DOCTYPE html> <meta charset=utf-8> <html> <head> <title>alibaba& ...
- Python基础--函数的定义和调用
一.函数的作用: 提高代码的可读性,减少代码的冗余,方便调用和修改,组织结构清晰 二.函数的定义:函数遵循先定义后调用的原则 1.无参函数 def funcname(): #def 是关键字,后跟函数 ...
- Spark测试代码
测试代码: import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.hive.HiveContext ...
- 1.4WEB API 路由配置及参数传输
在没有添加路由时,webapi 的访问比较恶心,访问的时候是没有接口方法的.如果定义了同样参数的方法,它就傻傻的分不清的,为了解决这个问题,我们加上路由吧. 这是生成的api帮助文档列表,可以看到下面 ...