题目链接: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. createdb - 创建一个新的 PostgreSQL 数据库

    SYNOPSIS createdb [ option...] [ dbname] [ description] DESCRIPTION 描述 createdb 创建一个新的 PostgreSQL 数据 ...

  2. viewport 640宽的做法 针对iphone和安卓单独设置

    <!DOCTYPE html> <html lang="ch"> <head> <meta charset="utf-8&quo ...

  3. ansible中yaml语法应用

    4.yaml语法应用 ansible的playbook编写是yaml语言编写,掌握yaml语法是编写playbook的必要条件,格式要求和Python相似,具体教程参考如下 yaml语言教程 附上一个 ...

  4. mysql安装及navicat连接

    1.下载mysql官方连接:https://dev.mysql.com/downloads/mysql/ 下载成功后,解压到自己想要的路径下并创建my.ini文件和配置环境变量 然后我们在根目录下创建 ...

  5. Swing实现个人简历

    源码: import java.awt.Container;import java.awt.FlowLayout;import java.awt.Font; import javax.swing.*; ...

  6. 解决 python No migrations to apply 无法生成表

    第一步: 删除该app名字下的migrations文件. 第二步: 进入数据库,找到django_migrations的表,删除该app名字的所有记录. delete from django_migr ...

  7. 笔试算法题(16):二叉树深度计算 & 字符串全排列

    出题:要求判断二元树的深度(最长根节点到叶节点的路径): 分析:二元递归不容易使用循环实现 解题: struct Node { int value; Node *left; Node *right; ...

  8. 合并多个MP4文件

    把多个MP4文件连接起来的方法与音频文件不太一样,比较有效的方法是: $ cat mylist.txt file '/path/to/file1' file '/path/to/file2' file ...

  9. 微信sdk 签名

    <?php namespace app\wechat\service; use think\Config; class Signature { protected $appId ; protec ...

  10. Quartz --quartz.properties

    quartz.properties 如果项目中没有该配置文件,则会去jar包中读取自带配置文件 默认的配置如下 # Default Properties file for use by StdSche ...