Matrix Power Series

Time Limit: 3000MS Memory Limit: 131072K

Total Submissions: 19338 Accepted: 8161

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

可以找到递推关系 : s[k]=s[k-1]+A^k;

然后构造矩阵,利用矩阵快速幂

具体见代码

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h> using namespace std;
int n,k;
int m;
struct Node
{
int a[65][65]; };
Node multiply(Node a,Node b)
{
Node c;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
c.a[i][j]=0;
for(int k=1;k<=n;k++)
{
(c.a[i][j]+=(a.a[i][k]*b.a[k][j])%m)%=m;
}
}
}
return c;
}
Node quick(Node a,int x)
{
Node c;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
c.a[i][j]=(i==j?1:0);
for(x;x>0;x>>=1)
{
if(x&1)
c=multiply(c,a);
a=multiply(a,a);
}
return c;
}
int main()
{
while( scanf("%d%d%d",&n,&k,&m)!=EOF)
{
Node a;Node b;Node c;
memset(a.a,0,sizeof(a.a));
memset(b.a,0,sizeof(b.a));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&a.a[i][j+n]);
b.a[i+n][j+n]=a.a[i][j+n];
}
for(int i=1;i<=n;i++)
{
b.a[i][i]=1;
b.a[i+n][i]=1;
}
n=n*2;
c=multiply(a,quick(b,k));
for(int i=1;i<=n/2;i++)
for(int j=1;j<=n/2;j++)
if(j==n/2)printf("%d\n",c.a[i][j]);
else printf("%d ",c.a[i][j]);
}
return 0;
}

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

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

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

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

  3. POJ 3233:Matrix Power Series 矩阵快速幂 乘积

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

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

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

  5. POJ3233:Matrix Power Series(矩阵快速幂+二分)

    http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...

  6. poj 3233 Matrix Power Series 矩阵求和

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

  7. POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵

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

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

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

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

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

  10. POJ3233:Matrix Power Series(矩阵快速幂+递推式)

    传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...

随机推荐

  1. linode使用apt更新时停止的错误

    使用ubuntu在更新系统时候,有时候会出现这样的问题: 0% [Connecting to security.ubuntu.com (2001:67c:1560:8001::14)] 问了下客户,他 ...

  2. [k8s]metricbeat的kubernetes模块&kube-metric模块

    正确姿势启动metricbeat metricbeat.modules: - module: system metricsets: - cpu - filesystem - memory - netw ...

  3. [Objective C]super dealloc 调用时机

    转自:http://dcm19872007.blog.163.com/blog/static/86519374201311953739818/ objective-c 语言中最头疼的事就是内存释放,申 ...

  4. Python黑魔法,一行实现并行化

    Python 在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和 GIL,我觉得错误的教学指导才是主要问题.常见的经典 Python 多线程.多进程教程多显得偏“重”.而且往往隔靴搔 ...

  5. nginx源代码分析--GDB调试

    利用gdb[i]调试nginx[ii]和利用gdb调试其他程序没有两样,只是nginx能够是daemon程序,也能够以多进程执行,因此利用gdb调试和寻常会有些许不一样. 当然,我们能够选择将ngin ...

  6. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

  7. tornado长轮询

    1.什么是长轮询顾名思义,长轮询就是不停循环请求服务器,获取最新信息.长轮询分为两类:1)浏览器以固定时间间隔向服务器发送请求缺点是轮询频率要足够快,但又不能太频繁,否则当成百上千个客户端不断请求,会 ...

  8. 处理图片(updated)

    高像素的图片,比如分辨率为 7712x4352 的照片,当加载到一个 bitmap 中时会占用相当大的内存. 每个像素会占用 4个字节的内存,所以当没有被压缩时,全部的图片会占用 12800万字节(约 ...

  9. xeno 实时性能测试 在100us的采样周期的测试数据

    1 xeno 用户层测试时间: root@sama5d3-linux:/usr/bin latency -t0 -T25 -p100== Sampling period: 100 us== Test ...

  10. Keil的使用方法(汇总)

    推荐 分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来! http://www.captainbed.net/strongerhuang 软件的开发 ...