矩陣快速冪+倍增法(純模板)

Mat power(Mat M,int P){
    Mat tot(1);
    while (P){
        if (P&1) tot=tot*M;
        P>>=1,M=M*M;
    }
    return tot;
}



Mat count(Mat M,int P){
    Mat M0,E(1),M1=E;
    while (P){
        if (P&1) M0=M0+M1*power(M,P);
        P>>=1;
        M1=M1*(E+power(M,P));
    }
    return M0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章