博客
关于我
2020牛客寒假算法基础集训营1 J u's的影响力(矩阵快速幂+费小马降幂)
阅读量:400 次
发布时间:2019-03-05

本文共 1492 字,大约阅读时间需要 4 分钟。

用矩阵快速幂计算x,y,a的幂次。

#include
using namespace std;#define ll long longstruct mt{ ll a[3][3];};mt t(mt a,mt b,ll mod){ mt res; int i,j,k; for(i=0;i<3;i++){ for(j=0;j<3;j++){ res.a[i][j]=0; for(k=0;k<3;k++){ res.a[i][j]+=a.a[i][k]*b.a[k][j]%mod; res.a[i][j]%=mod; } } } return res;}mt power(mt a,ll b,ll mod){ mt res; int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ res.a[i][j]=0; } } res.a[0][0]=res.a[1][1]=res.a[2][2]=1; while(b){ if(b&1)res=t(res,a,mod); b>>=1; a=t(a,a,mod); } return res;}ll feb(ll n,ll mod){ mt temp; int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ temp.a[i][j]=0; } } temp.a[0][1]=temp.a[1][1]=temp.a[1][0]=1; mt res=power(temp,n-1,mod); return (res.a[0][0]+res.a[0][1])%mod;}ll feb2(ll n,ll mod){ mt temp; int i,j; for(i=0;i<3;i++){ for(j=0;j<3;j++){ temp.a[i][j]=0; } } temp.a[0][1]=temp.a[1][1]=temp.a[1][0]=temp.a[1][2]=temp.a[2][2]=1; mt res=power(temp,n-1,mod); return (res.a[0][0]+2*res.a[0][1]+res.a[0][2])%mod;}ll power(ll a,ll b,ll mod){ ll res=1; while(b){ if(b&1)res=res*a%mod; b>>=1; a=a*a%mod; } return res;}int main(){ int m=1e9+7; int i,j; ll n,x,y,a,b; cin>>n>>x>>y>>a>>b; if(n==1){ cout<

转载地址:http://uoewz.baihongyu.com/

你可能感兴趣的文章
MySQL常见错误分析与解决方法总结
查看>>
mysql并发死锁案例
查看>>
MySQL幻读:大家好,我是幻读,我今天又被解决了
查看>>
MySQL底层概述—1.InnoDB内存结构
查看>>
MySQL底层概述—2.InnoDB磁盘结构
查看>>
MySQL底层概述—3.InnoDB线程模型
查看>>
MySQL底层概述—4.InnoDB数据文件
查看>>
MySQL底层概述—5.InnoDB参数优化
查看>>
MySQL底层概述—6.索引原理
查看>>
MySQL底层概述—7.优化原则及慢查询
查看>>
MySQL底层概述—8.JOIN排序索引优化
查看>>
MySQL底层概述—9.ACID与事务
查看>>
Mysql建立中英文全文索引(mysql5.7以上)
查看>>
mysql建立索引的几大原则
查看>>
Mysql建表中的 “FEDERATED 引擎连接失败 - Server Name Doesn‘t Exist“ 解决方法
查看>>
mysql开启bin-log日志,用于canal同步
查看>>
MySQL开源工具推荐,有了它我卸了珍藏多年Nactive!
查看>>
MySQL异步操作在C++中的应用
查看>>
MySQL引擎讲解
查看>>
Mysql当前列的值等于上一行的值累加前一列的值
查看>>