http://poj.org/problem?id=3233

题解 矩阵快速幂+二分等比数列求和

AC代码

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f,mod = ;
const double epx = 1e-;
typedef long long ll;
struct Matrix
{
int m[maxn][maxn]; //有哪位大佬知道为什么数组开大了会 Runtime Error
};
Matrix A,I;
int n,m,k;
Matrix add(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
c.m[i][j]=a.m[i][j]+b.m[i][j];
c.m[i][j]%=m;
}
}
return c;
}
Matrix multi(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
c.m[i][j]=;
for(int k=;k<n;k++)
{
c.m[i][j]+=a.m[i][k]*b.m[k][j];
}
c.m[i][j]%=m;
}
}
return c;
}
Matrix poww(Matrix a,int n)
{
Matrix ans=I;
while(n>)
{
if(n&)
ans=multi(ans,a);
n=n>>;
a=multi(a,a);
}
return ans;
}
Matrix sum(Matrix a,int n)
{
if(n==) return a;
Matrix t=sum(a,n/);
if(n&)
{
Matrix cur=poww(a,n/+);
t=add(t,multi(t,cur));
t=add(t,cur);
}
else
{
Matrix cur=poww(a,n/);
t=add(t,multi(t,cur));
}
return t;
}
int main()
{
cin>>n>>k>>m;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cin>>A.m[i][j];
A.m[i][j]%=m;
I.m[i][j]=(i==j);
}
}
Matrix ans=sum(A,k);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
cout<<ans.m[i][j]<<" ";
}
cout<<endl;
}
return ;
}

poj 3233 Matrix Power Series 矩阵求和的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. [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. C#_JDBC连接数据库

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. Java实现三角形计数

    题: 解: 这道题考的是穷举的算法. 一开始看到这道题的时候,本能的想到用递归实现.但使用递归的话数据少没问题,数据多了之后会抛栈溢出的异常.我查了一下,原因是使用递归创建了太多的变量, 每个变量创建 ...

  3. Caused by: javax.el.PropertyNotFoundException: Property 'product' not found on type java.lang.String

    今天在JSP利用EL表达式取值报了 "javax.el.PropertyNotFoundException”,经过debug和打印将问题定位到这段代码: HTML应该是没啥问题,看提示在ja ...

  4. [BZOJ1046][HAOI2007]上升序列 DP+贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1046 我们先求出对于每一个数字作为开头的LCS的长度f[i],最长的f[i]为mxlen. ...

  5. 虚拟机下安装 CentOS 7 的几个小问题

    ※ 网络问题(Destination Host Unreachable) 安装时网络选择的"桥接"模式, 安装完毕,并配置IP地址后,发现只能ping通自己,局域网内的其他IP无法 ...

  6. vue2.0版本指令v-if与v-show的区别

    v-if: 判断是否加载,可以减轻服务器的压力,在需要时加载. v-show:调整css dispaly属性,可以使客户端操作更加流畅. v-if示例: <!DOCTYPE html> & ...

  7. IOS 面试题系列

    随着iOS平台开发的职位的增加,笔试.面试也越来越有“套路”,这里我总结了一些面试题,多数是Objective-C的基础知识,适合于面试新人,答案是我自己答的,不准确的地方,欢迎指出. 1.   Ob ...

  8. Android(java)学习笔记193:ContentProvider使用之获得系统联系人信息01

    1.系统联系人的数据库(3张最重要的表) (1)raw_contacts  联系人表        保存联系人的id   contact_id (2)data 数据表       保存联系人的数据 ( ...

  9. shellinabox的安装使用

    一.简介 Shell In A Box(发音是shellinabox)是一款基于Web的终端模仿器,由Markus Gutschke开辟而成.它有内置的Web办事器,在指定的端口上作为一个基于Web的 ...

  10. 打开windows服务

    #include <winsvc.h> void CXXXDlg::ServiceRun() { SERVICE_STATUS ssStatus; //获得ServiceControl M ...