Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2403 Accepted Submission(s): 1006
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
Recommend
思路:
- 构造矩阵:
- |1 1| | f(2) f(1)|
- A= |1 0| = | f(1) f(0)|
- |1 1| ^b | f(b+1) f(b)|
- A^b =|1 0| = | f(b) f(b-1)|
- f(b) = matrix[0][1]=matrix[1][0];
- 首项是:A^b
- 公比是:A^k
- 项数是:N
- 可以把问题进一步简化
- 因为矩阵的加法对乘法也符合分配律,我们提出一个A^b来,形成这样的式子:
- A^b*( I + A^k + (A^k)^2 + .... + (A^k)^(N-1) )
- 化简成A^b+A^b*( B+B^2+……B^(n-1) ),其中B=A^
- G(N)=B^I + ... + B^(N-1),
- 设q为N/2
- 如果q为偶数
- 则G(N)=G(q)+G(q)*B^q
- 如果q为奇数的话
- G(N)=G(q)+B^(q+1)+G(q)*B(q+1)
- ac代码
#include<stdio.h> #include<string.h> struct s { __int64 m[2][2]; }; __int64 k,x,n,m; struct s muti(struct s a,struct s b) { __int64 i,j,k; struct s c; memset(c.m,0,sizeof(c.m)); for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(k=0;k<2;k++) { c.m[i][j]+=(a.m[i][k]*b.m[k][j])%m; } c.m[i][j]%=m; } } return c; } struct s addm(struct s a,struct s b) { struct s c; int i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) c.m[i][j]=(a.m[i][j]+b.m[i][j])%m; } return c; } struct s powm(struct s a,__int64 n) { struct s b; // int i,j; b.m[0][0]=b.m[1][1]=1; b.m[0][1]=b.m[1][0]=0; while(n) { if(n&1) b=muti(b,a); a=muti(a,a); n/=2; } return b; } struct s badd(struct s a,__int64 n) { struct s g,o; if(n==1) { struct s tt=a; return tt; } g=badd(a,n/2); if(n&1) { o=powm(a,n/2+1); return addm(addm(g,o),muti(g,o)); } else { o=powm(a,n/2); return addm(g,muti(g,o)); } } int main() { //int k,b,n,m while(scanf("%I64d%I64d%I64d%I64d",&k,&x,&n,&m)!=EOF) { struct s a,t,mb,ans; a.m[0][0]=a.m[1][0]=a.m[0][1]=1; a.m[1][1]=0; t=powm(a,k); t=badd(t,n-1); mb=powm(a,x); ans=addm(mb,muti(mb,t)); printf("%I64d\n",ans.m[1][0]); } }