POJ 3233_Matrix Power Series
题意:
求n*n矩阵的幂和
分析:
逐个加起来时间复杂度太高,通过在矩阵中套个矩阵和,再利用矩阵快速幂,最后时间复杂度为O(n3logn)
代码:
#include<cstdio>
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N= 85;
int n;
struct Matrix
{
int row,cal;
ll m[N][N];
};
Matrix init(Matrix a, ll t)
{
for(int i = 0; i < a.row; i++)
for(int j = 0; j < a.cal; j++)
a.m[i][j] = t;
return a;
}
Matrix mul(Matrix a,Matrix b, int mod)
{
Matrix ans;
ans.row = a.row, ans.cal = b.cal;
ans = init(ans,0);
for(int i = 0; i < a.row; i++)
for(int j = 0; j < b.cal; j++)
for(int k = 0; k < a.cal; k++)
ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j])%mod;
return ans;
}
Matrix quick_pow(int k, Matrix A, int mod)
{
Matrix I;
I.row = 2 * n, I.cal = n;
I = init(I,0);
for(int i = 0; i < n; i++)
I.m[i][i] = 1;
int cnt = 1;
while(k){
if(k&1) I = mul(A, I, mod);
A = mul(A, A, mod);
k>>=1;
}
return I;
}
int main (void)
{
int k, mod;scanf("%d%d%d",&n,&k,&mod);
Matrix A, I;
A.row= A.cal = 2 * n;
A = init(A, 0);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%d",&A.m[i][j]);
for(int i = n; i < 2 * n; i++){
A.m[i][i] = 1;
A.m[i][i - n] = 1;
}
Matrix res = quick_pow(k+1, A, mod);
int tmp;
for(int i = n; i < 2 * n; i++){
for(int j = 0; j < n; j++){
if(i - j == n) tmp = (res.m[i][j] - 1+mod)%mod;
else tmp = res.m[i][j];
printf("%d%c",tmp, j == n-1?'\n':' ');
}
}
return 0;
}
样例都调不出来,半天才发现自己原来的模板乘法写错了,神奇的竟然用错的模板过了两道题。。。真是连自己都不能信了,懒惰的后果,下次一定要自己重新动手!!
哦因为之前习惯写I∗Ak的形式,所以a.cal=b.cal,现在换个右乘,错误就暴露了。
POJ 3233_Matrix Power Series的更多相关文章
- POJ 3233Matrix Power Series
妈妈呀....这简直是目前死得最惨的一次. 贴题目: http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Mem ...
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 20309 Accepted: ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- [ACM] POJ 3233 Matrix Power Series (求矩阵A+A^2+A^3...+A^k,二分求和或者矩阵转化)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15417 Accepted: ...
- 矩阵十点【两】 poj 1575 Tr A poj 3233 Matrix Power Series
poj 1575 Tr A 主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的 ...
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- C#菜鸟正则表达式一
LZ菜鸟,仅整理笔记,顺带记录一下,谓之增加印象. LZ认为,没必要太纠结原理,模型, 屌丝能用就对了,剩下的事情用多了自然会去探索. 中文:正则表达式,英文:Regular ExPression, ...
- vue-cli 3 配置打包环境
从新建项目到设置打包环境 1.vue create vue-cli-env 2.新建 vue.config.js 文件,设置baseUrl: './' 3.新建各个环境的文件,例如:.env.deve ...
- CSS3 按钮特效(一)
1. 实例 2.HTML 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- Unity笔记(4)自学第六天
今天主要是写了demo的策划案 [关卡设计部分]: [关卡数值设计]:
- mysql执行语句汇总
插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...
- preg_replace_callback使用方法
官网解释: 执行一个正则表达式搜索并且使用一个回调进行替换 (PHP 4 >= 4.0.5, PHP 5) preg_replace_callback — 执行一个正则表达式搜索并且使用一个回调 ...
- php自动加载函数
含义:将函数注册到SPL __autoload函数栈中.如果该栈中的函数尚未激活,则激活它们. 先看__autoload 函数 printit.class.php <?php class PRI ...
- Jvisualvm--JAVA性能分析工具
JDK自带的JAVA性能分析工具.它已经在你的JDK bin目录里了,只要你使用的是JDK1.6 Update7之后的版本.点击一下jvisualvm.exe图标它就可以运行了. 这里是VisualV ...
- ZooKeeper系列(二)
Zookeeper的环境配置 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. 1.单机模式:Zookeeper只运行在一台服务器上,适合测试环境 ...
- leetcode_654. Maximum Binary Tree
https://leetcode.com/problems/maximum-binary-tree/ 给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创 ...