hdu1588(斐波那契,矩阵连乘)

202 篇文章 0 订阅
154 篇文章 0 订阅

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

让我们求

sn= f(b)+ f(k+b)+ f(2k+b)+……+ f(nk+b).


运用以上方法,矩阵连乘,解决问题!

(我的代码求别改,这段代码好不容易才改对的,本来的代码老是ce!!!改成这样我也不造哪里错了又改对了,求大牛指导哭

#include<iostream>
#include<stdio.h>
using namespace std;
const int max0=2;
const int max1=4;
typedef long long ll;
int mod;

typedef struct{
long long zj[max0][max0];
} matrix;
typedef struct{
long long zj[max1][max1];
} matrix1;

matrix I={1,0,0,1};
matrix j2={0,1,1,1};
matrix1 jj1={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
matrix1 jj2={0,0,1,0,
0,0,0,1,
0,0,1,0,
0,0,0,1};

matrix lc1(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<max0;i++)
    {
        for(int j=0;j<max0;j++)
        {
            c.zj[i][j]=0;
            for(int k=0;k<max0;k++)
            c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod;
            c.zj[i][j]%=mod;
        }
    }
    return c;
}
matrix a,b;
matrix1 a0,b0;
matrix quick1(ll n)
{
    a=j2,b=I;
    while(n>=1)
    {
        if(n&1)
            b=lc1(b,a);
        n>>=1;
        a=lc1(a,a);
    }
    return b;
}
matrix1 lc2(matrix1 a,matrix1 b)
{
    matrix1 c;
    for(int i=0;i<max1;i++)
    {
        for(int j=0;j<max1;j++)
        {
            c.zj[i][j]=0;
            for(int k=0;k<max1;k++)
            c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod;
            c.zj[i][j]%=mod;
        }
    }
    return c;
}

matrix1 quick2(ll n)
{
    a0=jj2,b0=jj1;
    while(n>=1)
    {
        if(n&1)
            b0=lc2(b0,a0);
        n>>=1;
        a0=lc2(a0,a0);
    }
    return b0;
}

int main()
{
    ll k,bbb,n;
    while(cin>>k>>bbb>>n>>mod)
    {
        matrix tm1=quick1(k);
        matrix tm2=quick1(bbb);
        jj2.zj[0][0]=tm1.zj[0][0],jj2.zj[0][1]=tm1.zj[0][1],jj2.zj[1][0]=tm1.zj[1][0],jj2.zj[1][1]=tm1.zj[1][1];
        matrix1 tm=quick2(n);
        long long tmp=(tm2.zj[0][0]%mod*tm.zj[0][3]%mod)%mod+(tm2.zj[0][1]%mod*tm.zj[1][3]%mod)%mod;
        tmp=(tmp+mod)%mod;
        cout<<tmp<<endl;
    }
    return 0;
}

原来ce代码 哭,求指错!!:

#include<iostream>
#include<stdio.h>
using namespace std;
const int max0=2;
const int max1=4;
typedef long long ll;
int mod;

typedef struct{
long long zj[max0][max0];
} matrix;
typedef struct{
long long zj[max1][max1];
} matrix1;

matrix j0={1,0,0,1};
matrix j2={0,1,1,1};
matrix1 jj1={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
matrix1 jj2={0,0,1,0,
0,0,0,1,
0,0,1,0,
0,0,0,1};

matrix lc1(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<max0;i++)
    {
        for(int j=0;j<max0;j++)
        {
            c.zj[i][j]=0;
            for(int k=0;k<max0;k++)
            c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod;
            c.zj[i][j]%=mod;
        }
    }
    return c;
}
matrix quick1(ll n)
{
    matrix a=j2,b=j0;
    while(n>=1)
    {
        if(n&1)
            b=lc1(b,a);
        n>>=1;
        a=lc1(a,a);
    }
    return b;
}
matrix1 lc2(matrix1 a,matrix1 b)
{
    matrix1 c;
    for(int i=0;i<max1;i++)
    {
        for(int j=0;j<max1;j++)
        {
            c.zj[i][j]=0;
            for(int k=0;k<max1;k++)
            c.zj[i][j]+=((a.zj[i][k]%mod)*(b.zj[k][j]%mod))%mod;
            c.zj[i][j]%=mod;
        }
    }
    return c;
}

matrix1 quick2(ll n)
{
    matrix1 a=jj2,b=jj1;
    while(n>=1)
    {
        if(n&1)
            b=lc2(b,a);
        n>>=1;
        a=lc2(a,a);
    }
    return b;
}

int main()
{
    ll k,bbb,n;
    while(cin>>k>>bbb>>n>>mod)
    {
        matrix tm1=quick1(k);
        matrix tm2=quick1(bbb);
        jj2.zj[0][0]=tm1.zj[0][0],jj2.zj[0][1]=tm1.zj[0][1],jj2.zj[1][0]=tm1.zj[1][0],jj2.zj[1][1]=tm1.zj[1][1];
        matrix1 tm=quick2(n);
        long long tmp=(tm2.zj[0][0]%mod*tm.zj[0][3]%mod)%mod+(tm2.zj[0][1]%mod*tm.zj[1][3]%mod)%mod;
        tmp=(tmp+mod)%mod;
        cout<<tmp<<endl;
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值