您的位置:首页 > 其它

HDU 1588 斐波那契数列数列变形和矩阵连乘

2014-07-26 12:00 316 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1588

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

题目大意:求Fibonacci数列的固定项的和。

解题思路:

                


                 


方法如下:

                                       


另外值得一提的是:我们在构造矩阵的时候要用  1   1                              f(n-2)                f[n-1]

                                                                                                                                            =  

                                                                                       1    0                             f[n-1]                 f
    的形式,这样一来我们就不用再讨论n是否为零的情况了,若f[n-1]在f[n-2]上方那么f[0]是无法用矩阵运算表示的。

代码如下:

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
typedef long long LL;
LL k,b,n,MOD;
const int N=4;
const int M=2;
struct Matrix1
{
LL m

;
};
struct Matrix2
{
LL m[M][M];
};
Matrix2 I2=
{
1,0,
0,1
};
Matrix1 I1=
{
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
Matrix1 multi1(Matrix1 a,Matrix1 b)
{
Matrix1 c;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
c.m[i][j]=0;
for(int k=0; k<N; k++)
{
c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD;
}
c.m[i][j]=c.m[i][j]%MOD;
}
return c;
}
Matrix1 quick_mod1(Matrix1 a,LL k)
{
Matrix1 ans=I1;
while(k!=0)
{
if(k&1)
{
ans=multi1(ans,a);
}
k>>=1;
a=multi1(a,a);
}
return ans;
}
Matrix2 multi2(Matrix2 a,Matrix2 b)
{
Matrix2 c;
for(int i=0; i<M; i++)
for(int j=0; j<M; j++)
{
c.m[i][j]=0;
for(int k=0; k<M; k++)
{
c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD;
}
c.m[i][j]=c.m[i][j]%MOD;
}
return c;
}
Matrix2 quick_mod2(Matrix2 a,LL k)
{
Matrix2 ans=I2;
while(k!=0)
{
if(k&1)
{
ans=multi2(ans,a);
}
k>>=1;
a=multi2(a,a);
}
return ans;
}
int main()
{
while(~scanf("%I64d%I64d%I64d%I64d",&k,&b,&n,&MOD))
{
Matrix2 A={0,1,
1,1
};
Matrix1 P={0,0,1,0,
0,0,0,1,
0,0,1,0,
0,0,0,1};
Matrix2 t1=quick_mod2(A,b);
Matrix2 t2=quick_mod2(A,k);
P.m[0][0]=t2.m[0][0];
P.m[0][1]=t2.m[0][1];
P.m[1][0]=t2.m[1][0];
P.m[1][1]=t2.m[1][1];
Matrix1 t3=quick_mod1(P,n);
LL x=(t3.m[0][3]*t1.m[0][0]%MOD+t3.m[1][3]*t1.m[0][1]%MOD)%MOD;
if(x<MOD)
x+MOD;
printf("%I64d\n",x);
}
return 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: