Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2077 Accepted Submission(s): 899
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.
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.
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
S(n) = f(0) + f(1) + ... + f(n) = f(0) + (A + A^2 + ... + A^n)*F所得矩阵的右上角的值
那么如何求得A + A^2 + ... + A^n呢
可以继续构造如下的分块矩阵,其中I是单位矩阵
|A I|
|0 I|
令R等于上面的矩阵,则
R^2 = |A^2 A*I + I|
|0 I |
R^3 = |A^3 A^2 * I + A * I + I|
| 0 I |
可以发现右上角即为I + A + A^2 + ... + A^n,多个I后面给减掉就可以了
这样我们同样可以再次利用矩阵幂次求得R^n
#include <iostream>
using namespace std;
const int MAX = 4;
const int max1=2;
int Mod;
typedef struct{
long long m[MAX][MAX];
} Matrix;
typedef struct{
long long m[max1][max1];
} Matrix2;
Matrix P = {0,1,1,0,
0,0,0,1,
0,0,1,0,
0,0,0,1
};
Matrix I = {1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
Matrix2 P1={0,1,
1,1};
Matrix2 I1={1,0,
0,1};
Matrix matrixmul(Matrix a,Matrix b)
{
int i,j,k;
Matrix c;
for (i = 0 ; i < MAX; i++)
for (j = 0; j < MAX;j++)
{
c.m[i][j] = 0;
for (k = 0; k < MAX; k++)
{
c.m[i][j]+=((a.m[i][k]%Mod)*(b.m[k][j]%Mod))%Mod;
}
c.m[i][j] %= Mod;
}
return c;
}
Matrix quickpow(long long n)
{
Matrix m = P, b = I;
while (n >= 1)
{
if (n & 1)
b = matrixmul(b,m);
n = n >> 1;
m = matrixmul(m,m);
}
return b;
}
Matrix2 matrixmul1(Matrix2 a,Matrix2 b)
{
int i,j,k;
Matrix2 c;
for (i = 0 ; i < max1; i++)
for (j = 0; j < max1;j++)
{
c.m[i][j] = 0;
for (k = 0; k < max1; k++)
{
c.m[i][j]+=((a.m[i][k]%Mod)*(b.m[k][j]%Mod))%Mod;
}
c.m[i][j] %= Mod;
}
return c;
}
Matrix2 quickpow1(long long n)
{
Matrix2 m = P1, b = I1;
while (n >= 1)
{
if (n & 1)
b = matrixmul1(b,m);
n = n >> 1;
m = matrixmul1(m,m);
}
return b;
}
int main()
{
int k,b,n;
Matrix2 tp1,tp2;
Matrix tp;
while(cin>>k>>b>>n>>Mod)
{
tp1=quickpow1(k);
tp2=quickpow1(b);
P.m[0][0]=tp1.m[0][0];
P.m[0][1]=tp1.m[0][1];
P.m[1][0]=tp1.m[1][0];
P.m[1][1]=tp1.m[1][1];
tp=quickpow(n);
long long tmp=(tp2.m[0][0]%Mod*tp.m[0][3]%Mod)%Mod+(tp2.m[0][1]%Mod*tp.m[1][3]%Mod)%Mod;
tmp=(tmp+Mod)%Mod;
cout<<tmp<<endl;
}
return 0;
}