题目链接: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. mysqldumpslow使用说明

    mysqldumpslow使用说明 mysqldumpslow --help Usage: mysqldumpslow [ OPTS... ] [ LOGS... ] Parse and summar ...

  2. php 接收表单 方法的区别

    在php中用于接收表单数据的方法有$_REQUEST.$_POST和$_GET.他们作用都是类似的,下面来看一下他们的区别. 一.$_REQUEST能够用于接收post与get方法提交的数据,但是$_ ...

  3. [bzoj 1004][HNOI 2008]Cards(Burnside引理+DP)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1004 分析: 1.确定方向:肯定是组合数学问题,不是Polya就是Burnside,然后题目上 ...

  4. SequoiaDB 系列之七 :源码分析之catalog节点

    这一篇紧接着上一篇SequoiaDB 系列之六 :源码分析之coord节点来讲 在上一篇中,分析了coord转发数据包到catalog节点(也有可能是data节点,视情况而定).这一次,我们继续分析上 ...

  5. AngularJs——grunt神器的使用

    前面我们已经知道了如何安装grunt,本章节给各位道友介绍如何使用 grunt 的插件,grunt是重点在于如何配置使用 Gruntfile.js,官网上也有很多范例. 1,包装函数 module.e ...

  6. iBATIS sqlMapConfig配置详解

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC & ...

  7. Maven 初学(一)基本概念

    Pom中有三个主要元素 Groupid,artifactid,version goupid 是一个组织唯一的标识  例如 com.ibm.www artifactid  是一个工程呢ID        ...

  8. Java设计模式-代理模式(Proxy)

    其实每个模式名称就表明了该模式的作用,代理模式就是多一个代理类出来,替原对象进行一些操作,比如我们在租房子的时候回去找中介,为什么呢?因为你对该地区房屋的信息掌握的不够全面,希望找一个更熟悉的人去帮你 ...

  9. Java基础-静态代理与动态代理比较

    JAVA的静态代理与动态代理比较 静态代理类: 由程序员创建或由特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.动态代理类: 在程序运行时,运用反射机制动态创建 ...

  10. java系统高并发解决方案(转载)

    转载博客地址:http://blog.csdn.net/zxl333/article/details/8454319 转载博客地址:http://blog.csdn.net/zxl333/articl ...