首页 > ACM题库 > HDU-杭电 > HDU 1588 Gauss Fibonacci-分治-[解题报告] C++
2013
12-12

HDU 1588 Gauss Fibonacci-分治-[解题报告] C++

Gauss Fibonacci

问题描述 :

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.

输入:

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.

输出:

For each line input, out the value described above.

样例输入:

2 1 4 100
2 0 4 100

样例输出:

21
12

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
struct Matrix
{
    long long m[3][3];
};
Matrix E,B;
long long kk,bb,nn,mod;
void Init()
{
    for(int i=1;i<=2;i++)
    for(int j=1;j<=2;j++)
        E.m[i][j]=(i==j);
    B.m[1][1]=B.m[1][2]=1;
    B.m[2][1]=1;
    B.m[2][2]=0;
}
Matrix Multi(Matrix A,Matrix B)
{
    Matrix ans;
    for(int i=1;i<=2;i++)
    for(int j=1;j<=2;j++)
    {
        ans.m[i][j]=0;
        for(int k=1;k<=2;k++)
        {
            ans.m[i][j]+=A.m[i][k]*B.m[k][j];
            ans.m[i][j]%=mod;
        }
    }
    return ans;
}
Matrix Pow(Matrix A,int k)
{
    Matrix ans=E;
    while(k)
    {
        if(k&1)
        {
            k--;
            ans=Multi(ans,A);
        }
        else
        {
            k/=2;
            A=Multi(A,A);
        }
    }
    return ans;
}
Matrix Add(Matrix A,Matrix B)
{
    Matrix ans;
    for(int i=1;i<=2;i++)
    for(int j=1;j<=2;j++)
    {
        ans.m[i][j]=(A.m[i][j]+B.m[i][j])%mod;
    }
    return ans;
}
Matrix Sum(Matrix mat,long long n)
{
    Matrix ans;
    if(n==0)
    {
        ans.m[1][1]=ans.m[1][2]=0;
        ans.m[2][1]=ans.m[2][2]=0;
        return ans;
    }
    else if(n==1)
    {
        return mat;
    }
    else
    {    //  cnt=A^k
        // sum(n) = A^k+A^2k+...A^(n-1)k  
        //        = cnt+cnt^2+cnt^3+...cnt^(n-1) 
        //   t=sum(n/2)
        // sum(n) = sum(n/2) + cnt^(n/2+1)+cnt^(n/2+2)+...cnt^(n-1)
        // sum(n) =  t +  cnt^(n/2+1)+cnt^(n/2+2)+...cnt^(n-1)
        // sum(n) =  t +  t * cnt^(n/2)   
        //  递归到最后的时候,n为奇数时直接算,相当于 n=39时
        //  sum(39) = cnt^1+cnt^2+...+cnt^39  t=19
        //  sum(39) = sum(19)+sum(19)*cnt^19+ cnt^39 (最后一项是因为n为奇数所余留的一项) 
        Matrix res,t;
        t=Sum(mat,n/2);
        res=Add(t,Multi(Pow(mat,n/2),t));
        if(n&1)
            res=Add(res,Pow(mat,n));
        return res;
    }
}
int main()
{
    while(scanf("%lld%lld%lld%lld",&kk,&bb,&nn,&mod)==4)
    {
        Init();
        Matrix M1=Pow(B,bb);
        Matrix M2=Pow(B,kk);
        Matrix res=Multi(M1,Add(E,Sum(M2,nn-1)));
        printf("%lld\n",res.m[1][2]);
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/shiyuankongbu/article/details/8458459