题目地址: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. 使用WMI来连接远端计算机

    1. wmi连接前提 利用wmi来连接远端计算机首先要具有远端计算机管理员的用户名和密码.如果计算机在域中的话,要有域管理员用户名和密码,或者是把域帐户加入本机管理员组中也可以. 2. 相关类的用法- ...

  2. Google与微软为jQuery等类库提供的CDN服务

    相关链接: Google:  http://code.google.com/apis/ajaxlibs/Microsoft:  http://www.asp.net/ajaxlibrary/cdn.a ...

  3. Access数据库数据转换Table.Json

    使用WPF组件 xaml <Window x:Class="JsonConvert.MainWindow" xmlns="http://schemas.micros ...

  4. [转帖]了解AmbiLight知识

    了解科技前沿的方法..American Online=AOL.algorithm算法.(Denzel say, and I don't know) Engadget瘾科技网站.英文版Engadget网 ...

  5. java对mysql数据库进行单表筛选备份、还原操作

    最近在做的一个项目需要对mysql数据库中的单个表格进行备份 其中,一部分表格需要进行筛选备份(例如对最近插入的1000条记录进行备份) 思路:java调用系统命令完成备份操作 假设现在有数据库tes ...

  6. ONVIF Event消息解析(How to work with gSoap)

    Prepare Requirements ONVIF Event gSoap reference ONVIF Specification 问题描述 Event是ONVIF核心规范中一块, 文档解释了如 ...

  7. Get URL parameters & values with jQuery

    原文: http://jquery-howto.blogspot.jp/2009/09/get-url-parameters-values-with-jquery.html In this post, ...

  8. The ultimate jQuery Plugin List(终极jQuery插件列表)

    下面的文章可能出自一位奥地利的作者,  列出很多jQuery的插件.类似的网站:http://jquerylist.com/原文地址: http://www.kollermedia.at/archiv ...

  9. VBoxManage命令详解

    转自:http://zhang-ly520.iteye.com/blog/300606 由于最近工作对vbox有一定涉猎,发现这个写的比较好,先转来,稍有空时再根据自己的心得整理一下. VBoxMan ...

  10. 19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)

    题目意思:去掉链表中倒数第n个节点 思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了   2.一次遍历,两个指针,用指针间的距离去计算. ps:特别注意删掉 ...