UVALive 6472 Powers of Pascal

來源:互聯網
上載者:User

標籤:style   color   os   io   for   代碼   

題目的意思是:


給了一個無窮大Pascal矩陣,定義了powers,然後詢問power為P的pascal矩陣裡面的第R行C列的元素是多少。

最開始讀錯題意了...然後 就成了一個神得不得了的題了。後來請教的別人。

感覺可以用矩陣快速冪做。

然後,不用快速冪的話,你會驚奇的發現,變成了找規律的題了...

答案成了 comb(i,j) * P^(i-j)

對於comb(i,j),利用組合數性質,可以得到,當j>i-j 的時候 j=i-j;

因為題目說答案不會爆long long  所以可以預先處理,大概56左右,後發現50也行

組合數公式 Comb(i,j)=Comb(i-1,j)+Comb(i-1,j-1);


Ps:寫這個是為了提醒自己....有時候打表找規律也是一種做一些數學有關的題的手段,當時實在不知道怎麼做了時。

代碼如下,再次感謝xxx


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=100000;typedef long long LL;LL C[maxn+2][52];LL kk,k,p,r,c;void init(){    for(int i=0;i<=maxn;i++) C[i][0]=1;    for(int i=1;i<=maxn;i++)        for(int j=1;j<=min(i,50);j++)            C[i][j]=C[i-1][j]+C[i-1][j-1];}int main(){    init();    //cout<<C[3][1]<<endl;    scanf("%lld",&k);    while(k--)    {        scanf("%lld%lld%lld%lld",&kk,&p,&r,&c);        LL ans=1;        for(int i=1;i<=(r-c);i++)            ans*=p;        if(c>r/2) c=r-c;        ans*=C[r][c];        printf("%lld %lld\n",kk,ans);    }    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.