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

你可能感兴趣的文章
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>