斐波那契数列最大公约数

结论:\((fib_n,fib_m)\)=\(fib_{(n,m)}\)
证明:参考大佬的blog
OrzCTY
(我怎么记得以前板板讲过Orz
然后用矩阵快速幂随便做了。。。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int mod=1e8;
long long a,b;
struct Matrix{
    long long g[3][3];
    Matrix(){memset(g,0,sizeof g);}
    Matrix operator * (const Matrix &rhs) const {
        Matrix ans;
        for(int i=1;i<=2;i++) 
            for(int j=1;j<=2;j++) 
               for(int k=1;k<=2;k++) 
                    (ans.g[i][j]+=(g[i][k]*rhs.g[k][j])%mod)%=mod;
        return ans;
    }
}A,B;
Matrix ksm(Matrix x,long long d) {
    Matrix res=x;
    d--;
    while(d) {
        if(d&1) res=res*x;
        x=x*x;
        d>>=1;
    }
    return res;
}
long long gcd(long long a,long long b) {
    return b?gcd(b,a%b):a;
}
int main() {
    cin>>a>>b;
    long long p=gcd(a,b);
    A.g[1][1]=A.g[1][2]=A.g[2][1]=1;
    B=ksm(A,p);
    cout<<B.g[2][1];
    return 0;
}
posted @ 2018-10-18 11:53  SWHsz  阅读(1156)  评论(3编辑  收藏  举报