POJ - 3233 Matrix Power Series (矩阵等比二分求和)
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 1
Sample Output
1 2
2 3
题意:求矩阵总和
思路:矩阵高速幂取模,和等比数列矩阵求和,这里说一下怎么二分求矩阵的等比序列和,设矩阵为A。次数为k
设sum(k) = A^1 + A^2 + A^3 + ..... + A^K,那么为了可以二分递归下去我们首先拆出个( A^1 + A^2 + ... + A^(k/2) ,可以非常easy得到:sum(k) = sum(k/2) * (A^(k/2) + 1)
在代码里是假设这个次数是奇数的话会少算一个A^k,所以要记得加上
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
typedef long long ll;
using namespace std;
const int maxn = 32; int m, n;
struct Matrix {
int v[maxn][maxn];
Matrix() {}
Matrix(int x) {
init();
for (int i = 0; i < maxn; i++)
v[i][i] = x;
}
void init() {
memset(v, 0, sizeof(v));
}
Matrix operator *(Matrix const &b) const {
Matrix c;
c.init();
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
c.v[i][j] = (c.v[i][j] + (v[i][k]*b.v[k][j])) % m;
return c;
}
Matrix operator ^(int b) {
Matrix a = *this, res(1);
while (b) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
} u(1); Matrix Add(Matrix a, Matrix b) {
for (int i = 0; i < maxn; i++)
for (int j = 0; j < maxn; j++)
a.v[i][j] = (a.v[i][j]+b.v[i][j]) % m;
return a;
} Matrix BinarySum(Matrix a, int n) {
if (n == 1)
return a;
if (n & 1)
return Add(BinarySum(a, n-1), a^n);
else return BinarySum(a, n>>1) * Add(u, a^(n>>1));
} int main() {
int k;
Matrix a, ans;
while (scanf("%d%d%d", &n, &k, &m) != EOF) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &a.v[i][j]);
ans = BinarySum(a, k);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
printf("%d%c", ans.v[i][j], (j==n-1)? '\n':' ');
}
return 0;
}
POJ - 3233 Matrix Power Series (矩阵等比二分求和)的更多相关文章
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- 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 Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- POJ 3233 Matrix Power Series 矩阵快速幂
设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...
- POJ 3233 Matrix Power Series(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵十点【两】 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 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- POJ 3233 Matrix Power Series (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
随机推荐
- linux c中需要记住的东西
1.记住常见的字符的ASCII值 a------------97 b------------98 A------------65 B------------66 空格' ...
- 【08】css sprite是什么,有什么优缺点
[08]css sprite是什么?有什么优缺点? 概念:将多个小图片拼接到一个图片中.通过background-position和元素尺寸调节需要显示的背景图案. 优点: 减少HTTP请求数,极大地 ...
- android 之 service
在Activity中设置两个按钮,分别为启动和关闭Service: bt01.setOnClickListener(new Button.OnClickListener() { @Override ...
- vim第五章 命令行模式
vim第五章命令行模式 技巧 27 结识vim的命令行模式 在命令行模式中执行的命令有被称作ex命令 在按/调出查找提示符或者<C-r>=访问表示寄存器时 命令行模式也被激活 ...
- TOJ 4804: 树网的核
这个是NOIP的提高组的题 4804: 树网的核 Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Sub ...
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...
- 算法复习——莫队算法(bzoj1878)
题目: Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的 ...
- cf711E ZS and The Birthday Paradox
ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that g ...
- 使用Eclipse+axis2一步一步发布webservice
1.下载axis2相关软件http://axis.apache.org/axis2/java/core/download.html 2.Java环境配置:JAVA_HOME.JRE_HONE.PATH ...
- 建立django博客应用及数据库模型
1.现在就来创建我们的 Django 博客应用,我把它命名为 blog.激活虚拟环境,进入到 manage.py 文件所在的目录下,运行 python manage.py startapp blog ...