矩阵的又一个新使用方法,构造矩阵进行高速幂。

比方拿 nyoj299 Matrix Power Series 来说

给出这样一个递推式: S = A + A2 + A3 +
… + Ak.

让你求s。A是一个矩阵,而k很大。

怎么办呢?

推理发现:Fn = A + A*F(n-1)

然后我们能够构造矩阵:

(Fn 。1 ) =  (Fn-1 ,1) * (A。0。

A,1) = (F1 , 1) * (A,0。

A,1)^K-1

那么我们就能够用一个矩阵高速幂了。

以下是模板题目的代码:

#include <cstdio>
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
int M;
const long long N = 32*2;
long long t,b,c,f1,f2;
struct tree //基础矩阵
{
long long line,cal;
long long a[N+1][N+1];
};
struct Node //构造矩阵
{
long long line,cal;
long long a[N+1][N+1];
Node(tree x)
{
line=x.line*2;
cal=2*x.cal;
for(int i=0;i<x.line*2;i++)
{
for(int j=0;j<x.cal;j++)
{
a[i][j]=x.a[i%x.line][j];
}
}
for(int i=0;i<x.line;i++)
for(int j=x.cal;j<x.cal*2;j++)
a[i][j]=0;
for(int i=x.line;i<2*x.line;i++){
for(int j=x.cal;j<2*x.cal;j++){
if(i==j)
a[i][j]=1;
else
a[i][j]=0;
}
}
}
}; Node isit(Node x,long long c) //矩阵初始化
{
for(long long i=0;i<N;i++)
for(long long j=0;j<N;j++)
x.a[i][j]=c;
return x;
} Node Matlab(Node x,Node s) //矩阵乘法
{
Node ans(x);
ans.line = x.line,ans.cal = s.cal;
ans=isit(ans,0);
for(long long i=0;i<x.line;i++)
{
for(long long j=0;j<x.cal;j++)
{
for(long long k=0;k<s.cal;k++)
{
ans.a[i][j] += x.a[i][k]*s.a[k][j];
ans.a[i][j]=(ans.a[i][j])%M;
}
}
}
return ans;
}
Node Fast_Matrax(tree x,long long n) //矩阵高速幂
{
Node ans(x),tmp(x);
for(int i=0;i<ans.line/2;i++) //chushihua
{
for(int j=0;j<ans.cal;j++)
{
ans.a[i][j]=ans.a[i+ans.line/2][j];
//printf("%d ",ans.a[i][j]);
}
}
ans.line/=2;
while(n>0)
{
if(n%2)
{
ans=Matlab(ans,tmp);
}
tmp=Matlab(tmp,tmp);
n/=2;
}
return ans;
}
int main()
{
int n,k,m;
while(~scanf("%d%d%d",&n,&k,&m))
{
M=m;
tree p;
p.line=n,p.cal=n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&p.a[i][j]);
Node ans=Fast_Matrax(p,k-1);
for(int i=0;i<ans.line;i++)
{
for(int j=0;j<ans.cal/2;j++)
printf("%d ",ans.a[i][j]);
puts("");
}
}
return 0;
}

构造矩阵解决这个问题 【nyoj299 Matrix Power Series】的更多相关文章

  1. POJ 3233 Matrix Power Series(矩阵快速幂)

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

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

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

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

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

  4. [矩阵乘法] PKU3233 Matrix Power Series

    [ 矩 阵 乘 法 ] M a t r i x P o w e r S e r i e s [矩阵乘法]Matrix Power Series [矩阵乘法]MatrixPowerSeries Desc ...

  5. C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

    Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...

  6. POJ 3233 Matrix Power Series (矩阵乘法)

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

  7. [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:  ...

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

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

  9. [POJ3233]Matrix Power Series 分治+矩阵

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [POJ3233]Matrix Power Series 分治+矩阵 题目大意 A为n×n(n<= ...

随机推荐

  1. Android SDK Manager 代理服务器设置

    http://blog.csdn.net/star_splendid/article/details/6939063 自己机子更新的话,速度1KB/s 实在是等不及了~找方法吧 http://www. ...

  2. gdb -Mysql源代码级调试方法

    http://blog.csdn.net/hitzhang/article/details/5985474 gdb -q --batch --ex "set height 0" - ...

  3. printf回到上一行开头以及回到本行开头的方法

    回到上一行开头 #include <stdio.h> #include <unistd.h> int main(void) { ; ){ printf("%d\n&q ...

  4. 图形图像的绘制 GandyDraw

    源代码:下载地址, http://pan.baidu.com/s/1eQIzj3c 具体在下面的这个地方找,

  5. java获取指定日期之前或之后的时间

    /** * 前/后?分钟 * * @param d * @param minute * @return */ public static Date rollMinute(Date d, int min ...

  6. Android如何检查对象的类型

    The instanceof operator compares an object to a specified type. You can use it to test if an object ...

  7. Android 4.4 KitKat升级率已经接近18%(2014-07-09 07:29)

    腾讯数码讯(编 译:张秀梅)按照惯例, 每个月的第一个星期的星期一谷歌都会发布最新一期Android版本分布图.从去年十月末谷歌发布Android 4.4 KitKat以来,截止到目前为止Androi ...

  8. Orchard运用 - 特定主题添加独立代码文件

    今天继续跟大家分享捣鼓Orchard的一些心得.其实有时一些问题或者Bugs还是蛮好解决的,主要看你采取哪种方式方法.比如有时我们为了扩展某些特性或功能,你可以搭建一个全新的模块来完成,如果临时的或简 ...

  9. Node.js:Express 框架

    Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具.使用 Express 可以快速地搭建一个完整功能的网站 ...

  10. ubuntu14.04如何卸载mysql

    1. 删除mysql的数据文件 sudo rm /var/lib/mysql/ -R 2. 删除mqsql的配置文件 sudo rm /etc/mysql/ -R 3. 自动卸载mysql的程序 su ...