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为奇数时: 就按照这么搞 ...
随机推荐
- 咬碎STL空间配置器
STL空间配置器 一.开场白: 给我的感觉就是,了解是空间配置器的功能,是那么的明了:在看原理,我还是很开心:接下来是360度大转变: 那么长的变量或者函数命名.那么多的宏.不爽,不过,遇上我这种二货 ...
- iOS极光推送SDK的使用流程
一.极光推送简介 极光推送是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,整合了iOS.Android和WP平台的统一推送服务.使用起来方便简单,已于集成,解决了原生远程推送繁 ...
- Python 实现火车票查询工具
注意:由于 12306 的接口经常变化,课程内容可能很快过期,如果遇到接口问题,需要根据最新的接口对代码进行适当修改才可以完成实验. 一.实验简介 当你想查询一下火车票信息的时候,你还在上 12306 ...
- python 操作PostgreSQL
pip install psycopg Python psycopg2 模块APIs 以下是psycopg2的重要的的模块例程可以满足Python程序与PostgreSQL数据库的工作. S.N. A ...
- 总体来说,require_once 肯定要比 require 性能好
首先,总体来说,require_once 肯定要比 require 性能好. 因为 require 某个文件等同于 "编译 + 执行" 这个文件:require_once 避免了对 ...
- 关于 Ubuntu Linux 16.04中文版的 root 权限及桌面登录问题
新接触 Ubuntu 的朋友大多会因为安装中没有提示设置 root 密码而不太清楚是什么原因. 起初 Ubuntu 团队希望安装尽可能的简单. 不使用 root , 在安装期间的两个用户交互步骤可以省 ...
- spark算子:partitionBy对数据进行分区
def partitionBy(partitioner: Partitioner): RDD[(K, V)] 该函数根据partitioner函数生成新的ShuffleRDD,将原RDD重新分区. s ...
- python操作mongodb
# python操作mongodb # 首先,引入第三方模块pymongo,该模块是python用来操作mongodb的 import pymongo # 第二步,设置ip地址,以及表格名称,表格名字 ...
- 【阿里聚安全·安全周刊】 全美警局已普遍拥有破解 iPhone 的能力 | 女黑客破解任天堂Switch,称硬件漏洞无法修复
本周的七个关键词: 破解 iPhone丨 女黑客破解任天堂丨假的身份证 丨 扫黄打非丨华盛顿特区发现手机间谍设备 丨 Telegram被俄罗斯监管机构告上法庭丨价值5万美金的Firefox浏览器漏洞 ...
- Python求解啤酒问题(携程2016笔试题)
问题描述:一位酒商共有5桶葡萄酒和1桶啤酒,6个桶的容量分别为30升.32升.36升.38升.40升和62升,并且只卖整桶酒,不零卖.第一位顾客买走了2整桶葡萄酒,第二位顾客买走的葡萄酒是第一位顾客的 ...