Hdu 5015 233 Matrix 矩陣快速冪

來源:互聯網
上載者:User

標籤:matrix

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5015

題目大意:給你一個n*m(n<=10,m<=10^9)的矩陣,第一行的元素為233,2333,23333.。。。,第一列的元素為 給定的已知的元素,求a[n][m],其中a[i][j]=a[i-1][j]+a[i][j-1];

解題思路:由於n較小,所以我們可以一列一列的推,即矩陣遞推如下:

/**     本題是一個n * m的矩陣(m比較大)               a[0][1]=233  a[0][2]=2333  ...    a[1][0]    a[1][1]    a[2][0]    a[2][1]    a[3][0]    a[3][1]    a[4][0]    a[4][1]    .    .                                                                               { 10 0 0 0 1 }    .                                                                               { 1  1 0 0 0 }    {2333,a[1][1],a[2][1],a[3][1],.....,3}= {233,a[1][0],a[2][0],a[3][0],.....,3} * { 1  1 1 0 0 } ;                                                                                    { 1  1 1 1 0 }                                                                                    { 0  0 0 0 1 }**/
代碼如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define LL long longint n,m;struct mat{    LL ma[12][12];};mat init(mat x){    memset(x.ma,0,sizeof(x.ma));    x.ma[0][0]=10;    x.ma[0][n+1]=1;    for(int i=1;i<=n;i++)        for(int j=0;j<=i;j++)        x.ma[i][j]=1;    x.ma[n+1][n+1]=1;    return x;}mat cal(mat a,mat b){    mat c;    memset(c.ma,0,sizeof(c.ma));    for(int i=0;i<=(n+1);i++)    for(int k=0;k<=(n+1);k++)    for(int j=0;j<=(n+1);j++)    c.ma[i][j]=(c.ma[i][j]+a.ma[i][k]*b.ma[k][j])%10000007;    return c;}mat pow(mat a,int k){    mat e;    memset(e.ma,0,sizeof(e.ma));    for(int i=0;i<=n+1;i++)        e.ma[i][i]=1;    while(k>0)    {        if(k&1) e=cal(e,a);        a=cal(a,a);        k>>=1;    }    return e;}int main(){    int a[13];    while(~scanf("%d%d",&n,&m))    {        a[0]=233;        for(int i=1;i<=n;i++)            scanf("%I64d",&a[i]);        a[n+1]=3;        mat x;        x=init(x);        x=pow(x,m);        LL ans=0;        for(int i=0;i<=n+1;i++)            ans=(ans+x.ma[n][i]*a[i])%10000007;        printf("%I64d\n",ans);    }    return 0;}


Hdu 5015 233 Matrix 矩陣快速冪

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.