P1306 斐波那契公约数

Posted xuyixuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P1306 斐波那契公约数相关的知识,希望对你有一定的参考价值。

题意

求斐波那契数列第n项和第m项的最大公约数

题解

设斐波那契数列第x项为F[x]
则有结论(Gcd(F[n], F[m]) = F[Gcd(n, m)])
证明:
不妨设n < m
(F[m] = F[m-1] +F[m-2])
(= 2*F[m-2] + F[m-3])
(= 3*F[m-3] + 2*F[m-4])
(=...)
(= F[x+1]*F[m-x] + F[x] * F[)

代码

#include <cstdio>
typedef long long ll;
int T, N, g;
ll n, m, aa, cc, x0, mod;
struct Matrix
{
    ll a[4][4];
    Matrix& operator =(const Matrix& x)
    {
        for (register int i = 1; i <= N; ++i)
            for (register int j = 1; j <= N; ++j)
                a[i][j] = x.a[i][j];
        return *this;
    }
};
Matrix a, b, c;
ll _mul(ll x, ll y, ll s = 0)
{
    for (; y; y >>= 1, x = (x + x) % mod)
        if (y & 1)
            s = (s + x) % mod;
    return s;
}
Matrix Mul(const Matrix& x, const Matrix& y)
{
    Matrix s;
    for (register int i = 1; i <= N; ++i)
        for (register int j = 1; j <= N; ++j)
            s.a[i][j] = 0;
    for (register int i = 1; i <= N; ++i)
        for (register int j = 1; j <= N; ++j)
            for (register int k = 1; k <= N; ++k)
                s.a[i][j] = (s.a[i][j] + _mul(x.a[i][k], y.a[k][j])) % mod;
    return s;
}
Matrix _pow(Matrix x, ll y)
{
    Matrix s;
    for (register int i = 1; i <= N; ++i)
        for (register int j = 1; j <= N; ++j)
            s.a[i][j] = (i == j);
    for (; y; y >>= 1, x = Mul(x, x)) if (y & 1) s = Mul(s, x);
    return s;
}
int main()
{
    N = 2;
    scanf("%lld%lld%lld%lld%lld%d", &m, &aa, &cc, &x0, &n, &g);
    mod = m;
    c.a[1][1] = x0; c.a[1][2] = cc;
    a.a[1][1] = aa; a.a[2][1] = a.a[2][2] = 1;
    if (n <= 0) {printf("%lld
", x0); return 0; }
    b = Mul(c, _pow(a, n));
    printf("%lld
", (b.a[1][1] + mod) % mod % g);
}

以上是关于P1306 斐波那契公约数的主要内容,如果未能解决你的问题,请参考以下文章

P1306 斐波那契公约数

Luogu P1306 斐波那契公约数

P1306 斐波那契公约数

斐波那契矩阵快速幂模板斐波那契公约数

斐波那契公约数的相关证明

斐波那契公约数(luogu 1306)