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: ...
随机推荐
- 查找闲置ip
首先ping 一下该网段的广播地址,如ping 192.168.0.255 ,然后使用arp -a就可以查看到那些地址是被使用的,以及网卡的mac值.剩余的是暂时还未被使用的,当然没开机的机器就看不出 ...
- 【C#】【数据结构】002-线性表:单链表
C#数据结构:单链表 1.自定义单链表结构: 单链节点类 using System.Collections; using System.Collections.Generic; using Unity ...
- PAT Basic 1023
1023 组个最小数 给定数字0-9各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意0不能做首位).例如:给定两个0,两个1,三个5,一个8,我们得到的最小 ...
- centos 装 android studio
一.安装前的准备 1.1.如果你还没有安装ubuntu 14.04 LTS系统,请参考下面的文章安装 http://blog.csdn.net/lsyz0021/article/details/521 ...
- 【04】图解JSON
[04]图解JSON 附件列表
- python 多线程、多进程、协程性能对比(以爬虫为例)
基本配置:阿里云服务器低配,单核2G内存 首先是看协程的效果: import requests import lxml.html as HTML import sys import time impo ...
- PHP优化之批量操作MySQL
设计一个数据表如下: create table optimization( id INT NOT NULL AUTO_INCREMENT, value VARCHAR(10) NOT NULL, PR ...
- TOJ 4244: Sum
4244: Sum Time Limit(Common/Java):3000MS/9000MS Memory Limit:65536KByteTotal Submit: 63 ...
- hdu 1848 sg——dfs&&打表双实现
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- 信息安全试验-DES加密!
信息安全试验二--DES加密算法 本渣表示没有理解原理,照着书上敲了一发,运行无误! 吐槽:手动S盒简直丧心病狂,扩展置换表全是手动输入,加密原理还是很好理解,两次异或,先混淆. 此代码数据由老师给出 ...