【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: ...
随机推荐
- linux目录文件操作
一.linux系统目录结构 1.顶层根目录 顶层根目录使用 “/”来表示 2.linux中的一些重要目录 (1)bin目录 放置常用的可执行文件(其中ls命令位列其中) (2)sbin目录 放置系统的 ...
- window.onload 函数不执行处理
Google Chrome window.onload = function(){} 函数不执行处理 function pageOnLoad(){} window.onload = pageOnL ...
- hdu 4788
#include<stdio.h> #include<math.h> int main() { int a; double d; char s],ch; for;i++) d; ...
- 国内程序员的十大疑问之一:为什么老外不愿意用MyBatis?
老外用MyBatis吗 昨天我在我在知乎看到了一张比较Hibernate和MyBatis使用情况的图,顺手发了条朋友圈: Hibernate vs MyBatis ,谁能告诉我什么样的国情导致了这么大 ...
- 【Tomcat】tomcat logs 目录下各日志文件的含义
tomcat每次启动时,自动在logs目录下生产以下日志文件,按照日期自动备份.可以帮助我们更好的找出错误. 一. 认识各种目录的作用及记录的信息 目录
- nyoj_10_skiing_201405181748
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- POJ 2240_Arbitrage
题意: 给定一系列货币汇率,求能否从一种货币,经过一系列转换,最终转化回更高价值的该种货币. 分析: 即为求最长路并判断是否存在"正"权值回路,这里用的bellmanford算法. ...
- Delphi ADO的Lookup类型字段的问题
关于ADO数据集控件中的Lookup类型字段,在其Lookupkeyfields属性指向的字段中存在NULL值的,就会出现'EOleException with message '发生未知错误',这个 ...
- 动态演示冒泡排序java
动态演示冒泡排序java //冒泡排序是一种简单的交换排序,基本思路,从数列左边开始扫描元素,在扫描过程中依次对相邻元素进行比较,将较大元素后移. public class NumberSort { ...
- MongoDB小结21 - find【游标】
数据库使用游标来控制find的执行结果. 客户端对游标的实现通常能够对最终结果进行有效控制. 可以限制结果的数量,略过部分结果,对任意方向任意键的组合对结果进行排序,或者去执行一些功能强大的操作. 我 ...