POJ 3233 Matrix Power Series (矩阵分块,递推)
矩阵乘法是可以分块的,而且幂的和也是具有线性的。
不难得到 Si = Si-1+A*Ai-1,Ai = A*Ai-1。然后矩阵快速幂就可以了。
- /*********************************************************
- * ------------------ *
- * author AbyssalFish *
- **********************************************************/
- #include<cstdio>
- #include<iostream>
- #include<string>
- #include<cstring>
- #include<queue>
- #include<vector>
- #include<stack>
- #include<vector>
- #include<map>
- #include<set>
- #include<algorithm>
- #include<cmath>
- #include<ctime>
- using namespace std;
- typedef long long ll;
- typedef vector<int> row;
- typedef vector<row> mat;
- int n, k, M;
- mat Mul;
- mat &operator *(mat &A, mat& B)
- {
- mat &R = Mul;
- R.assign(n,row(n));
- for(int i = ; i < n; i++){
- for(int j = ; j < n; j++){
- for(int k = ; k < n; k++){
- R[i][j] = (R[i][j] +A[i][k]*B[k][j])%M;
- }
- }
- }
- return R;
- }
- //#define LOCAL
- #ifdef LOCAL
- void censor(mat &B)
- {
- for(auto r: B){
- for(int c: r)
- cout<<c<<' ';
- cout<<endl;
- }
- }
- #endif
- mat operator ^(mat A,int q)
- {
- mat Re(n,row(n));
- for(int i = ; i < n; i++) Re[i][i] = ;
- while(q){
- if(q&) Re = Re*A;
- A = A*A;
- q >>= ;
- }
- return Re;
- }
- int main()
- {
- #ifdef LOCAL
- freopen("in.txt","r",stdin);
- #endif
- int nn; scanf("%d%d%d",&nn,&k,&M);
- n = *nn;
- mat A(nn,row(nn));
- for(int i = ; i < nn; i++){
- for(int j = ; j < nn; j++){
- scanf("%d",&A[i][j]);
- }
- }
- mat B(n,row(n));
- for(int i = ; i < nn; i++) {
- B[i][i] = ;
- copy(A[i].begin(),A[i].end(),B[i].begin()+nn);
- copy(A[i].begin(),A[i].end(),B[i+nn].begin()+nn);
- }
- B = B^k;
- for(int i = ; i < nn; i++){
- for(int j = ; j < nn; j++){
- printf("%d%c",B[i][j+nn],j==nn-?'\n':' ');
- }
- }
- #ifdef LOCAL
- cout<<"rum time:"<<clock()<<"ms"<<endl;
- #endif // LOCAL
- return ;
- }
POJ 3233 Matrix Power Series (矩阵分块,递推)的更多相关文章
- 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 Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 15739 Accepted: ...
- 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 矩阵快速幂
设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://poj.org/problem?id=3233 题解 矩阵快速幂+二分等比数列求和 AC代码 #include <stdio.h> #include <math.h&g ...
- 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(矩阵等比求和)
题目链接 模板题. #include <cstdio> #include <cstring> #include <iostream> #include <ma ...
- 矩阵十点【两】 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 (矩阵乘法)
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 11954 Accepted: ...
随机推荐
- Codeforces - 102222A - Maximum Element In A Stack - 模拟
https://codeforc.es/gym/102222/problem/F 注意到其实用unsigned long long不会溢出. #include<bits/stdc++.h> ...
- Ocelot(三)- 服务发现
Ocelot(三)- 服务发现 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10907856.html 源码地址:https ...
- c++第四次实验2
Part 1 车辆基本信息管理 1.代码 #include<iostream> using namespace std; #include"car.h" #includ ...
- 文件上传Django
当Django在处理文件上传的时候,文件数据被保存在request.FILES FILES中的每个键为<input type="file" name="" ...
- Linux学习常用命令大全
Linux知识大全 转载须说明出处,整理不易 一.常用的linux命令 1.2 ls 命令说明 1.3 ls 通配符的使用 2.切换目录cd命令 3.创建和删除文件操作 4.移动和拷贝文件 4.3.m ...
- 使用HTTP协议访问网络(Android)
在做项目的过程中需要连接服务器访问数据,还没有接触过Android网络编程方面,参考了<Android第一行代码>,在做的过程中遇到了很多的问题,这里就此记录一下. 先给出访问网络的代码: ...
- thinkphp5使用querylist采集图片示例
首先composer引入querylist composer require jaeger/querylist 注意需要php7.0以上版本 <?php namespace app\index\ ...
- HTTP/TCP协议基础
HTTP协议 基本概念 HTTP协议(超文本传输协议 HyperText Transfer Protocol):是用于从WWW服务器传输超文本到本地浏览器的传送协议.它不仅保证计算机正确快速地传输超文 ...
- 029 Divide Two Integers 两数相除
不使用乘号,除号和取模符号将两数相除.如果溢出返回 MAX_INT.详见:https://leetcode.com/problems/divide-two-integers/description/ ...
- 记录一个直接操作mediawiki数据库遇到的坑
我的mediawiki使用的是postgresql数据库,当你进入到mediawiki数据库时,运行sql select * from pg_tables; 你会发现mediawiki的数据表的sch ...