bzoj3231[Sdoi2008]遞迴數列

來源:互聯網
上載者:User
Description

一個由自然數組成的數列按下式定義:
對於i <= k:ai = bi
對於i > k: ai = c1ai-1 + c2ai-2 + … + ckai-k
其中bj和 cj (1<=j<=k)是給定的自然數。寫一個程式,給定自然數m <= n, 計算am + am+1 + am+2 + … + an, 並輸出它除以給定自然數p的餘數的值。 Input

由四行組成。
第一行是一個自然數k。
第二行包含k個自然數b1, b2,…,bk。
第三行包含k個自然數c1, c2,…,ck。
第四行包含三個自然數m, n, p。 Output

僅包含一行:一個正整數,表示(am + am+1 + am+2 + … + an) mod p的值。 HINT

對於100%的測試資料:
1<= k<=15
1 <= m <= n <= 10^18
對於所有測試資料:
0<= b1, b2,… bk, c1, c2,…, ck<=10^9
1 <= p <= 10^8

矩陣快速冪裸題.
我們只需要在構造矩陣時再維護一個首碼和就好,這樣兩遍快速冪,再用結果做個差即可
構造矩陣為
⎡⎣⎢⎢⎢⎢⎢⎢C110...1C201...0C300...0...............CK00...0⎤⎦⎥⎥⎥⎥⎥⎥×⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢AkAk−1Ak−2...A1sumk−1⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥ \begin{bmatrix} C1& C2&C3&...&CK \\1 & 0&0&...&0\\0&1 &0&...&0\\...&...&...&...&...\\1&0&0&...&0\end{bmatrix}\quad \times\begin{bmatrix}Ak\\Ak-1\\Ak-2\\...\\A1\\sum_{k-1}\\\end{bmatrix}\quad
注意特判掉 n,m<K n,m的情況.
代碼

    #include <cstdio>#include <iostream>#include <cstring>#define maxn 18#define mem(a,b) memset(a,b,sizeof(a))using namespace std;inline void read(int& x){   char c=getchar();x=0;int y=1;    while(c<'0'||c>'9'){if(c=='-') y=-1;c=getchar();}    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();    x*=y;}typedef long long ll;ll sum[maxn],n,m;int C[maxn],K,mod;struct matrix{   int s[maxn][maxn];    matrix(){mem(s,0);}    inline void init(){mem(s,0);for(int i=0;i<K;++i) this->s[i][i]=1;}    inline int* operator [](int x){return s[x];}    inline friend matrix operator *(matrix x,matrix y)    {   matrix z;        for(int i=0;i<K;++i)            for(int j=0;j<K;++j)                for

聯繫我們

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