hdu 1588 Gauss Fibonacci(斐波那契,等比数列求和,矩阵快速幂)

Gauss Fibonacci

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


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
题意:给k,b,n,M,作用如题,求F(g(n))之和

思路:首先n很大,可以想到用矩阵快速幂。  斐波那契数列的构造矩阵是A=(1,1)

                                                                                                              (1,0)

然后是如何处理g(i)的问题,我们进行一下公式推理

F(g(1))+...+F(g(n))

=A(b)+A(k+b)+...+A((n-1)*k+b)            (括号表示幂)

=A(b)*(1+A(k)+A(2*k)+...+A((n-1)*k))

令B=A(k)

=A(b)*(B(0)+B(1)+..+B(n-1))

也就是矩阵的等比数列求和了。

那么我们求出A(b)和B后就可以直接套用求和公式,最后因为斐波那契数列里F(n)是右上角,所以取[0][0]即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long mod;
struct Matrix
{
    long long ma[2][2];
};
Matrix res;
Matrix mult(Matrix A,Matrix B)
{
    Matrix C;
    memset(C.ma,0,sizeof(C.ma));
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            for(int k=0; k<2; k++)
                C.ma[i][j]=(C.ma[i][j]+A.ma[i][k]*B.ma[k][j])%mod;
    return C;
}
Matrix pow_mod(Matrix A,long long n)
{
    Matrix B;
    B.ma[0][0]=B.ma[1][1]=1;
    B.ma[0][1]=B.ma[1][0]=0;
    while(n)
    {
        if(n&1) B=mult(B,A);
        A=mult(A,A);
        n>>=1;
    }
    return B;
}
Matrix add(Matrix a,Matrix b){
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            a.ma[i][j]=(a.ma[i][j]+b.ma[i][j])%mod;
        }
    }
    return a;
}
Matrix cal(Matrix A,long long k) ///用二分法求矩阵和,递归实现 计算 a^1+a^2.....+a^p
{
    if(k==1)
        return A;
    else if(k&1)
        return add(cal(A,k-1),pow_mod(A,k));
    else
        return mult(cal(A,k>>1),add(pow_mod(A,k>>1),res));
}
int main()
{
    long long k,b,n;
    while(~scanf("%lld %lld %lld %lld",&k,&b,&n,&mod))
    {
        Matrix A,B;
        A.ma[0][0]=A.ma[0][1]=A.ma[1][0]=1;
        A.ma[1][1]=0;
        res.ma[0][0]=res.ma[1][1]=1;
        res.ma[0][1]=res.ma[1][0]=0;
        B=A;
        A=pow_mod(A,b);
        B=pow_mod(B,k);
        B=add(res,cal(B,n-1));
        A=mult(A,B);
        printf("%lld\n",A.ma[0][1]);
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值