设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]

则有递推式B[K] = T*B[k-1],即有B[k] = T^k*B[0],令S[0] 为n*n的0矩阵。

矩阵快速幂求出即可·····

还可以使用两次分治的方法····自行百度····

贴代码:

 #include<cstdio>
#include<cstring>
int n,k,p,d;//d = 2*n
struct matrix
{
int m[][];
} A;
matrix mul(int a[][],int b[][])
{
matrix ans;
memset(ans.m,,sizeof(ans.m));
for(int i=; i<=d; ++i)
for(int j=; j<=d; ++j)
for(int k=; k<=d; ++k)
ans.m[i][j] = (ans.m[i][j] + a[i][k]*b[k][j]%p)%p;
return ans;
}
matrix qPow()
{
matrix ans;
memset(ans.m,,sizeof(ans.m));
for(int i=; i<=d; ++i)
ans.m[i][i] = ;
while(k)
{
if(k&) ans = mul(ans.m,A.m);
A = mul(A.m,A.m);
k >>= ;
}
return ans;
}
int main()
{
// freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&k,&p))
{
memset(A.m,,sizeof(A.m));
int t[][];
for(int i=; i<=n; ++i)
{
for(int j=; j<=n; ++j)
{
scanf("%d",&A.m[i][j]);
t[i][j] = A.m[i][j];
}
}
for(int i=n+; i<=*n; ++i)
A.m[i][i-n] = ,A.m[i][i] = ;
d = n<<;
matrix ans = qPow();
for(int i=n+; i<=d; ++i)
{
for(int j=; j<=n; ++j)
{
int res =;
for(int k=; k<=n; ++k)
res = (res + ans.m[i][k]*t[k][j]%p)%p;
if(j != ) printf(" ");
printf("%d",res);
}
puts("");
}
}
return ;
}

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

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

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

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

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

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

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

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

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

  5. poj 3233 Matrix Power Series 矩阵求和

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

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

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

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

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

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

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

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

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

随机推荐

  1. Service相关--读书笔记

    2013-12-30 18:16:11 1. Service和Activty都是从Context里面派生出来的,因此都可以直接调用getResource(),getContentResolver()等 ...

  2. 使用AppCan自带的升级功能实现移动端升级

    1.需要在AppCan项目的config.xml文件中设置“更新地址”,即在执行uexWidget.checkUpdate();时访问的后台页面地址,比如: http://192.168.0.10:8 ...

  3. [windows驱动]windows8.1驱动调试前戏

    人们都说在干正事之前,得先做足前戏才会爽,我一直很认同这个观点,下面我来总结下进行windows8.1的WDK调试所要做的准备工作. 软件安装: 1.VS2013. 2.WDK8.1 3.Window ...

  4. 虚拟机centos配置ip

    涉及到三个配置文件,分别是: /etc/sysconfig/network /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/resolv.conf /et ...

  5. memcpy的用法及实现

    memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中,返回dest所指内存地址的起始位置. #include <string.h&g ...

  6. C++中的数组与指针

    数组与指针看起来很像 int a[] = {1, 2 ,3}; int *p = a; 如此,我们可以p[0], p[1], p[2] 看起来,与直接使用数组名没什么两样,但是看这段代码 sizeof ...

  7. Python 温习

    关于Python内置函数的示例 Type "copyright", "credits" or "license()" f重写or more ...

  8. susy-Toolkit 之翻译

    Toolkit工具包 The Susy 2.0 toolkit is built around our shorthand syntax. Use the shorthand to control e ...

  9. Corporative Network_并查集

    Description A very big corporation is developing its corporative network. In the beginning each of t ...

  10. 4、C#基础整理(if语句经典习题)

    1.用 if 判断输入的是否是空格键的方法:(Console.ReadKey()的用法) ConsoleKeyInfo readkey = Console.ReadKey(); Console.Wri ...