码迷,mamicode.com
首页 > 其他好文 > 详细

P1306 斐波那契公约数

时间:2018-10-17 20:44:00      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:lld   clu   最大   reg   opera   ret   \n   def   scan   

题意

求斐波那契数列第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\n", x0); return 0; }
    b = Mul(c, _pow(a, n));
    printf("%lld\n", (b.a[1][1] + mod) % mod % g);
}

P1306 斐波那契公约数

标签:lld   clu   最大   reg   opera   ret   \n   def   scan   

原文地址:https://www.cnblogs.com/xuyixuan/p/9806758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!