hdu 1588 Gauss Fibonacci(矩陣快速冪)

來源:互聯網
上載者:User

標籤:des   style   http   java   color   os   strong   io   

Gauss FibonacciTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2090    Accepted Submission(s): 903


Problem DescriptionWithout expecting, Angel replied quickly.She says: "I‘v heard that you‘r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. "
How good an opportunity that Gardon can not give up! The "Problem GF" told by Angel is actually "Gauss Fibonacci".
As we know ,Gauss is the famous mathematician who worked out the sum from 1 to 100 very quickly, and Fibonacci is the crazy man who invented some numbers.

Arithmetic progression:
g(i)=k*i+b;
We assume k and b are both non-nagetive integers.

Fibonacci Numbers:
f(0)=0
f(1)=1
f(n)=f(n-1)+f(n-2) (n>=2)

The Gauss Fibonacci problem is described as follows:
Given k,b,n ,calculate the sum of every f(g(i)) for 0<=i<n
The answer may be very large, so you should divide this answer by M and just output the remainder instead. 
InputThe input contains serveral lines. For each line there are four non-nagetive integers: k,b,n,M
Each of them will not exceed 1,000,000,000.
 
OutputFor each line input, out the value described above. 
Sample Input
2 1 4 1002 0 4 100
 
Sample Output
2112
 
AuthorDYGG 
SourceHDU “Valentines Day” Open Programming Contest 2007-02-14 

題解及代碼:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int mod=1e9;struct mat{    __int64 t[4][4];    void set()    {        memset(t,0,sizeof(t));    }} a,b,c;mat multiple(mat a,mat b,int n,int p){    int i,j,k;    mat temp;    temp.set();    for(i=0; i<n; i++)        for(j=0; j<n; j++)        {            if(a.t[i][j]!=0)                for(k=0; k<n; k++)                    temp.t[i][k]=(temp.t[i][k]+a.t[i][j]*b.t[j][k]+p)%p;        }    return temp;}mat quick_mod(mat b,int n,int m,int p){    mat t;    t.set();    for(int i=0;i<n;i++) t.t[i][i]=1;    while(m)    {        if(m&1)        {            t=multiple(t,b,n,p);        }        m>>=1;        b=multiple(b,b,n,p);    }    return t;}void init1(){   b.set();   b.t[0][1]=1;   b.t[1][0]=1;   b.t[1][1]=1;}void init2(){    b.t[0][2]=1;    b.t[1][3]=1;    b.t[2][2]=1;    b.t[3][3]=1;}int main(){    int  _k,_b,_n,M;    while(cin>>_k>>_b>>_n>>M)    {        init1();        a=quick_mod(b,2,_b,M);        init1();        b=quick_mod(b,2,_k,M);        init2();        c=quick_mod(b,4,_n,M);        __int64 ans=0;        b.t[0][0]=c.t[0][2];        b.t[0][1]=c.t[0][3];        b.t[1][0]=c.t[1][2];        b.t[1][1]=c.t[1][3];        c=multiple(a,b,2,M);        ans=c.t[1][0];        cout<<ans<<endl;    }    return 0;}/*F為斐波那契數列:F[0]=0,F[1]=1,F[n]=F[n-1]+F[n-2];g為一個函數,g(i)=k*i+b;S[n]=∑F[g(i)]=F[b]+F[k+b]+F[k*2+b]+……+F[k*(n-1)+b]我們設A為斐波那契數列的初始矩陣,B為轉換矩陣那麼S[n]=A*B^b+A*B^(k+b)+A*B^(k*2+b)+……+A*B^((n-1)*k+b)=A*B^b*(I+B^k+B^(2*k)+……+B^((n-1)*k))那麼我們這裡只需要求出A,B^b和I+B^k+B^(2*k)+……+B^((n-1)*k)就可以了當然前兩個比較好求,關鍵是後面這個,首先我們可以先求出B^k=C然後在構造一個4*4的矩陣來求和|C I| 其二次方是|C^2 I+C| 三次方|C^3 I+C+C^2| …… 這樣我們就能求出和了。|0 I|          |0     I|       |0         I|*/




聯繫我們

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