标签: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);
}
标签:lld clu 最大 reg opera ret \n def scan
原文地址:https://www.cnblogs.com/xuyixuan/p/9806758.html