HDU1588解题报告

Gauss Fibonacci

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


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
 

Author
DYGG
 

Source
 

Recommend
linle




             参考代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<ctime>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define pb push_back
#define mp make_pair
#define CLR(x) memset(x,0,sizeof(x))
#define _CLR(x) memset(x,-1,sizeof(x))
#define REP(i,n) for(int i=0;i<n;i++)
#define Debug(x) cout<<#x<<"="<<x<<" "<<endl
#define REP(i,l,r) for(int i=l;i<=r;i++)
#define rep(i,l,r) for(int i=l;i<r;i++)
#define RREP(i,l,r) for(int i=l;i>=r;i--)
#define rrep(i,l,r) for(int i=1;i>r;i--)
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
using namespace std;
int k,b,n,M;
struct mat
{
    ll d[3][3];  //必须用long long,否则会爆int
} A,B,C,E;

mat multi(mat a,mat b)
{
    mat ans;
    rep(i,0,2)
    {
        rep(j,0,2)
        {
            ans.d[i][j]=0;
            rep(k,0,2)
                ans.d[i][j]+=a.d[i][k]*b.d[k][j];
            ans.d[i][j]%=M;
        }
    }
    return ans;
}

mat add(mat a,mat b)
{
    mat ans;
    rep(i,0,2)
        rep(j,0,2)
           ans.d[i][j]=(a.d[i][j]+b.d[i][j])%M;
    return ans;
}

mat quickmulti(mat A,int n)
{
    if(n==0) return E;
    if(n==1) return A;
    mat ans=E;
    while(n)
    {
        if(n&1)
        {
            n--;
            ans=multi(ans,A);
        }
        else
        {
            n>>=1;
            A=multi(A,A);
        }
    }
    return ans;
}

mat binarymulti(mat A,int n)   //二分矩阵等比之和
{
    if(n==1) return A;
    mat tmp=binarymulti(A,n/2),tmp1=quickmulti(A,n/2);
    mat ans=add(tmp,multi(tmp,tmp1));
    if(n&1) ans=add(ans,multi(multi(tmp1,tmp1),A));  //multi(multi(tmp1,tmp1),A)即A^n
    return ans;
}

int main()
{
    E.d[0][0]=E.d[1][1]=A.d[0][0]=A.d[0][1]=A.d[1][0]=B.d[0][0]=1;
    E.d[0][1]=E.d[1][0]=A.d[1][1]=B.d[0][1]=B.d[1][0]=B.d[1][1]=0;
    C.d[0][0]=C.d[0][1]=C.d[1][1]=0,C.d[1][0]=1;
    //E表示单位矩阵,A表示斐波那契数列的矩阵,B矩阵是包含F(1)和F(0)的列向量,C矩阵相当于是包含F(0)和F(-1)的矩阵,C矩阵主要针对b=0的情况。              
    while(~scanf("%d%d%d%d",&k,&b,&n,&M))
    {
        mat mt1=B,mt2=quickmulti(A,k); //mt2表示A^k
        if(b==0) mt1=C;
        else mt1=multi(quickmulti(A,b-1),mt1); //计算出包含F(b)和F(b-1)的列向量
        mt2=binarymulti(mt2,n-1);  //计算A+A^2+……+A^(n-1)的和矩阵
        mt2=add(mt2,E);  //加上A的0次幂,即单位矩阵
        mt1=multi(mt2,mt1); //乘以包含F(b)和F(b-1)的列向量
        printf("%I64d\n",mt1.d[0][0]%M);
    }
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值