hdu 1588 Gauss Fibonacci(矩阵乘法)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1588

Gauss Fibonacci

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


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
 

推导:


所以计算过程中涉及到了两个不同规模的矩阵。当然也可以用一种大矩阵表示两种不同的小矩阵,在矩阵计算中控制好边界即可:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
struct matrie1{
    LL m[2][2];
};
struct matrie2{
    LL m[4][4];
};
matrie1 A={
    0,1,
    1,1,
};
matrie1 AI={
    1,0,
    0,1
};
matrie2 B={
0,0,1,0,
0,0,0,1,
0,0,1,0,
0,0,0,1
};
matrie2 BI={
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
int k,b,n,mod;
matrie1 multi1(matrie1 a,matrie1 b){
    matrie1 q;
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            q.m[i][j]=0;
            for(int k=0;k<2;k++){
                q.m[i][j]+=a.m[i][k]*b.m[k][j]%mod;
            }
            q.m[i][j]%=mod;
        }
    }
    return q;
}
matrie1 pow1(int k){
    matrie1 q=AI,tmp=A;
    while(k){
        if(k&1)q=multi1(q,tmp);
        tmp=multi1(tmp,tmp);
        k>>=1;
    }
    return q;
}
matrie2 multi2(matrie2 a,matrie2 b){
    matrie2 q;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            q.m[i][j]=0;
            for(int k=0;k<4;k++){
                q.m[i][j]+=a.m[i][k]*b.m[k][j]%mod;
            }
            q.m[i][j]%=mod;
        }
    }
    return q;
}
matrie2 pow2(int k){
    matrie2 q=BI,tmp=B;
    while(k){
        if(k&1)q=multi2(q,tmp);
        tmp=multi2(tmp,tmp);
        k>>=1;
    }
    return q;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~(scanf("%d %d %d %d",&k,&b,&n,&mod))){
         matrie1 R=pow1(k);
         for(int i=0;i<2;i++){
             for(int j=0;j<2;j++)B.m[i][j]=R.m[i][j];
         }
         matrie2 BN=pow2(n);
         matrie1 B01;
         for(int i=0;i<2;i++){
             for(int j=2;j<4;j++)B01.m[i][j-2]=BN.m[i][j];
         }
         matrie1 Ab=pow1(b);
         matrie1 res=multi1(Ab,B01);
         printf("%d\n",res.m[0][1]%mod);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值