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)须要多少次调用 ...
随机推荐
- vim 将文件所有行合并到一行
vim 将文件所有行合并到一行 在 Normal Mode下执行: ggvGJ gg 用于跳到行首 v 转换成 visual 模式 G 跳到最后一行 J 合并行
- No-2.注释
01. 注释的作用 使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释) 以 # 开头,# 右边的所有东西都被当做说明文字,而不是真正要执行的程序,只起到 ...
- python compare with other language
java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...
- Android自动化测试之Monkey
本来是做Web后端的,来公司实习变成微信小程序前端了,到这周变成Android APP测试人员了,也是微醺啊. 由于对手工测试终究是有些抵触,所有昨天小试了一下不用写代码的自动化压力测试,在此记下我的 ...
- [Python3网络爬虫开发实战] 4.1-使用XPath
XPath,全称XML Path Language,即XML路径语言,它是一门在XML文档中查找信息的语言.它最初是用来搜寻XML文档的,但是它同样适用于HTML文档的搜索. 所以在做爬虫时,我们完全 ...
- HTTP实验:分别使用httpd-2.2和httpd-2.4实现
1. 需求描述 1.建立httpd服务,要求: (1) 提供两个基于名称的虚拟主机: www1.stuX.com,页面文件目录为/web/vhosts/www1:错误日志为/var/log/httpd ...
- Python之字符串计算(计算器)
Python之字符串计算(计算器) import re expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998 ...
- python链家网高并发异步爬虫and异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- Python面试快问快答,理论要的就是速度与精准,Python面试题No2
今天的面试题 第1题:python2和python3的range(100)的区别 range()函数的含义 range函数是一个用来创建算数级数序列的通用函数,返回一个[start, start + ...
- LeetCode(60) Permutation Sequence
题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...