【矩陣快速冪】hdu 1757

來源:互聯網
上載者:User

不解釋

#define N 10int MOD ;struct Mat{    int mat[10][10];};//初始化單位矩陣Mat init(){    Mat E;    for(int i = 0; i < N; i++){        for(int j = 0; j < N; j++){            if(i == j)            E.mat[i][i] = 1;            else            E.mat[i][j] = 0;        }    }    return E;}//重載乘法Mat operator *(Mat a,Mat b){    Mat c;    memset(c.mat,0,sizeof(Mat));    for(int i = 0; i < N; i++){        for(int j = 0; j < N; j++){            for(int k = 0; k < N; k++){                if(a.mat[i][k] && b.mat[k][j]){                    c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;                }            }        }    }    return c;}//重載加法Mat operator +(Mat a,Mat b){    Mat c;    memset(c.mat,0,sizeof(Mat));    for(int i = 0; i < N; i++){        for(int j = 0; j < N; j++){            c.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % MOD;        }    }    return c;}//重載冪次方Mat operator ^(Mat A,LL x){    if(x == 1)return A;    Mat c;    c = init();    for(; x ; x >>= 1){        if(x&1){            c = c*A;        }        A = A*A;    }    return c;}int main(){    int k,m;    while(scanf("%d%d",&k,&m)!=EOF){        if(k<10){            printf("%d\n",k % m);        }else{            int a[10];            int i;            for(i=0;i<10;i++)scanf("%d",&a[i]);            Mat gao = { a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],                        1,0,0,0,0,0,0,0,0,0,                        0,1,0,0,0,0,0,0,0,0,                        0,0,1,0,0,0,0,0,0,0,                        0,0,0,1,0,0,0,0,0,0,                        0,0,0,0,1,0,0,0,0,0,                        0,0,0,0,0,1,0,0,0,0,                        0,0,0,0,0,0,1,0,0,0,                        0,0,0,0,0,0,0,1,0,0,                        0,0,0,0,0,0,0,0,1,0 };            MOD = m;            Mat ans = gao^(k-9);            int sum = 0;            for(i = 0;i < 10;i++){                sum += ans.mat[0][i]*(9-i);            }            printf("%d\n",sum%MOD);        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.