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 矩阵快速幂 乘积的更多相关文章

  1. 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] ...

  2. POJ 3233 Matrix Power Series 矩阵快速幂+二分求和

    矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...

  3. poj 3233 Matrix Power Series(矩阵二分,高速幂)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 15739   Accepted:  ...

  4. POJ3233:Matrix Power Series(矩阵快速幂+二分)

    http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k ...

  5. poj 3233 Matrix Power Series 矩阵求和

    http://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...

  6. POJ3233 Matrix Power Series 矩阵快速幂 矩阵中的矩阵

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 27277   Accepted:  ...

  7. Poj 3233 Matrix Power Series(矩阵乘法)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Description Given a n × n matrix A and ...

  8. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

  9. POJ3233:Matrix Power Series(矩阵快速幂+递推式)

    传送门 题意 给出n,m,k,求 \[\sum_{i=1}^kA^i\] A是矩阵 分析 我们首先会想到等比公式,然后得到这样一个式子: \[\frac{A^{k+1}-E}{A-E}\] 发现要用矩 ...

随机推荐

  1. boot集成mybatis分页插件pagehelper

    导入依赖 <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter ...

  2. 奖学金(0)<P2007_1>

    奖学金 (scholar.pas/c/cpp) [问题描述] 某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金.期末,每个学生都有3门课的成绩:语文.数学.英语.先按总分 ...

  3. 电影推荐算法---HHR计划

    1,先看FM部分. 2,看看冷启动. 0,热门召回源. 1,男女召回源,年龄召回源,职业召回源,score最高. 2,男女年龄职业相互组合: 3,存入redis.天级别更新. 3,召回+排序先搞懂. ...

  4. 笔记-python-lib—data types-enum

    笔记-python-lib—data types-enum 1.      enum Source code: Lib/enum.py 文档:https://docs.python.org/3/lib ...

  5. java后端开发echarts

    参考: https://blog.csdn.net/mxdmojingqing/article/details/77340245 https://github.com/abel533/ECharts

  6. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

  7. windows 创建软连接

    mklink /d D:\www\child.ivyoffline\common\components\policy D:\www\child.ivyoffline\appOA\protected\c ...

  8. 【Java】变量命名规范

    Java是一种区分字母的大小写的语言,所以我们在定义变量名的时候应该注意区分大小写的使用和一些规范,接下来我们简单的来讲讲Java语言中包.类.变量等的命名规范. (一)Package(包)的命名 P ...

  9. Mac brew安装的php修改了php.ini之后如何重启php?

    环境:nginx+mysql+php7.2:Mac利用homebrew安装的php7.2 问题:修改了PHP的配置文件,php.ini:服务器是nginx,如何重启PHP? 开启: brew serv ...

  10. 哈希表,Java中的hashCode

    哈希表: 将我们所需的键通过哈希函数转换索引,然后存储在一个数组中. 哈希表是时间和空间之间的平衡,体现空间换时间的算法思想(联想到预加载,缓存等,有时候多存储,预处理缓存一些东西,带来时间复杂度的改 ...