POJ 3233:Matrix Power Series 矩阵快速幂 乘积
| Time Limit: 3000MS | Memory Limit: 131072K | |
| Total Submissions: 18450 | Accepted: 7802 |
Description
Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak.
Input
The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m < 104). Then follow n lines each containing n nonnegative
integers below 32,768, giving A’s elements in row-major order.
Output
Output the elements of S modulo m in the same way as A is given.
Sample Input
2 2 4
0 1
1 1
Sample Output
1 2
2 3
题意很简单,就是矩阵相乘,然后求和。自己做的时候快速幂,发现快速幂竟然还是TLE。
不知道怎么搞,看了网上的代码,发现这个求和的深搜sum2很经典,充分利用偶数求和,假设是求1到6的和,先将6除以2,求1到3的和,然后对1到3的和 乘以3就是4到6的和,再一相加就是1到6的和。这段代码的思想很巧妙,很喜欢。以后求1到n的和时候可以用得上~
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; struct matrix {
int m[35][35];
}; int n, mod;
long long ko;
matrix no; matrix mu(matrix no1, matrix no2)
{
matrix t;
memset(t.m, 0, sizeof(t.m)); int i, j, k;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
for (k = 1; k <= n; k++)
{
t.m[i][j] += no1.m[i][k] * no2.m[k][j];
if (t.m[i][j] >= mod)
{
t.m[i][j] %= mod;
}
}
}
}
return t; } matrix multi(matrix no, long long x)
{
matrix b;
memset(b.m, 0, sizeof(b.m));
int i;
for (i = 1; i <= n; i++)
{
b.m[i][i] = 1;
}
while (x)
{
if (x & 1) b = mu(b, no);
x = x >> 1;
no = mu(no, no);
}
return b;
} matrix add(matrix no1, matrix no2)
{
matrix t; int i, j; for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
t.m[i][j] = no1.m[i][j] + no2.m[i][j];
if (t.m[i][j] >= mod)
{
t.m[i][j] %= mod;
}
}
}
return t;
} matrix sum2(long long i)//假设i为7
{
if (i == 1)return no;
if (i & 1)
return add(multi(no, i), sum2(i - 1));//7+6+...
else
{
long long k = i >> 1;//3
matrix s = sum2(k);//1 2 3
return add(s, mu(s, multi(no, k)));1 2 3 + 4 5 6
} } int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i, j;
cin >> n >> ko >> mod; for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &no.m[i][j]);
if (no.m[i][j] >= mod)
{
no.m[i][j] %= mod;
}
}
}
no = sum2(ko);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (j == 1)
cout << no.m[i][j];
else
cout << " " << no.m[i][j];
}
cout << endl;
}
//system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3233:Matrix Power Series 矩阵快速幂 乘积的更多相关文章
- POJ 3233 Matrix Power Series 矩阵快速幂
设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- poj 3233 Matrix Power Series(矩阵二分,高速幂)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- POJ3233:Matrix Power Series(矩阵快速幂+二分)
http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...
- poj 3233 Matrix Power Series 矩阵求和
http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 27277 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(矩阵高速功率+二分法)
职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9. 这 ...
- POJ3233:Matrix Power Series(矩阵快速幂+递推式)
传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...
随机推荐
- 「CF383C Propagating tree」
这应该属于一个比较麻烦的数据结构处理树上问题. 题目大意 给出一颗根节点编号为 \(1\) 的树,对于一个节点修改时在它的子树中对于深度奇偶性相同的节点加上这个权值,不同则减去这个值,单点查询. 分析 ...
- 2019年springmvc面试高频题(java)
前言 2019即将过去,伴随我们即将迎来的又是新的一年,过完春节,马上又要迎来新的金三银四面试季.那么,作为程序猿的你,是否真的有所准备的呢,亦或是安于本职工作,继续做好手头上的事情. 当然,不论选择 ...
- swiper插件遇到的坑
1.网速卡的情况下轮播图会出现塌陷 解决方法: 在swiper外层固定高度,用填充百分比方法: html代码:在swiper-container加一层外层,外层用padding-top:50%(看图片 ...
- Cookie信息保存到本地(MozillaCookieJar)
from urllib import request from http.cookiejar import MozillaCookieJar cookiejar = MozillaCookieJar( ...
- mysql cmmand not found
https://www.cnblogs.com/yangzigege/p/8337393.html
- 【Python数据挖掘】第六篇--特征工程
一.Standardization 方法一:StandardScaler from sklearn.preprocessing import StandardScaler sds = Standard ...
- 37 java序列化与反序列化
一.java序列化与反序列化 1.序列化: 是指把java对象转换为字节序列的过程: 2.反序列化:是指把字节序列恢复为java对象的过程. 二.为什么要序列化 我们知道,当两个进程进行远程通信时,可 ...
- yolo系列目标检测+自标注数据集进行目标识别
1. yolov1的识别原理 参考:https://blog.csdn.net/u010712012/article/details/85116365 https://blog.csdn.net/gb ...
- alert \ confirm \ prompt
alert() : 会将()中的内容弹出,返回的是()中的内容值,也就是字符串值 confirm :需要用户点击 "确定" 或 "取消" ,若用户点击 ”确定“ ...
- 初识Python和使用Python爬虫
一.python基础知识了解: 1.特点: Python的语言特性: Python是一门具有强类型(即变量类型是强制要求的).动态性.隐式类型(不需要做变量声明).大小写敏感(var和VAR代表 ...