POJ 3233 Matrix Power Serie
题意:给一个n×n的矩阵A,求S = A + A2 + A3 + … + Ak。
解法:从式子中可得递推式S(n) = S(n - 1) + An,An = An-1×A,可得矩阵递推式
[S(n), An] = [S(n - 1), An-1] * [1 0]
[A A] <-orz画不出二维矩阵了
初始状态S(0)为0矩阵,A0为单位矩阵,跑一下矩阵快速幂……
矩阵运算写屎了……调了一下午bugQAQ……矩阵套矩阵什么的好讨厌啊……
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<iomanip>
#define LL long long
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; struct node
{
int a[35][35];
}matrix;
int n = 2, m = 100;
node mul(node a, node b)
{
node res;
memset(res.a, 0, sizeof res.a);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
int tmp = 0;
for(int k = 0; k < n; k++)
{
tmp += a.a[i][k] * b.a[k][j];
tmp %= m;
}
res.a[i][j] = tmp;
}
return res;
}
void ADD(node &a, node b)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
a.a[i][j] += b.a[i][j];
a.a[i][j] %= m;
}
}
void MUL(node a[][2], node b[][2], int x)
{
node res[2][2];
for(int i = 0; i < x; i++)
for(int j = 0; j < 2; j++)
{
node tmp;
memset(tmp.a, 0, sizeof tmp.a);
for(int k = 0; k < 2; k++)
ADD(tmp, mul(a[i][k], b[k][j]));
res[i][j] = tmp;
}
for(int i = 0; i < x; i++)
for(int j = 0; j < 2; j++)
a[i][j] = res[i][j];
}
node POW(int k)
{
node base[2][2];
memset(base[0][0].a, 0, sizeof base[0][0].a);
for(int i = 0; i < n; i++)
base[0][0].a[i][i] = 1;
memset(base[0][1].a, 0, sizeof base[0][1].a);
base[1][1] = base[1][0] = matrix;
node x[1][2];
memset(x[0][0].a, 0, sizeof x[0][0].a);
memset(x[0][1].a, 0, sizeof x[0][1].a);
for(int i = 0; i < n; i++)
x[0][1].a[i][i] = 1;
while(k)
{
if(k & 1)
MUL(x, base, 1);
k >>= 1;
MUL(base, base, 2);
}
return x[0][0];
}
int main()
{
int k;
while(~scanf("%d%d%d", &n, &k, &m))
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
scanf("%d", &matrix.a[i][j]);
matrix.a[i][j] %= m;
}
node ans = POW(k);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(j) printf(" ");
printf("%d", ans.a[i][j]);
}
puts("");
}
}
return 0;
}
POJ 3233 Matrix Power Serie的更多相关文章
- 矩阵十点【两】 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 ...
- 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 (矩阵乘法)
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: ...
- 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 Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
- POJ 3233 Matrix Power Series(矩阵快速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...
随机推荐
- structs spring hibernate 三者之间有什么关系?
现在开发流行MVC模式,structs在C(控制器)中使用:hibernate在M(模型)中被使用:至于 spring ,最大的作用在于,structs.hibernate的对象,由于在各个层之间相互 ...
- PHP字符串中的变量解析(+教你如何在PHP字符串中加入变量)
定义字符串的时候,用单引号或者双引号都是可以的.我个人习惯是用双引号.在输出字符串的时候,若字符串中含有字符串变量,使用单引号和双引号则是有区别的.如下面程序: 1 2 3 4 5 6 7 8 < ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- 关于ios 8 7 下的模态窗口大小的控制 代碼+場景(mainstoryboard)( Resizing UIModalPresentationFormSheet )
1 代碼 UIViewController* modalController = [[UIViewController alloc]init];modalController.modalTransit ...
- TopCoder 603 div1 & div2
div2 250pts MiddleCode 题意:s串长度为奇数时,将中间字符取掉并添加到t末尾:长度为偶数时,将中间两个较小的字符取掉并添加到末尾. 分析:直接做,学习了一下substr(s, p ...
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
- Git教程之工作区和暂存区(5)
工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区:
- Android 自定义Android带图片和文字的ImageButton
经过分析,上述按钮效果实际上就是一个布局,一个最简单不过的垂直线性布局,上部分是一个ImageView,下部分是一个TextView,这个布局可点击.可设置监听. 我们首先要编写自己的ImageBut ...
- 代码自动生成工具_java版
项目结构: 这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后, 我想用代码自动生成我想要的代码,最后生成的效果: 也就是说为每一个bean都生成相应的Dao, ...
- eclipse(STS,myeclipse)老是报ThreadPoolExecutor$Worker.run()
资料地址:http://stackoverflow.com/questions/6290470/eclipse-debugger-always-blocks-on-threadpoolexecutor ...