【POJ 3233】Matrix Power Series
【题目链接】
【算法】
要求 A^1 + A^2 + A^3 + ... + A^k
考虑通过二分来计算这个式子 :
令f(k) = A^1 + A^2 + A ^ 3 + ... + A^k
那么,当k为奇数时,f(k) = f(k-1) + A ^ k
当k为偶数时,f(k) = f(n/2) + A ^ (n/2) * f(n/2)
因此,可以通过二分 + 矩阵乘法快速幂的方式,在O(n^3log(n)^2)的时间内解决此题
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 35 int i,j,n,k,m;
struct Matrix
{
int mat[MAXN][MAXN];
} a,ans; inline Matrix add(Matrix a,Matrix b)
{
int i,j;
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % m;
}
}
return ans;
}
inline Matrix mul(Matrix a,Matrix b)
{
int i,j,k;
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
for (k = ; k <= n; k++)
{
ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % m;
}
}
}
return ans;
}
inline Matrix power(Matrix a,int m)
{
Matrix ans,p = a;
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
ans.mat[i][j] = (i == j);
}
}
while (m > )
{
if (m & ) ans = mul(ans,p);
p = mul(p,p);
m >>= ;
}
return ans;
}
Matrix solve(int n)
{
Matrix tmp;
if (n == ) return a;
if (n % == )
{
tmp = solve(n/);
return add(tmp,mul(power(a,n/),tmp));
} else return add(solve(n-),power(a,n));
} int main()
{ scanf("%d%d%d",&n,&k,&m);
for (i = ; i <= n; i++)
{
for (j = ; j <= n; j++)
{
scanf("%d",&a.mat[i][j]);
}
}
ans = solve(k);
for (i = ; i <= n; i++)
{
for (j = ; j < n; j++)
{
printf("%d ",ans.mat[i][j]);
}
printf("%d\n",ans.mat[i][n]);
} return ; }
【POJ 3233】Matrix Power Series的更多相关文章
- POJ 3233:Matrix Power Series 矩阵快速幂 乘积
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 18450 Accepted: ...
- 【poj3233】 Matrix Power Series
http://poj.org/problem?id=3233 (题目链接) 题意 给出一个n×n的矩阵A,求模m下A+A2+A3+…+Ak 的值 Solution 今日考试就A了这一道题.. 当k为偶 ...
- 【POJ - 3685】Matrix(二分)
Matrix Descriptions 有一个N阶方阵 第i行,j列的值Aij =i2 + 100000 × i + j2 - 100000 × j + i × j,需要找出这个方阵的第M小值. In ...
- 【POJ - 2078】Matrix(dfs)
-->Matrix Descriptions: 输入一个n×n的矩阵,可以对矩阵的每行进行任意次的循环右移操作,行的每一次右移后,计算矩阵中每一列的和的最大值,输出这些最大值中的最小值. Sam ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series 【经典矩阵快速幂+二分】
任意门:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K To ...
- 矩阵十点【两】 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 (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
- [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: ...
随机推荐
- IDEA基本使用及配置(2)
IDEA配置:File >> Setiings进入配置界面 1.主题配置:默认两种主题,黑色.白色,可以自己在网上下载,然后File >> Import Setiings导入, ...
- 一次偶遇 IOException
今天遇到了个挺有意思的事情,为了实现上传头像的功能,这是需要实现的功能,项目在本地跑起来且在本地 服务器(tomcat)也实现了头像上传,并把头像路径存入数据库,但是却在上传到线上服务器的时候死活存不 ...
- 【BZOJ 2118】 墨墨的等式(Dijkstra)
BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...
- 设计方案--移动端延迟300ms的原因以及解决方案
一.前言 移动端浏览器提供一个特殊的功能:双击(double tap)缩放. 二.移动端延迟300ms的原因 为什么要用触摸事件?触摸事件是移动端浏览器特有的html5事件. 因为移动端的clic ...
- .DS_Store的说明
今天清理电脑时,突然发现好像有文件的地方都会出现一个.DS_Store文件,今天有时间,索性就查了一下,并做总结发表一篇吧,怕有什么影响,并未真正实施,仅仅供自己收藏,仅供大家参考. DS_ ...
- 【转】Asp.net MVC Comet推送
原文链接:http://www.cnblogs.com/kissdodog/p/4283485.html 一.简介 在Asp.net MVC实现的Comet推送的原理很简单. 服务器端:接收到服务器发 ...
- Leetcode 152.乘机最大子序列
乘积最大子序列 给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 ...
- 【bzoj2527】[Poi2011]Meteors(树状数组(单点查询,区间修改)+整体二分)
[bzoj2527][Poi2011]Meteors Description Byteotian Interstellar Union (BIU) has recently discovered a ...
- Tyvj 1176 火焰巨魔的惆怅
Tyvj 1176 火焰巨魔的惆怅 背景 TYVJ2月月赛第一道 巨魔家族在某天受到了其他种族的屠杀,作为一个英雄,他主动担任了断后的任务,但是,在巨魔家族整体转移过后,火焰巨魔却被困住了,他出逃的方 ...
- hdu - 2851 Lode Runner (最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2851 首先有n层,每层的路径都有一个起点和终点和对应的危险值,如果某两层之间有交集,就能从这一层上到另外一层,不 ...