题目链接:http://poj.org/problem?id=3233

解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k,然后结果的每个元素A[i][j] % m。(n <= 30,k < 10^9,m < 10^4)

要用到矩阵快速幂,但我认为最重要的其实还是相加的那个过程,因为k的范围是10^9,一个一个加肯定是不行的,我想了一个办法就是我以k = 8为例说明:

ans = A + A^2 + A^3 + A^4 + A^5 + A^6 + A^7 + A^8

= A + A^2 + A^3 + A^4 + A^4 * (A + A^2 + A^3 + A^4)   // 这样分成两块之后就只要算一次A + A^2 + A^3 + A^4,就可以得出最后结果,

而A + A^2 + A^3 + A^4这个又可以通过相同的方法划分成如下:

= A + A^2 + A^2 * (A + A^2)同理。。。。就可以在logn时间求出他们的和了,然后快速的求A^k次方是用二分矩阵快速幂这里就不说了。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<deque>
#include<cmath>
using namespace std;
typedef __int64 INT;
const int N = ;
int m; struct node
{
INT t[N][N];
int n;
friend node operator * (node a,node b)
{
node B = a;
for(int i = ;i < a.n;++i)
for(int j = ;j < a.n;++j)
{
int tot = ;
for(int k = ;k < a.n;++k)
tot += ((a.t[i][k] * b.t[k][j]) % m);
B.t[i][j] = tot % m;
}
return B;
}
friend node operator + (node a,node b)
{
node B;
B.n = a.n;
for(int i = ;i < a.n;++i)
for(int j = ;j < a.n;++j)
B.t[i][j] = (a.t[i][j] + b.t[i][j]) % m;
return B;
}
};
node A;
void print(node t)
{
for(int i = ;i < t.n;++i)
for(int j = ;j < t.n;++j)
printf(j == t.n-? "%d\n":"%d ",t.t[i][j]);
}
node Pw(node tt,int n) //二分矩阵快速幂求A ^ n
{
if(n == ) return A;
node res;
res.n = tt.n;
memset(res.t,,sizeof(res.t));
for(int i = ;i < res.n;++i)
res.t[i][i] = ;
while(n)
{
if(n & )
res = res * tt;
n >>= ;
tt = tt * tt;
}
return res;
} node get_ans(int n) //求A^1 到 A ^ n的和
{
if(n == )
return A;
if(n & )
{
node temp1;
temp1.n = A.n;
temp1 = Pw(A,n);
node temp2;
temp2.n = ;
temp2 = get_ans(n-);
temp1 = temp1 + temp2;
return temp1;
}
else
{
node temp1;
temp1.n = A.n;
temp1 = Pw(A,n / );
node temp2 = get_ans(n / );
temp2.n = A.n;
temp1 = temp1 * temp2;
temp1 = temp1 + temp2;
return temp1;
}
} int main()
{ int n,k;
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
A.n = n;
for(int i = ;i < n;++i)
for(int j = ;j < n;++j)
scanf("%d",&A.t[i][j]);
node ans = get_ans(k);
print(ans);
}
return ;
}

Poj 3233 Matrix Power Series(矩阵二分快速幂)的更多相关文章

  1. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  2. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  3. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  4. 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] ...

  5. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

  6. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  7. POJ 3233 Matrix Power Series(矩阵等比求和)

    题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...

  8. 矩阵十点【两】 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的迹(就是主对角线上各项的 ...

  9. POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】

    任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K To ...

  10. [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:  ...

随机推荐

  1. 二级联动banner【墨芈原创,大神勿喷】

    这个banner效果在几个月前都做了,不过因为代码添乱,而且不宜调用就没发布,经过2周时间间间断断的编写,插件终于搞定了,除框架外其它都开源发布,至于框架没给源码是因为还没做好,后期做好了也会发布出来 ...

  2. 从日常开发说起,浅谈HTTP协议是做什么的。

    引言 HTTP协议作为Web开发的基础一直被大多数人所熟知,不过相信有很多人只知其一不知其二.比如咱们经常用到的session会话机制是如何实现的,可能很多人都说不出来吧.其实session会话就是H ...

  3. How to set China Azure Storage Connection String

    Configure Visual Studio to access China Azure Storage Open Visual Studio 2012, Server Explorer Add n ...

  4. 我的bootstrap使用的历程

    1.bootstrap快速开发,和amaze一样,同样是自己布局,然后找对应的模板,然后复制. 2.bootstrap实现的不完美的地方,我们要靠自己的样式去解决. 典型的居中布局, containe ...

  5. jquery封装常用方法

    var git = { //true表示格式正确 checkEmail: function (str) { -]{,})(\S*)/g) == null) { return false; } else ...

  6. Grovvy之解析XML文件

    假设现有customer.xml 文件内容如下: <?xml version="1.0" ?> <customers> <corporate> ...

  7. tomcat配置和优化

    转载: https://mp.weixin.qq.com/s?__biz=MzA3MzYwNjQ3NA==&mid=2651296654&idx=1&sn=b04fc6cecf ...

  8. 【转】变量的声明和定义,从C到编译原理到C++,再到Java

    基础学了太久,时间一长有些东西就可能记得不太清楚,俗话说得好,"好记性不如烂笔头",所以把基础中的基础-变量的声明和定义,从C到编译原理到C++,再到Java用烂笔头记录下来 最早 ...

  9. Java算法-堆排序

    package org.rut.util.algorithm.support; import org.rut.util.algorithm.SortUtil; public class HeapSor ...

  10. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...