poj3233Matrix Power Series
也是矩阵经典题目 二分递归求解
a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)(偶数)
a+a^2+a^3+..+a^(k/2)+a^(k/2+1)+...+a^k = a+a^2+..+a^k/2+a^k/2(a^1+a^2+..+a^k/2)+a^k。 奇数
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 1e9
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct Mat
{
int mat[][];
};
int n,mod;
Mat operator + (Mat a,Mat b)
{
Mat c;
int i,j;
for(i = ; i < n ;i++)
for(j = ;j < n ;j++)
{
if(a.mat[i][j]+b.mat[i][j]>mod)
c.mat[i][j] = (a.mat[i][j]+b.mat[i][j])%mod;
else
c.mat[i][j] = a.mat[i][j]+b.mat[i][j];
}
return c;
}
Mat operator * (Mat a,Mat b)
{
Mat c;
memset(c.mat,,sizeof(c.mat));
int i,j,k;
for(k = ; k < n ; k++)
{
for(i = ; i < n ;i++)
{
if(a.mat[i][k]==) continue;
for(j = ;j < n ;j++)
{
if(b.mat[k][j]==) continue;
c.mat[i][j] = (c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%mod;
}
}
}
return c;
}
Mat operator ^(Mat a,int k)
{
Mat c;
int i,j;
for(i = ; i < n ;i++)
for(j = ; j < n ;j++)
c.mat[i][j] = (i==j);
for(; k ;k >>= )
{
if(k&) c = c*a;
a = a*a;
}
return c;
}
Mat solve(Mat x,int k)
{
if(k==) return x;
Mat c ;
c = x^k;
Mat a = solve(x,k/);
Mat b = x^(k/);
if(k&) c = a+b*a+c;
else c = a+b*a;
return c;
}
int main()
{
int t;
int i,j;
while(scanf("%d%d%d",&n,&t,&mod)!=EOF)
{
Mat x;
for(i = ; i < n ;i++)
for(j = ; j < n ;j++)
scanf("%d",&x.mat[i][j]);
x = solve(x,t);
for(i = ; i < n ;i++)
{
for(j = ; j < n-; j++)
printf("%d ",x.mat[i][j]%mod);
printf("%d\n",x.mat[i][n-]%mod);
}
}
return ;
}
poj3233Matrix Power Series的更多相关文章
- POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)
http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total ...
- poj3233Matrix Power Series(矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23187 Accepted: ...
- POJ3233Matrix Power Series(矩阵快速幂)
题意 题目链接 给出$n \times n$的矩阵$A$,求$\sum_{i = 1}^k A^i $,每个元素对$m$取模 Sol 考虑直接分治 当$k$为奇数时 $\sum_{i = 1}^k A ...
- C++-POJ3233-Matrix Power Series[矩阵乘法][快速幂]
构造矩阵 #include <cstdio> ; struct Matrix{int a[MAXN][MAXN];}O,I;int N; ;i<MAXN;i++);j<MAXN ...
- POJ 3233Matrix Power Series
妈妈呀....这简直是目前死得最惨的一次. 贴题目: http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Mem ...
- MOOCULUS微积分-2: 数列与级数学习笔记 6. Power series
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- 线性代数(矩阵乘法):POJ 3233 Matrix Power Series
Matrix Power Series Description Given a n × n matrix A and a positive integer k, find the sum S = ...
- C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速
Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...
- POJ 3233 Matrix Power Series(二分等比求和)
Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- form怎样正确post文件
form在HTML中,是用于收集用户输入的,基本全部浏览器都支持form.给form加入method属性.就能实现将用户在form内控件输入的信息POST到制定地址.或发送GET请求. 写了以下一段代 ...
- AsyncSocket中tag參数的用处
tag參数是为了在回调方法中匹配发起调用的方法的,不会加在数据传输中. 调用write方法,等待接收消息.收到消息后,会回调didReadData的delegate方法, delegate方法中的ta ...
- 无限级分类Asp.net Mvc实现
无限级分类Asp.net Mvc实现 无限级分类涉及到异步加载子类.加载当前类和匹配问题,现在做一个通用的实现. (一) 效果如下: (二)设计.实现及使用 (1)数据库 (a)表设计db ...
- hdoj-1242-Rescue【广搜+优先队列】
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 在类的头文件里尽量少引入其它头文件 <<Effective Objective-C>>
与C 和C++ 一样,Objective-C 也使用"头文件"(header file) 与"实现文件"(implementation file)来区隔代码.用 ...
- 关于OutOfMemoryError的处理
转自:http://www.blogjava.net/rosen/archive/2010/05/21/321575.html http://www.blogjava.net/rosen/archiv ...
- OpenvSwitch代码分析之bridge和port
ovs-vsctl add-br br0 会在数据库里面加入新bridge的信息ovs-vsctl add-port br0 eth0 会在数据库里面加入新的port信息 void bridge_ru ...
- Codeforces Beta Round #22 (Div. 2 Only) E. Scheme dfs贪心
E. Scheme To learn as soon as possible the latest news about their favourite fundamentally new ope ...
- 交换分区 在dd命令执行期间 top 其消耗系统约14%的cpu,而mem占比约为0
[资源不友好代码] from pyltp import * d_dir = '/usr/local/ltp_data_v3.4.0/' def gen_one_sentence_part(paragr ...
- python 获取代码宿主机名 ip
1.获取hostname 相同代码 不同宿主机 日志名 互异 且 可识别宿主机 分布式爬虫 https://docs.python.org/3.6/library/socket.html#socket ...