【专题】斐波那契

· · 个人记录

斐波那契定义

F_1=1,F_2=1 F_i=F_{i-1}+F_{i-2}(i≥3)

斐波那契数列常见的可以通过矩阵快速幂求解。

例题1:P4910 帕秋莉的手环

例题2:P3986 斐波那契数列

例题3:P2544 [AHOI2004]数字迷阵

斐波那契性质

1、 \gcd( F [ n + 1 ] , F [ n ] ) = 1

2、 F [ n + m ] = F [ n ] * F [ m + 1 ] + F [ n - 1 ] * F [ m ]

该性质证明比较无脑。

3 . \gcd ( F [ n + m ] , F [ n ] ) = \gcd( F [ n ] , F [ m ] )

证明:

\gcd ( F [ n + m ] , F [ n ]) = \gcd ( F [n+1]F[ m ]+F [ n ] F [ m - 1 ],F [ n ] ) = \gcd(F[n+1]F[m],F[n]) =\gcd(F[n+1],F[n])\times \gcd ( F [ m ], F[ n ]) = \gcd ( F [m] , F [ n ] )

4、(较常用)\gcd ( F[ n ] , F[ m ] ) = F[ \gcd ( n , m ) ]

例题:P1306 斐波那契公约数

5 . 若k是n的倍数。

F_{2n}=F_n\times F_{n+1}+F_{n-1}\times F_n F_{3n}=F_{2n}\times F_{n+1}+F_{2n-1}\times F_n

也就是说 , F_{kn} 的每一个项一定会包含 F_{n} , F_{kn} 是 F_n 的倍数。

6 . 上述的性质是可逆的,若F_{a}|F_{b},则a|b。

7. 卡西尼恒等式

上图可以反映的一个等式是:

F_{n+1}\times F_{n-1}-F_{n}^{2}=(-1)^{n}

证明暂时略去。

8 .F_1+F_2+...+F_n=F_{n+2}-1

证明:

使用数学归纳法,当i=1时,F_1=F_3-1成立,假设i=k的时候成立,则有:F_1+F_2+...+F_k=F_{k+2}-1。

i=k+1$时有:$F_1+F_2+...+F_k+F_{k+1}=F_{k+2}-1+F_{k+1}=F_{k+3}-1

得证。

例题:P4834 萨塔尼亚的期末考试

9 . 以斐波那契数列相邻两项作为输入会使欧几里德算法达到最坏复杂度。

斐波那契编码

我们可以利用斐波那契数列为正整数编码,性质:任何自然数n可以被唯一地表示成一些斐波那契数的和:

N=F_{k1}+F_{k2}+...+F_{ks}

并且不能使用两个相邻的斐波那契数。

例题1:P3539 [POI2012]ROZ-Fibonacci Representatio

例题2:P4133 [BJOI2012]最多的方案

模意义下周期性(较重要)

考虑模 p 意义下的斐波那契数列,可以使用抽屉原理证明,该数列是有周期性的。模意义下前(p^2+1)个斐波那契数对(两个相邻数配对):

(F_1,F_2),(F_2,F_3)....(F_{p^2+1},F_{p^2+2})

在前(p^2+1)个数对中必有两个相同的数对,于是这两个数对可以往后生成相同的斐波那契数列,于是有周期性。

例题1:UVA11582 巨大的斐波那契数

例题2:P7325 [WC2021] 斐波那契

皮萨诺周期(有用)

模n意义下斐波那契数列的周期被称为皮萨诺周期,该数可以证明总是不超过6n,且只有在满足 n=2\times 5^k 的形式时才取到等号。

例题:P4994 终于结束的起点

斐波那契循环节

这里主要是求F_n \pmod{P}的一种更快捷的方法,且比较实用。根据斐波那契有循环节可以将所求的{F_n}限制在 模数的值域内,从而能够更加方便地求解(利用矩阵乘法即可)。

问题在于:如何快速得到在模P意义下的循环节。

几个步骤:

step1:

step2

直接取这个$m_{i} \times p_i^{k_i-1}$。因为我们是为了快速计算斐波那契数列的某个值,而$m_{i} \times p_i^{k_i-1}$是$M_i$一个倍数,最后得到的"循环节"$M^{'}=lcm(m_{1} \times p_1^{k_1-1},⋯,m_{s} \times p_s^{k_s-1})$也就会相应地变成正确的循环节$M$的倍数。但是$M^{'}$一定是斐波那契数列循环的周期之一。因此用它来进行矩阵快速幂是正确的。 **step3:** - 对于每个$p_i$,如果$p_i≤5$,直接打表:$2$的最小循环节是$3$,$3$的最小循环节是$8$,$5$的最小循环节是$20$。 若$p_i≡±1 \pmod{5} $,则$m_i$是 $p_i-1$ 的某一因数,直接取本身即可。 若$p_i≡±2 \pmod 5$,则$m_i$是 $2p_i+2$ 的某一因数且$2∤\frac{2p_i+2}{m_i}$,直接取本身亦可。 因此操作步骤本身是不难的。 例题:[P4000 斐波那契数列](https://www.luogu.com.cn/problem/P4000) $code$:$(P4000)


/*
  __ __ _  __ __ _   __ __ _ 
 /       //       / |   _   |     
 `---   / `---   /  |  |_\  |
    /  /     /  /   |   __  |
   /  `---. /  `---.|  |  | \_
  /_ _ _ _|/_ _ _ _||__|  |_ _|

*/

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define x1 x11
#define y1 y11
#define x2 x22
#define y2 y22
#define z1 z11
#define z2 z22
const int maxn=1e6+5;
const int INF=2e16;
//const int mod=1e9+7;
const double Exp=1e-6;
const double Pi=acos(-1.0);
inline int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
int mod;
int Gcd(int x,int y)
{
    if(y==0)
    return x;
    return Gcd(y,x%y);
}
int Lcm(int x,int y)
{
    return x/Gcd(x,y)*y;
}
char c[30000005];
int s[maxn];
int p;
int tot=0;
int prime[maxn];
struct Matrix
{
    int v[3][3];
};
Matrix mul(Matrix a,Matrix b)
{
    Matrix c;
    memset(c.v,0,sizeof(c.v));
    for(reg int i=1;i<=2;i++)
    {
        for(reg int j=1;j<=2;j++)
        {
            for(reg int k=1;k<=2;k++)
                c.v[i][j]=(c.v[i][j]+a.v[i][k]*b.v[k][j]%mod)%mod;
        }
    }
    return c;
}
Matrix ksm(Matrix x,int y)
{
    Matrix res;
    memset(res.v,0,sizeof(res.v));
    for(reg int i=1;i<=2;i++)
    res.v[i][i]=1;
    while(y)
    {
        if(y & 1)
            res=mul(res,x);
        x=mul(x,x);
        y>>=1;
    }
    return res;
}
int Work(int p1)
{
    tot=0;
    for(reg int i=2;i*i<=p1;i++)
    {
        if(p1%i==0)
        {
            int now=1;
            while(p1%i==0)
            {
                now=now*i;
                p1/=i;
            }
            s[++tot]=now;
            prime[tot]=i;
        }
    }
    if(p1>1)
    {
        tot++;
        s[tot]=p1;
        prime[tot]=p1;
    }
    for(reg int i=1;i<=tot;i++)
        s[i]=s[i]/prime[i];
    for(reg int i=1;i<=tot;i++)
    {
        int v=prime[i];
        if(v==2)
            s[i]=s[i]*3;
        else
        if(v==3)
            s[i]=s[i]*8;
        else
        if(v==5)
            s[i]=s[i]*20;
        else
        if(v%5==1 || v%5==4)
            s[i]=s[i]*(v-1);
        else
            s[i]=s[i]*(2*(v+1));
    }
    int res=s[1];
    for(reg int i=2;i<=tot;i++)
        res=Lcm(res,s[i]);
    return res;
}
int F(int n)
{
    Matrix ans;
    memset(ans.v,0,sizeof(ans.v));
    Matrix t;
    memset(t.v,0,sizeof(t.v));
    t.v[1][1]=t.v[1][2]=t.v[2][1]=1;
    ans.v[1][1]=1,ans.v[1][2]=0;
    t=ksm(t,n-1);
    ans=mul(ans,t);
    return ans.v[1][1];
}
signed main()
{
    scanf("%s",c+1);
    int len=strlen(c+1);
    p=read();
    mod=p;
    if(p==1)
    {
        printf("0\n");
        return 0;
    }
    int m=Work(p);
    int wns=0;
    for(reg int i=1;i<=len;i++)
        wns=(wns*10+(int)(c[i]-'0'))%m;
    if(wns==0)
    {
        printf("0\n");
        return 0;
    }
    else
    if(wns==1)
    {
        printf("1\n");
        return 0;
    }
    printf("%lld",F(wns));
    return 0;
}

参考blog:

1.https://blog.csdn.net/ltrbless/article/

2.weixin_30872157

3.https://oi-wiki.org/math/fibonacci/

4.https://blog.csdn.net/ding4440/article/details/