POJ-3233 Matrix Power Series 矩阵A^1+A^2+A^3...求和转化
S(k)=A^1+A^2...+A^k.
保利求解就超时了,我们考虑一下当k为偶数的情况,A^1+A^2+A^3+A^4...+A^k,取其中前一半A^1+A^2...A^k/2,后一半提取公共矩阵A^k/2后可以发现也是前一半A^1+A^2...A^k/2。因此我们可以考虑只算其中一半,然后A^k/2用矩阵快速幂处理。对于k为奇数,只要转化为k-1+A^k即可。n为矩阵数量,m为矩阵大小,复杂度O[(logn*logn)*m^3]
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#define LL long long
using namespace std; struct mx
{
LL n, m;
LL c[][];//需要根据题目开大
void initMath(LL _n)//初始化方阵
{
m = n = _n;
}
void initOne(LL _n)//初始化单位矩阵
{
m = n = _n;
for (LL i = ; i<n; i++)
for (LL j = ; j<m; j++)
c[i][j] = (i == j);
}
void print()//测试打印
{
for (LL i = ; i<n; i++)
{
for (LL j = ; j < m; j++)
{
cout << c[i][j];
if (j != m - )cout << ' ';
}
cout << endl;
}
}
};
int mod = ;
mx Mut(mx a, mx b)
{
mx c;
c.n = a.n, c.m = b.m;
for (LL i = ; i<a.n; i++)
for (LL j = ; j<b.m; j++)
{
LL sum = ;
for (LL k = ; k<b.n; k++)
sum += a.c[i][k] * b.c[k][j], sum %= mod;
c.c[i][j] = sum;
}
return c;
}
mx fastMi(mx a, LL b)
{
mx mut; mut.initOne(a.n);
while (b)
{
if (b % != )
mut = Mut(mut, a);
a = Mut(a, a);
b /= ;
}
return mut;
}
LL n, k;
mx a, ans, b;
mx s(LL kx)
{
if (kx == )
{
return a;
}
if (kx % ==)
{
mx p = s(kx / );
mx y = fastMi(a, kx/);
y = Mut(y,p);
for (int i = ; i < n; i++)for (int j = ; j < n; j++)
{
y.c[i][j] += p.c[i][j];
y.c[i][j] %= mod;
}
return y;
}
else
{
mx p = s(kx-);
mx y = fastMi(a, kx);
for (int i = ; i < n; i++)for (int j = ; j < n; j++)
{
y.c[i][j] += p.c[i][j];
y.c[i][j] %= mod;
}
return y;
}
}
int main(int argc, const char * argv[]) {
cin.sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
cin >> n >> k;
b.initMath(n);
ans.initMath(n);
a.initMath(n);
for(int i=;i<n;i++)
for (int j = ; j < n; j++)
{
cin >> a.c[i][j];
}
ans = s(k);
ans.print();
}
return ;
}
POJ-3233 Matrix Power Series 矩阵A^1+A^2+A^3...求和转化的更多相关文章
- Poj 3233 Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- 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] ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- POJ 3233 Matrix Power Series(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵十点【两】 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的迹(就是主对角线上各项的 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
随机推荐
- mybatis入门--配置
1.导入jar包 mybatis-x.x.x.jar 导入到lib目录下, 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 pom.xml 文件中: <depen ...
- jsr-303 参数校验—自定义校验注解
1.为什么要自定义? 通过上篇学习,了解到很多常用注解了,但是呢,总是有那么些需求.... 2.案例分析(手机号格式) 2.1.需要验证的实体 Bean public class LoginVo ...
- 公众号获取unionid
然后在微信客户端输入unionid接口的地址(比如发给文件传输助手www.XXX.COM/unionid.php),随便给别人发过去,在点击该链接,就能看到打印的accessToken,openid, ...
- [ Python ] unittest demo
# -*- coding: utf-8 -*- import unittest class MyUT(unittest.TestCase): def test_1(self): print(" ...
- axf 文件包含太多的调试信息,导致的编译错误
构建工程时,提示: build\my_test_prj.axf: Error: L6291E: Cannot assign Fixed Execution Region MCU_FLASH1 Load ...
- 基于注解的Spring事务配置
spring采用@Transactional注解进行事务申明,@Transactional既可以在方法上申明,也可以在类上申明,方法申明优先于类申明. 1.pom配置 包括spring核心包引入以及s ...
- 移动端项目在ios上输入框聚焦难解决方案
由于引入fastclick导致ios端input.textarea输入框难以点击聚焦,解决方案如下: 找到项目中的fastclick依赖或在main.js中改写fastclick的focus实现.
- 史上最全python面试题详解(一)(附带详细答案(持续更新))
1.简述解释型和编译型编程语言? 概念: 编译型语言:把做好的源程序全部编译成二进制代码的可运行程序.然后,可直接运行这个程序. 解释型语言:把做好的源程序翻译一句,然后执行一句,直至结束! 区别: ...
- SliverList , SliverFixedExtentList
SliverList 高度自动, SliverFixedExtentList 高度固定死. CustomScrollView( slivers:[ SliverList( delegate: Sliv ...
- php在cli模式下取得命令行中的参数的方法-getopt命令行可传递数组-简单自定义方法取命令行参数
在cli模式下执行PHP时,自动给脚本文件传递了一个变量$argv,其值即是一个命令中所有值组成的数组(以空格区分),在PHP程序中接收参数有3种方法1.直接使用argv变量数组. 2.使用$_SER ...