Hdu 1588 Gauss Fibonacci (matrix Rapid power)
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2090 Accepted Submission (s): 903
Problem DescriptionWithout 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 shoshould 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 has Ted 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 maid is described as follows:
Given k, B, n, calculate the sum of every f (g (I) for 0 <= I The answer may be very large, so you shoshould divide this answer by M and just output the remainder instead.
InputThe 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.
OutputFor each line input, out the value described above.
Sample Input
2 1 4 1002 0 4 100
Sample Output
2112
AuthorDYGG
SourceHDU "Valentines Day" Open Programming Contest 2007-02-14
Question and code:
#include
#include
#include
using namespace std;const int mod=1e9;struct mat{ __int64 t[4][4]; void set() { memset(t,0,sizeof(t)); }} a,b,c;mat multiple(mat a,mat b,int n,int p){ int i,j,k; mat temp; temp.set(); for(i=0; i
>=1; b=multiple(b,b,n,p); } return t;}void init1(){ b.set(); b.t[0][1]=1; b.t[1][0]=1; b.t[1][1]=1;}void init2(){ b.t[0][2]=1; b.t[1][3]=1; b.t[2][2]=1; b.t[3][3]=1;}int main(){ int _k,_b,_n,M; while(cin>>_k>>_b>>_n>>M) { init1(); a=quick_mod(b,2,_b,M); init1(); b=quick_mod(b,2,_k,M); init2(); c=quick_mod(b,4,_n,M); __int64 ans=0; b.t[0][0]=c.t[0][2]; b.t[0][1]=c.t[0][3]; b.t[1][0]=c.t[1][2]; b.t[1][1]=c.t[1][3]; c=multiple(a,b,2,M); ans=c.t[1][0]; cout<