题目地址:http://poj.org/problem?id=3233

题意:给你一个矩阵A,让你求A+A^2+……+A^k模p的矩阵值

题解:我们知道求A^n我们可以用二分-矩阵快速幂来求,而

当k是奇数A+A^2+……+A^k=A^(k/2+1)+(A+A^2+……A^(k/2))*(1+A^(k/2+1))

当k是偶数A+A^2+……+A^k=(A+A^2+……A^(k/2))*(1+A^(k/2))

可以在一次用二分。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef long long LL;
const int N=31;
const int mod=1000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); int n,k,p; struct M
{
int m[N][N];
}; void print(M t)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<n;j++)
printf("%d ",t.m[i][j]);
printf("%d\n",t.m[i][n]);
}
} M xh_mod(M a)
{
M t;
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
t.m[i][j]=a.m[i][j]%p;
return t;
} M xh_mult(M a,M b)
{
M t;
int i,j,k;
memset(t.m,0,sizeof(t.m));
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++)
t.m[i][j]=(t.m[i][j]+a.m[i][k]*b.m[k][j])%p;
return t;
} M xh_pow(M a,int b)
{
M t;
memset(t.m,0,sizeof(t.m));
for(int i=1;i<=n;i++)
t.m[i][i]=1;
while(b)
{
if(b&1) t=xh_mult(t,a);
a=xh_mult(a,a);
b/=2;
}
return t;
} M xh_add(M a,M b)
{
M t;
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
t.m[i][j]=(a.m[i][j]+b.m[i][j])%p;
return t;
} M love(M a,int k)
{
M t,x;
if(k==1)
{
t=a;
return t;
}
x=love(a,k/2);
if(k&1)
{
M o=xh_pow(a,k/2+1);
return xh_add(xh_add(x,o),xh_mult(x,o));
}
else
{
M o=xh_pow(a,k/2);
return xh_add(x,xh_mult(x,o));
}
} int main()
{
int i,j;
while(~scanf("%d%d%d",&n,&k,&p))
{
M a,t;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a.m[i][j]);
t=xh_mod(a);
a=love(t,k);
print(a);
}
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(矩阵乘法)

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

  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 矩阵求和

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

  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 3233 Matrix Power Series(矩阵等比求和)

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

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

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

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

  10. [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. 自定义android精美聊天界面

    编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...

  2. c-连接两个链表

    概述 还是相对简单,不过要记得释放不用的头结点即可. 代码为: //将lList2头结点连接在lList1尾结点的后面. void combine(linklist lList1, linklist ...

  3. WPF 弱事件

    因为在接触WPF的过程中追查INotifyPropertyChanged的通知原理的时候,发现了 PropertyChangedEventManager这个类,它是继承与WeakEventManage ...

  4. Python文件之----XML

    #coding=utf-8 from xml.dom import minidom from xml.dom.minidom import Document import xml def writeX ...

  5. qt实现类似QQ伸缩窗口--鼠标事件应用

    原创文章,引用请保证原文完整性,尊重作者劳动,原文地址http://blog.csdn.net/hiwubihe/article/details/38678305,qq:1269122125. 上一章 ...

  6. jQuery图片滑动

    一个非常简单实用的jQuery插件 可以用在页面的顶部广告展示 http://slidesjs.com/ 一个需要注意的问题, 就是在手机等客户端(IOS8以上), 使用此插件时, 经常会触发插件的r ...

  7. Adb shell 常用命令

    1. 查看IP adb shell netcfg 2. 查看挂载设备 adb devices 3. 将本地端口转发至手机端口 adb forward tcp: tcp: // PC上所有6100端口通 ...

  8. 关于超链接自动提示的demo

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. thinkphp excel txt文件上传实现

    <?php/************************************************************************************** ***  ...

  10. jquery hide() show()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...