Matrix Power Series

Time Limit: 3000MS Memory Limit: 131072K

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

Source

POJ Monthly–2007.06.03, Huang, Jinsong

给定矩阵A,求A + A^2 + A^3 + … + A^k的结果

/*
矩阵乘法经典题.
一开始并没有想出来orz.
发现正解好神奇.
这种题就应该先推出递推式子再构造矩阵.
本来还想矩阵套矩阵来着
弱啊.
学习了一下单位矩阵的用法.
题解:http://www.cnblogs.com/justPassBy/p/4448630.html
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 61
#define LL long long
using namespace std;
LL n,m,k,ans[MAXN][MAXN],b[MAXN][MAXN],c[MAXN][MAXN];
LL mul(LL x,LL y)
{
LL tot=0;
while(y)
{
if(y&1) tot=(tot+x)%m;
x=(x+x)%m;
y>>=1;
}
return tot;
}
void mi()
{
while(k)
{
if(k&1)
{
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
for(int k=1;k<=n*2;k++)
c[i][j]=(c[i][j]+ans[i][k]*b[k][j]%m)%m;
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
ans[i][j]=c[i][j],c[i][j]=0;
}
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
for(int k=1;k<=n*2;k++)
c[i][j]=(c[i][j]+b[i][k]*b[k][j]%m)%m;
for(int i=1;i<=n*2;i++)
for(int j=1;j<=n*2;j++)
b[i][j]=c[i][j],c[i][j]=0;
k>>=1;
}
}
void slove()
{
k--;
mi();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
cout<<ans[i][j]<<" ";
printf("\n");
}
}
void Clear()
{
memset(b,0,sizeof b);
memset(ans,0,sizeof ans);
}
int main()
{
while(~scanf("%lld%lld%lld",&n,&k,&m))
{
Clear();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>b[i][j];b[i][j]%=m;
ans[i][j]=ans[i][n+j]=b[i][j];
}
b[n+i][i]=b[n+i][n+i]=1;
}
slove();
}
return 0;
}

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(矩阵高速功率+二分法)

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

  3. poj 3233 Matrix Power Series 矩阵求和

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

  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://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

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

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

  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. POJ 3233 Matrix Power Series (矩阵乘法)

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

随机推荐

  1. Python爬虫快速上手教程

    1 这个是什么        整理Python中requests常用的API 2 代码 from bs4 import BeautifulSoup import requests import re ...

  2. Unity项目 - 简单时钟 Clock

    项目展示 Github项目地址:简单时钟 Clock 制作流程 表盘绘制: 采用Aseprite 像素绘图软件绘制表盘及指针.本例钟表素材大小 256x256,存储格式为png,但发现导入Unity后 ...

  3. C# 文件类中 File ,FileInfo 类的主要区别

    System.IO命名空间中提供的文件操作类有File和FileInfo,这两个类的功能基本相同,只是File是静态类,其中所有方法都是静态的,可以通过类名直接调用,不需要实例化.而FileInfo是 ...

  4. Java调用WebService方法总结(3)--wsimport调用WebService

    wsimport是JDK自带的把WSDL转成Java的工具,可以很方便的生成调用WebService的代码.文中所使用到的软件版本:Java 1.8.0_191. 1.准备 参考Java调用WebSe ...

  5. instanceof 和isInstance

    Java中的instanceof关键字 instanceof是Java的一个二元操作符,和==,>,<是同一类东东.由于它是由字母组成的,所以也是Java的保留关键字.它的作用是测试它左边 ...

  6. ArrayList集合实现RandomAccess接口有何作用?为何LinkedList集合却没实现这接口

    详见:https://blog.csdn.net/weixin_39148512/article/details/79234817 众所周知,在List集合中,我们经常会用到ArrayList以及Li ...

  7. 转 Python3 ssl模块不可用的问题

      编译安装完Python3之后,使用pip来安装python库,发现了如下报错:   $ pip install numpy pip is configured with locations tha ...

  8. 前后端分离架构:Web实现前后端分离,前后端解耦

    一.前言 ”前后端分离“已经成为互联网项目开发的业界标杆,通过Tomcat+Ngnix(也可以中间有个Node.js),有效地进行解耦.并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构 ...

  9. array_pop

    array_pop() 函数删除数组中的最后一个元素,返回数组的最后一个值.如果数组是空的,或者非数组,将返回 NULL. <?php$a=["red","gree ...

  10. (备忘)cron表达式的用法

    一.结构 cron表达式用于配置cronTrigger的实例,实现任务调度的功能. Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格 ...