poj 3233(矩阵高速幂)
题目链接: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(矩阵高速幂)的更多相关文章
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- POJ 3233 矩阵快速幂&二分
题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...
- Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板
题目链接 请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...
- poj 3233 矩阵快速幂+YY
题意:给你矩阵A,求S=A+A^1+A^2+...+A^n sol:直接把每一项解出来显然是不行的,也没必要. 我们可以YY一个矩阵: 其中1表示单位矩阵 然后容易得到: 可以看出这个分块矩阵的左下角 ...
- poj 2778 AC自己主动机 + 矩阵高速幂
// poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- POJ 3613 Cow Relays (floyd + 矩阵高速幂)
题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B B[i][j] 就表示 i-j 刚好走过两条路的方法数 那么同理 我们把 ...
- UVA 11551 - Experienced Endeavour(矩阵高速幂)
UVA 11551 - Experienced Endeavour 题目链接 题意:给定一列数,每一个数相应一个变换.变换为原先数列一些位置相加起来的和,问r次变换后的序列是多少 思路:矩阵高速幂,要 ...
- UVA10518 - How Many Calls?(矩阵高速幂)
UVA10518 - How Many Calls?(矩阵高速幂) 题目链接 题目大意:给你fibonacci数列怎么求的.然后问你求f(n) = f(n - 1) + f(n - 2)须要多少次调用 ...
随机推荐
- ubuntu12.04 配置apache+modwsgi+django1.5
1.首先下载modwsgi 链接如下: http://files.cnblogs.com/baoyiluo/mod_wsgi-3.4.zip 2.解压并安装mod_wsgi: ./configure ...
- mybatis generator 覆盖xml文件
mybatis generator默认采用追加方式生成,所以我们如果要重新生成代码的时候那么要先删除原来的文件. 解决办法: 1:创建一个自定义补丁类. OverwriteXmlPlugin.java ...
- egg.js上传文件到本地
'use strict'; const Service = require('egg').Service; const fs = require('fs'); const path = require ...
- 「 Luogu P1379 」 八数码难题
# 解题思路 这题不难,主要就是考虑如何判重,如果直接在 $9$ 个位置上都比较一遍的话.你会得到下面的好成绩 所以考虑另一种方法: 将九个位置压成一个整数,并且因为只有九个数,所以不会超出 $int ...
- http请求体笔记
一.请求体数据格式 1.application/json:json格式 2.text/plain:纯文本格式 3.application/x-www-form-urlencoded:url编码后产生的 ...
- Centos6.5下 执行“ll”提示“-bash: ll: command not found”
ll 是 ls -l的别名,之所所以 ll出现错误是因为没有定义别名. 如果要实现ll 命令,可以做如下操作: 编辑 ~./bashrc 添加 ls -l 的别名为 ll即可 [root@Centos ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- 全文搜索(AB-2)-权重
概念 权重是一个相对的概念,针对某一指标而言.某一指标的权重是指该指标在整体评价中的相对重要程度.权重是要从若干评价指标中分出轻重来,一组评价指标体系相对应的权重组成了权重体系. 释义 等同于比重 ...
- hihoCoder#1119 小Hi小Ho的惊天大作战:扫雷·二
原题地址 没有复杂算法,就是麻烦,写起来细节比较多,比较考验细心,一次AC好开心. 代码: #include <iostream> #include <vector> #inc ...
- 【ZJOI2017 Round1练习】D8T1 mushroom(点分治)
题意: 思路: num[a[u]]表示存在a[u]这个颜色且终点在u子树中的链长总和 ans[i]表示以当前的u为根,前面的子树对i的贡献之和 ..]of longint; size,f,ans,su ...