
2020牛客寒假算法基础集训营1 J u's的影响力(矩阵快速幂+费小马降幂)
发布日期:2021-05-08 15:15:01
浏览次数:26
分类:精选文章
本文共 1492 字,大约阅读时间需要 4 分钟。
用矩阵快速幂计算x,y,a的幂次。#includeusing 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<
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月12日 18时21分31秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
vue实现简单的点击切换颜色
2021-05-09
vue3 template refs dom的引用、组件的引用、获取子组件的值
2021-05-09
深入浅出mybatis
2021-05-09
Zookeeper快速开始
2021-05-09
882. Reachable Nodes In Subdivided Graph
2021-05-09
402. Remove K Digits
2021-05-09
375. Guess Number Higher or Lower II
2021-05-09
650. 2 Keys Keyboard
2021-05-09
764. Largest Plus Sign
2021-05-09
214. Shortest Palindrome
2021-05-09
916. Word Subsets
2021-05-09
869. Reordered Power of 2
2021-05-09
1086 Tree Traversals Again
2021-05-09
1127 ZigZagging on a Tree
2021-05-09
1062 Talent and Virtue
2021-05-09
1045 Favorite Color Stripe
2021-05-09
B. Spreadsheets(进制转换,数学)
2021-05-09
等和的分隔子集(DP)
2021-05-09
基础练习 十六进制转八进制(模拟)
2021-05-09
L - Large Division (大数, 同余)
2021-05-09