Matrix Power Series(POJ 3233)
- 原题如下:
Matrix Power Series
Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 28044 Accepted: 11440 Description
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative integers below 32,768, giving A’s elements in row-major order.
Output
Output the elements of S modulo m in the same way as A is given.
Sample Input
2 2 4
0 1
1 1Sample Output
1 2
2 3 - 题解:构造矩阵:
此时,令Sk=I+A+…+Ak-1,则有:
通过计算这个矩阵的k次幂,就可求出A的累乘和,时间复杂度为O(n3logk) - 代码:
#include <cstdio>
#include <cctype>
#define number s-'0'
#include <cstring>
#include <vector> using namespace std; typedef vector<int> vec;
typedef vector<vec> mat; int n,k,m;
mat A; void read(int &x)
{
char s;
x=;
bool flag=;
while (!isdigit(s=getchar()))
(s=='-')&&(flag=true);
for (x=number; isdigit(s=getchar());x=x*+number);
(flag)&&(x=-x);
} void write(int x)
{
if (x<)
{
putchar('-');
x=-x;
}
if (x>) write(x/);
putchar(x%+'');
} mat mul(mat &A, mat &B)
{
mat C(A.size(), vec(B[].size()));
for (int i=; i<A.size(); i++)
{
for (int j=; j<B[].size(); j++)
{
for (int k=; k<B.size(); k++)
{
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%m;
}
}
}
return C;
} mat pow(mat A, int n)
{
mat B(A.size(), vec(A.size()));
for (int i=; i<A.size(); i++) B[i][i]=;
while (n>)
{
if (n&) B=mul(B, A);
A=mul(A, A);
n>>=;
}
return B;
} int main(int argc, char * argv[])
{
read(n);read(k);read(m);
A=mat (n,vec(n));
mat B(n*, vec(n*));
for (int i=; i<n; i++)
{
for (int j=; j<n; j++)
{
read(A[i][j]);
B[i][j]=A[i][j];
}
B[n+i][i]=B[n+i][n+i]=;
}
B=pow(B,k+);
for (int i=; i<n; i++)
{
for (int j=; j<n; j++)
{
int a=B[n+i][j]%m;
if (i==j) a=(a+m-)%m;
printf("%d%c", a, j+==n?'\n':' ');
}
}
}
Matrix Power Series(POJ 3233)的更多相关文章
- Matrix Power Series POJ - 3233 矩阵幂次之和。
矩阵幂次之和. 自己想着想着就想到了一个解法,但是还没提交,因为POJ崩了,做了一个FIB的前n项和,也是用了这个方法,AC了,相信是可以得. 提交了,是AC的 http://poj.org/prob ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- 矩阵十点【两】 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 Total Submissions: 11954 Accepted: ...
- [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 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写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
随机推荐
- 自己动手编写一个Mybatis插件:Mybatis脱敏插件
1. 前言 在日常开发中,身份证号.手机号.卡号.客户号等个人信息都需要进行数据脱敏.否则容易造成个人隐私泄露,客户资料泄露,给不法分子可乘之机.但是数据脱敏不是把敏感信息隐藏起来,而是看起来像真的一 ...
- MySQL字符集操作
一.查看编码 show variables like 'character%'; 二.临时设置编码 1.set names xxx set names ${编码}; "set names x ...
- VMware Workstation 15 Pro安装带图形化界面的CentOS7
1.双击打开“VMware Workstation”,然后选择“创建新的虚拟机” 2.在安装向导中,选择“稍后安装操作系统”,然后点击“下一步”继续安装 3.在“客户机操作系统”中选择“Linux(L ...
- @SuppressWarnings注解用法详解(转)
原文连接https://blog.csdn.net/sysware_carol/article/details/52100580 今天来谈谈@SuppressWarnings注解的作用. J2SE 提 ...
- 第4章 DDL数据定义
第4章 DDL数据定义 4.1 创建数据库 1)创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db. hive (default)> creat ...
- CodeQL CLI入门
一.CodeQL CLI 安装和配置 1.下载CodeQL CLI 压缩包 https://github.com/github/codeql-cli-binaries/releases 2.创建Cod ...
- 牛客网数据库SQL实战解析(1-10题)
牛客网SQL刷题地址: https://www.nowcoder.com/ta/sql?page=0 牛客网数据库SQL实战解析(01-10题): https://blog.csdn.net/u010 ...
- 使用C#对华为IPC摄像头二次开发(一)
开发环境: 操作系统:Win10 x64专业版2004 开发工具:VS2019 16.7.2 目标平台:x86 首先去下载IPC SDK(点击下载,需要华为授权账户.) 新建一个WPF的项目,Fram ...
- 2020.08.14小bug
页面下面的滚动条怎么清除 css overflow-x: hidden;
- Combine 框架,从0到1 —— 3.使用 Subscriber 控制发布速度
本文首发于 Ficow Shen's Blog,原文地址: Combine 框架,从0到1 -- 3.使用 Subscriber 控制发布速度. 内容概览 前言 在发布者生产元素时消耗它们 使 ...