hdu 1588 Gauss Fibonacci 矩阵的高次高速求幂,矩阵的高速求和,斐波那契公式

Gauss Fibonacci

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


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 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
 

Author
DYGG
 

Source
HDU “Valentines Day” Open Programming Contest 2007-02-14
 
 
 
 
 
这个题目我真心觉得超级难,我做完这道题时真心精力耗尽了。
 
这个题目用了三个知识,斐波那契数,矩阵的高次幂求法,还有就是矩阵的求和的快速公式,后两个全部是二分法的思路:
 
嘿嘿,这个题目的主要思路是f(k*i+b)=a^(k*i+b),所以求其和就是a^b+a^(k+b)+a^(2*k+b)+(省略)。。。。。。,这样就是了嘛。所以把a^b提出来,可以写成a^b(
1+a^k+a^(2*k)+a^(3*k)),先求出a^b是多少,再求出a^k 是多少,在求其和,再与a^b相乘就是了嘛,很简单嘛。这里的a矩阵是{1,1,1,0};
 
 
写起来可是真心不容易啊!!!!
 
#include
#include
#include
using namespace std;
int i,j,k,b,n,m;
struct info
{
    long long int a[3][3];

};
info unit;
info mat;
info tp;
info mut(info x,info y)
{
    info c;
    for(i=1;i<3;i++)
    for(j=1;j<3;j++)
    c.a[i][j]=0;
    for(i=1;i<3;i++)
    for(j=1;j<3;j++)
    for(k=1;k<3;k++)
    {
       c.a[i][j]+=x.a[i][k]*y.a[k][j];
       c.a[i][j]=c.a[i][j]%m;
    }
     return c;
}

info pow(info p,int k)
{
    tp=p;
    p=unit;
    while(k)
    {
        if(k&1)
        p=mut(p,tp);
        tp=mut(tp,tp);
        k>>=1;
    }
    return p;

}
info add(info x,info y)
{
    info c;
    for(i=1;i<3;i++)
    {
        for(j=1;j<3;j++)
        {
            c.a[i][j]=(x.a[i][j]+y.a[i][j])%m;
        }
    }
    return c;

}
info f(int k,info p)
{
    if(k==1)
    return  p;
    else
    if(k&1)
    return add(f(k-1,p),pow(p,k));
    else
    return mut(f(k>>1,p),add(pow(p,k>>1),unit));
}
int main()
{


    info ak,ab,ans;
    unit.a[1][1]=1;unit.a[1][2]=0;
    unit.a[2][1]=0;unit.a[2][2]=1;
    mat.a[1][1]=1;mat.a[1][2]=1;
    mat.a[2][1]=1;mat.a[2][2]=0;
    while(scanf("%d%d%d%d",&k,&b,&n,&m)!=EOF)
    {

        ak=pow(mat,k);
        ab=pow(mat,b);
        ans=add(unit,f(n-1,ak));
        ans=mut(ans,ab);
        printf("%d\n",ans.a[2][1]);
    }
    return 0;
}

 

你可能感兴趣的:(数学问题)