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 ...
随机推荐
- crontab 指定执行用户
linux下可以通过配置crontab来定时执行任务,执行体可以是一条系统命令或自己写的一个脚本,同时可以指派用户来执行.配置crontab有两种方法.方法1.使用crontab命令,例如添加一个新的 ...
- IText 生成页脚页码
做doc文档报表的时候可能遇到这样的需求: 每一个页面需要页码,用IText可以完成这样的需求. IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.ja ...
- MySQL 卸载 --Mac
pkill mysql sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems ...
- [hackerrank]The Love-Letter Mystery
https://www.hackerrank.com/contests/w3/challenges/the-love-letter-mystery 简单题. #include <cstdlib& ...
- [iOS]修改开发者中心Bundle Identifier的一些配置
登录开发者中心https://developer.apple.com 然后找到你的Bundle Identifier. 这里暂时只讲开启推送的功能,如果需要别的直接勾选前面的选择框 然后拉到最下面点击 ...
- Redhat 使用中文安装后更换为英文的设定
vi /etc/sysconfig/i18n将LANG改为LANG=en_US.UTF-8保存退出,重新reboot
- Android 线程通讯类Handler
handler是线程通讯工具类.用于传递消息.它有两个队列: 1.消息队列 2.线程队列 消息队列使用sendMessage和HandleMessage的组合来发送和处理消息. 线程队列类似一段代码, ...
- 4、JPA table主键生成策略(在JPA中table策略是首推!!!)
用 table 来生成主键详解 它是在不影响性能情况下,通用性最强的 JPA 主键生成器.这种方法生成主键的策略可以适用于任何数据库,不必担心不同数据库不兼容造成的问题. initialValue不起 ...
- 计算机技能get(windows系统)
1.快速打开程序,比如计算器,注册表,先按win键(不用再按win+r啦),输入程序名字,如calc,regedit等,直接打开. 2.自动左右分屏,win+上下左右方向键,win+↑ 最大化,win ...
- 10个用于Web开发的最好 Python 框架
Python 是一门动态.面向对象语言.其最初就是作为一门面向对象语言设计的,并且在后期又加入了一些更高级的特性.除了语言本身的设计目的之外,Python标准 库也是值得大家称赞的,Python甚至还 ...