矩阵快速幂

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
typedef  vector<int> vec;
typedef  vector mat;

mat mul(mat A, mat B) { //两个矩阵的相乘 B*A后生成,A的列为C的列,B的行为C的行

    mat C(A.size(), vec(B[0].size()));
    for (int i=0; ifor (int j=0; jfor (int k=0; k0].size(); k++)
                C[i][k]+=B[i][j]*A[j][k];
        }
    }
    return C;
}

mat pow(mat A, int n) {  //快速幂
    mat B(A.size(), vec(A.size()));
    for (int i=0; i//单位矩阵
        B[i][i] = 1;

    while (n) {
        if (n&1)
            B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

int main() {
    int n;
    cin>>n;
    mat A(2, vec(2));
    A[0][0] = 1, A[0][1] = 1;
    A[1][0] = 1, A[1][1] = 0;
    A = pow(A, n);
    printf("%d\n", A[1][0]);
    return 0;
}

你可能感兴趣的:(数论)