您的位置:首页 > 其它

HDU1588 Gauss Fibonacci (矩阵快速幂+等比数列二分求和)

2016-07-21 09:47 363 查看


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

     12
      21

      对于fibonacci列的求法,是利用矩阵A的n次方;
     但这道题要求的是i从0到n-1时f(ki+b)的和,有如下的演算



对于等比数列和的求法,有二分求等比数列



代码:
#include<cstdio>

#include<cstring>

#include<algorithm>

#define mem(a,x) memset(a,x,sizeof(a))

#define
4000
Rep(i,x,n) for(int i=x;i<=n;i++)

using namespace std;

const int NUM = 2;

int maxn,mod;

typedef long long ll;

struct Matrix

{

    ll a[NUM][NUM];

    void init()

    {

        mem(a,0);

        for(int i=0;i<maxn;i++){

            a[i][i] = 1;

        }

    }

};

Matrix add(Matrix a,Matrix b)

{

    Matrix ans;

    mem(ans.a,0);

    for(int i=0;i<maxn;i++){

        for(int j=0;j<maxn;j++){

            ans.a[i][j] = a.a[i][j]+b.a[i][j];

            ans.a[i][j] %= mod;

        }

    }

    return ans;

}

Matrix mul(Matrix a,Matrix b)

{

    Matrix ans;

    for(int i=0;i<maxn;i++){

        for(int j=0;j<maxn;j++){

            ans.a[i][j] = 0;

            for(int k=0;k<maxn;k++){

                ans.a[i][j] += a.a[i][k]*b.a[k][j];

                ans.a[i][j] %= mod;

            }

        }

    }

    return ans;

}

Matrix pow(Matrix a,ll n)

{

    Matrix ans;

    ans.init();

    while(n){

        if(n&1)

            ans = mul(ans,a);

        n >>= 1;

        a = mul(a,a);

    }

    return ans;

}

Matrix sum(Matrix a,ll k)

{

    if(k==1)    return a;

    Matrix t = sum(a,k/2);

    if(k&1){

        Matrix cur = pow(a,k/2+1);

        t = add(t,mul(t,cur));

        t = add(t,cur);

    }

    else{

        Matrix cur = pow(a,k/2);

        t = add(t,mul(t,cur));

    }

    return t;

}

int main()

{

    int m;

    ll k,b,n;

    while(scanf("%lld %lld %lld %d",&k,&b,&n,&m)!=EOF){

        if(k==0&&b==0){

            printf("0\n");

            continue;

        }

        mod = m;    maxn = 2;

        Matrix P ={1,1,1,0},A,B,S1,S2,S3;

        A = pow(P,k);

        B={0,0,0,0};

        if(b>0){

            B = pow(P,b-1);

            S1 = sum(A,n-1);

            S2 = mul(B,S1);

            S3 = add(B,S2);

        }

        else{

            B = pow(P,k-1);

            S1 = sum(A,n-2);

            S2 = mul(B,S1);

            S3 = add(B,S2);

        }

        printf("%lld\n",S3.a[0][0]);

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: