博客
关于我
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/

你可能感兴趣的文章
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>
MySQL InnoDB事务隔离级别与锁机制深入解析
查看>>
Mysql InnoDB存储引擎 —— 数据页
查看>>
Mysql InnoDB存储引擎中的checkpoint技术
查看>>
Mysql InnoDB存储引擎中缓冲池Buffer Pool、Redo Log、Bin Log、Undo Log、Channge Buffer
查看>>
MySQL InnoDB引擎的锁机制详解
查看>>
Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock
查看>>
mysql InnoDB数据存储引擎 的B+树索引原理
查看>>
mysql innodb通过使用mvcc来实现可重复读
查看>>
mysql insert update 同时执行_MySQL进阶三板斧(三)看清“触发器 (Trigger)”的真实面目...
查看>>