P1306 斐波那契公约数 题解
请求出
结论:
证明如下:
首先引理 1:
运用归纳法,可以简单证明,此处略去。
引理 2:
运用归纳法:
设现已有
证毕。
最后用矩阵算
#include <algorithm>
#include <cstring>
#include <iostream>
#define speedup (ios::sync_with_stdio(0), cin.tie(0), cout.tie(0))
#define int long long
using namespace std;
const int mod = 1e8;
struct Mat
{
int m[5][5];
int r, c;
void clear(int R, int C)
{
memset(m, 0, sizeof m);
r = R, c = C;
}
void init()
{
for (int i = 0; i <= min(r, c); i++)
m[i][i] = 1;
}
friend Mat operator*(const Mat a, const Mat b)
{
Mat res;
res.clear(a.r, b.c);
for (int i = 1; i <= a.r; i++)
for (int j = 1; j <= b.c; j++)
for (int k = 1; k <= a.c; k++)
res.m[i][j] = (res.m[i][j] + a.m[i][k] * b.m[k][j] % mod) % mod;
return res;
}
} M, A;
Mat qmi(Mat a, int b)
{
Mat res;
res.clear(3, 3), res.init();
while (b)
{
if (b & 1)
res = res * a;
b >>= 1;
a = a * a;
}
return res;
}
signed main()
{
speedup;
int n, m;
cin >> n >> m;
M.clear(2, 2);
M.m[1][2] = M.m[2][1] = M.m[2][2] = 1;
A.clear(2, 1);
A.m[2][1] = 1;
A = qmi(M, __gcd(n, m) - 1) * A;
cout << A.m[2][1] << '\n';
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!