题目链接:http://poj.org/problem?id=3233

题意:给出一个公式求这个式子模m的解;

分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点。本题k的值非常大。所以要用二分求和来降低执行时间。

代码:

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <functional> using namespace std;
struct Matrax{
long long m[50][50];
}ter;
int n,m;
Matrax add(Matrax a,Matrax b){
Matrax p;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
p.m[i][j]=a.m[i][j]+b.m[i][j];
p.m[i][j]%=m;
// cout<<p.m[i][j]<<" ";
}
// cout<<endl;
}
return p;
}//矩阵加法
Matrax muli(Matrax a,Matrax b){
Matrax p;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
p.m[i][j]=0;
for(int k=0;k<n;k++){
p.m[i][j]+=a.m[i][k]*b.m[k][j];
p.m[i][j]%=m;
}
}
return p;
}//矩阵乘法
Matrax quick_mod(Matrax a,int b){
Matrax ans=ter;
while(b){
if(b&1){
ans=muli(ans,a);
b--;
}
else {
b>>=1;
a=muli(a,a);
}
}
return ans;
}//高速幂
Matrax sum(Matrax a,int k){
if(k==1)return a;
Matrax ans,b;
ans=sum(a,k/2);
if(k&1){
b=quick_mod(a,k/2+1);
ans=add(ans,muli(ans,b));
ans=add(ans,b);
}
else {
b=quick_mod(a,k/2);
ans=add(ans,muli(ans,b));
}
return ans;
}//二分求和
int main(){
int k;
while(scanf("%d%d%d",&n,&k,&m)!=EOF){
Matrax A,tmp;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%I64d",&A.m[i][j]);
ter.m[i][j]=(i==j);
tmp.m[i][j]=0;
}
tmp=sum(A,k);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
cout<<tmp.m[i][j]<<" ";
cout<<endl;
} }
return 0;
}

poj 3233(矩阵高速幂)的更多相关文章

  1. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

  2. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

  3. Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

    题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...

  4. poj 3233 矩阵快速幂+YY

    题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...

  5. poj 2778 AC自己主动机 + 矩阵高速幂

    // poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...

  6. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  7. POJ 3613 Cow Relays (floyd + 矩阵高速幂)

    题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B  B[i][j]  就表示   i-j 刚好走过两条路的方法数 那么同理 我们把 ...

  8. UVA 11551 - Experienced Endeavour(矩阵高速幂)

    UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...

  9. UVA10518 - How Many Calls?(矩阵高速幂)

    UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...

随机推荐

  1. linux 后台进程

    1.进程放入后台 ctrl+z 进程放入后台 暂停执行 2.进程放入后台执行 bg % n 或者 bg n 进程放入后台执行 3.进程取出前台执行 fg % n 或者 fg n 进程取出前台执行 4. ...

  2. 工具:docs网页笔记

    用到工具python 网页笔记工具:mkdocs https://www.cnblogs.com/bigmagic/p/10309260.html 案例: https://github.com/zha ...

  3. vue-router scrollBehavior的用法

    问题: 使用keep-alive标签后部分安卓机返回缓存页位置不精确问题 解决方案: <div id="app"> <keep-alive> <rou ...

  4. http主要请求头

    一.内容协商 1.Accept:希望服务器返回的数据格式,如下面的:text/javascript, application/javascript, application/ecmascript, a ...

  5. c++基础_字母图形

    #include <iostream> #include <algorithm> using namespace std; int main(){ ,m=,c; cin> ...

  6. 自媒体人Chrome浏览器必备插件精选神器!

    自从互联网时代起,浏览器使用从最早的IE,到opera,到猎豹浏览器,到360双核浏览器,到火狐,到safari,到目前最喜欢用的chrome.一路下来,chrome的稳定性与扩展性征服了我,成了我必 ...

  7. FFmpeg加水印

    ffmpeg中文水印乱码两种原因 1.字符编码格式原因,中文必须是utf8编码格式的(我遇到的问题,在vs2013上写的中文,已做编码格式转码,放到centos7.2上编译运行也会出现中文乱码的问题, ...

  8. Linux 下 Jenkins安装

    Jenkins介绍 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括: 1.持续的软件版本发布/测试项目. 2.监控外部调用执行的工作. 安装环境 操作系统:lin ...

  9. LeetCode(46)Permutations

    题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...

  10. Linux下安装SVN,仓库创建,用户权限管理

    Exported from Notepad++           Linux下安装SVN,仓库创建,用户权限管理 1.SVN安装 Ubuntu系统下安装:sudoapt-getinstallsubv ...