题目地址: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. (转)如何将ecshop首页主广告位的flash轮播替换为js轮播

    转之--http://www.ecshoptemplate.com/article-1710.html 这个ecshop很常见,因为现在比起flash难以修改,js更加符合人们的使用习惯,而默认ecs ...

  2. 加速器eaccelerator不兼容高版本php

    话说PHP官方发布PHP5.4已经有一阵了,根据使用的情况来看,似乎还是很不错的.从初始发布到现在升级到的PHP5.4.4,修正不少的Bug.PHP5.4新的版本,除了提供了更多新的特性,还有大幅的效 ...

  3. UIToolbar+UIWebView 浏览器

    创建界面 var webView : UIWebView! var toolBar : UIToolbar! let swiftWH = UIScreen.mainScreen().bounds.si ...

  4. UIGestureRecognizer手势识别

    UIGestureRecognizer 1.#import "ViewController.h"2.3.@interface ViewController ()<UIGest ...

  5. Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解

    文件的加载,只需要三步就够了,废话不多说,来直接的吧. 一.建表 话不多说,直接开始. 建表,对于日志文件来说,最后有分区,在此案例中,对年月日和小时进行了分区. 建表tracktest_log,分隔 ...

  6. linux 命令及进程控制

    main.c  main.o/main.obj  main/main.exe          编译                连接 程序运行;      两步: gcc/g++  -c  mai ...

  7. Java学习----构造方法的重载

    一个类中有多个同名的参数不一样(参数的个数,参数的类型,参数的顺序)的构造方法 public class Student { public Student() { System.out.println ...

  8. element的height与width

    关于一个element所有的高度宽度 ele.style.width,ele.style.height:操纵style样式.+"px" offsetWidth.offsetHeig ...

  9. Math.round()、Math.ceil()、Math.floor()与Math.random()的区别?

    Math.round(x) 四舍五入 加上0.5向下取整 Math.round(1.5) 2 Math.round(-11.5) -11 Math.round(-11.2) -10 Math.ceil ...

  10. iOS使用VLC

    简       注册登录 添加关注 作者 牵线小丑2016.03.18 10:42 写了4836字,被38人关注,获得了43个喜欢 iOS使用VLC 字数946 阅读698 评论1 喜欢14 简介 库 ...