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写的,总是超时,不知道为什么,之后就改用 ...
随机推荐
- JS日历控件 灵活设置: 精确的时分秒.
在今年7月份时候 写了一篇关于 "JS日历控件" 的文章 , 当时仅仅支持 年月日 的日历控件,如今优化例如以下: 1. 在原基础上 支持 yyyy-mm-dd 的年月 ...
- asp.net mvc 中使用单例
有这样一个service,需要运行的asp.net站点上,但要保证这个实例是唯一的.单例用来启用聊天机器人,保证唯一,以免启动多个,造成客户端发送消息的时候,会造成每个机器人都发送消息,app收到多条 ...
- <s:property>的用法(jsp获取action中的值或者方法)
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法) ...
- ou've likely run out of ephemeral ports on your system
redis.exceptions.ConnectionError: Error 99 connecting to 127.0.0.1:6379. Cannot assign requested add ...
- mongo14-----group,aggregate,mapReduce
group,aggregate,mapReduce 分组统计: group() 简单聚合: aggregate() 强大统计: mapReduce() db.collection.group(docu ...
- HDU3001 Travelling —— 状压DP(三进制)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/ ...
- servlet简单例子1
servlet简单例子1 分类: servlet jsp xml2012-04-18 21:54 3646人阅读 评论(3) 收藏 举报 servletloginjspaction浏览器 LoginS ...
- I.MX6 Android shutdown shell command
/******************************************************************************* * I.MX6 Android shu ...
- python调用window dll和linux so例子
#!/usr/bin/python# -*- coding: UTF-8 -*-#python dll.pyimport win32api# 打开记事本程序,在后台运行,即显示记事本程序的窗口win3 ...
- Tinkoff Challenge - Elimination Round C. Mice problem(模拟)
传送门 题意 给出一个矩形的左下角和右上角的坐标,给出n个点的初始坐标和运动速度和方向,询问是否存在一个时间使得所有点都在矩形内,有则输出最短时间,否则输出-1 分析 对于每个点如果运动过程中都不在矩 ...