现在的位置: 首页 > 综合 > 正文

HDU1588 Gauss Fibonacci 矩阵乘法

2012年07月07日 ⁄ 综合 ⁄ 共 2704字 ⁄ 字号 评论关闭

//g(i)=k*i+b
//f(i): fibonacci number
//s(n)=f(g(0))+f(g(1))+...+f(g(n));
//[ f(n+1)  f(n)   ]   = [ 1  1 ] ^n
//[ f(n)    f(n-1) ]     [ 1  0 ]
//s(n)=sum(A^(k*i+b))  (0<=i<n)
//s(n)=A^b*(sum((A^k)^i))  (0<=i<n)
//B=[ [A I];
//    [0 I] ]
//B^(k+1) = [ [A^k   I+A+...+A^k];
//            [0       I        ] ]

Gauss Fibonacci

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1610    Accepted Submission(s): 697

Problem Description
Without 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.

 

Input
The 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.
 

Output
For each line input, out the value described above.
 

Sample Input
2 1 4 100 2 0 4 100
 

Sample Output
21 12
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#define MAXN 32

using namespace std;

struct Matrix
{
    int size;
    long long modulo;
    long long element[MAXN][MAXN];
    void setSize(int);
    void setModulo(long long);
    Matrix operator* (Matrix);
    Matrix power(int);

};

void Matrix::setSize(int a)
{
    for (int i=0; i<a; i++)
        for (int j=0; j<a; j++)
            element[i][j]=0;
    size = a;
}

void Matrix::setModulo(long long a)
{
    modulo = a;
}

Matrix Matrix::operator* (Matrix param)
{
    Matrix product;
    product.setSize(size);
    product.setModulo(modulo);
    for (int i=0; i<size; i++)
        for (int j=0; j<size; j++)
            for (int k=0; k<size; k++)
            {
                product.element[i][j]+=element[i][k]*param.element[k][j];
                product.element[i][j]%=modulo;
            }
    return product;
}

Matrix Matrix::power(int exp)
{
    if(exp==0)
    {
        Matrix tmp;
        tmp.setSize(size);
        for(int i=0;i<size;i++)
            tmp.element[i][i]=1;
        return tmp;
    }
    Matrix tmp = (*this) * (*this);
    if (exp==1) return *this;
    else if (exp & 1) return tmp.power(exp/2) * (*this);
    else return tmp.power(exp/2);
}

int k,b,n,m;
Matrix a,ab,ak,aki,ans;

int main()
{
    while(~scanf("%d%d%d%d",&k,&b,&n,&m))
    {
        a.setSize(2);
        a.setModulo(m);
        a.element[0][0]=a.element[0][1]=a.element[1][0]=1;
        ab=a.power(b);
        ak=a.power(k);
        aki.setSize(4);
        aki.setModulo(m);
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                aki.element[i][j]=ak.element[i][j];
        for(int i=0;i<2;i++)
            aki.element[i][i+2]=aki.element[i+2][i+2]=1;
        aki=aki.power(n);
        ans.setSize(2);
        ans.setModulo(m);
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                ans.element[i][j]=aki.element[i][j+2];
        ans=ab*ans;
        printf("%lld\n",ans.element[0][1]);
    }
    return 0;
}

抱歉!评论已关闭.