博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P1306 斐波那契公约数
阅读量:7052 次
发布时间:2019-06-28

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

矩阵乘法都忘了啊...

先说题,有结论:gcd(f[n],f[m]) = f[gcd(n,m)] 

证明不知道,背着就行了反正就算记住到考场也没法重证一遍

知道结论用矩阵快速幂就ok了

然而递推式我会,怎么实现我忘了...

众所周知,一个n * m的矩阵和m * p的矩阵相乘,生成一个n * p的矩阵

新矩阵的a[i][j] = a[i][k]  +a[k][j],其中1 <= k <= m

然后这两个记住就好写了,其他的和普通快速幂一样了

#include
#include
#include
#include
#include
#include
using namespace std;inline int read(){ int ans = 0,op = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') op = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { (ans *= 10) += ch - '0'; ch = getchar(); } return ans * op;}typedef int mainint;#define int long longconst int mod = 19990208;struct mat{ int a[3][3]; int r,c; inline void mem() { memset(a,0,sizeof(a)); r = 0,c = 0; }};int n,m;mat mul(mat x,mat y){ mat p; p.mem(); for(int i = 0;i < x.r;i++) for(int j = 0;j < y.c;j++) for(int k = 0;k < x.c;k++) (p.a[i][j] += x.a[i][k] * y.a[k][j]) %= mod; p.r = x.r,p.c = y.c; return p;}void power(int b){ mat ans,res; ans.mem(),res.mem(); res.r = res.c = 2; res.a[0][0] = res.a[0][1] = res.a[1][0] = 1; ans.r = 1,ans.c = 2; ans.a[0][0] = ans.a[0][1] = 1; while(b) { if(b & 1) ans = mul(ans,res); res = mul(res,res); b >>= 1; } printf("%lld\n",ans.a[0][0]);}int gcd(int x,int y) { return !y ? x : gcd(y,x % y); }mainint main(){ int t = read(); while(t--) { int n = read(),m = read(); n = gcd(n,m); if(n <= 2) printf("1\n"); else power(n - 2); }}

 

转载于:https://www.cnblogs.com/LM-LBG/p/10644984.html

你可能感兴趣的文章
OBJ文件格式简介
查看>>
实验三 有限自动机的构造与识别
查看>>
python的学习笔记之——time模块常用内置函数
查看>>
计算机是如何工作的
查看>>
【c++】必须在类初始化列表中初始化的几种情况
查看>>
阿拉伯数字1与英语字母l造成的代码bug
查看>>
深度学习常见的专业术语
查看>>
2018-2019-2 20165334《网络对抗技术》Exp2 后门原理与实践
查看>>
HTML提交方式post和get区别(实验)
查看>>
Java 11.do语句
查看>>
学习理论之感知器与最大间隔分类器
查看>>
Be Nice!要善良
查看>>
二、ansible配置简要介绍
查看>>
解决docker容器中无ifconfig命令和ping命令问题
查看>>
CHAR、TCHAR、WCHAR_T之间的区别与问题
查看>>
sql小计合计
查看>>
安装Java
查看>>
Ubuntu Linux输入法fcitx方块乱码解决设置
查看>>
node递归批量重命名指定文件夹下的文件
查看>>
python if not用法
查看>>