Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2932 Accepted Submission(s): 1211
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
- /*******************************************************************************************
- 首先我们看g=k*i+b; 是一个等差数列
- 如果能推出f(g)这个函数也是一个等差或者等比数列,就可以得出一个公式
- f(k*i+b) 0<=i<n
- 难点构造矩阵!!!!
- 建立一个二分矩阵A=[1 1,1 0]
- f(b)=A^b
- 则:
- f(b)=A^b
- f(k+b)=A^k+b
- f(2*k+b)=A^2*k+b
- .
- .
- .
- f((n-1)*k+b)=A^(n-1)*k+b
- 我们就可以得出一个等比数列:
- 首项:A^b
- 公比:A^k
- 项数:n
- (res是单位矩阵)
- 运用等比数列求和公式得出:sum=A^b*(res+A^k+(A^k)^2+(A^k)^3+...+(A^k)^(n-1))
- 或者直接求 sum=A^b + A^(k+b)+(A^(b+2k))...+(A^((n-1)*k+b))
- 需要注意的一点是:当b=0的情况
- ***************************************************************************************/
- /***************************************************************************************
- 例如:A+A^2+A^3+A^4+A^5+A^6=(A+A^2+A^3)+A^3*(A+A^2+A^3) 用递归写
- ***************************************************************************************/
/*!!!!!!!!爆int*/ #include<stdio.h> #include<math.h> #include<stdlib.h> #include<algorithm> #include<string.h> #include<vector> using namespace std; const int N = 2; int M,k,b; struct Matri{ int a[N][N]; Matri(){ memset(a,0,sizeof(a)); } }; Matri multi(Matri A,Matri B){ int i,j,k; Matri tmp; for(i=0;i<N;i++){ for(j=0;j<N;j++){ for(k=0;k<N;k++) tmp.a[i][j] = (tmp.a[i][j] + 1LL*A.a[i][k]*B.a[k][j])%M; } } return tmp; } Matri add(Matri A,Matri B){ int i,j; Matri tmp; for(i=0;i<N;i++){ for(j=0;j<N;j++){ tmp.a[i][j] = (0LL+A.a[i][j]+B.a[i][j])%M; } } return tmp; } Matri pow(Matri A,long long t){ Matri tmp; int i,j; for(i=0;i<N;i++) tmp.a[i][i] = 1; for(;t>0;t/=2){ if(t&1) tmp = multi(tmp,A); A = multi(A,A); } return tmp; } Matri solve(int n,Matri bs){ // bs^n的前n想和 if(n==1) return pow(bs,b); //首项 else { Matri t = solve(n/2,bs); //等比数列前一半的和 Matri dt = pow(bs,1LL*n/2 * k); // if(n%2==0) return add( t,multi(dt,t) ); else return add( add(t,multi(dt,t)),pow(bs,b*1LL+(n-1)*k) ); } } void prit(Matri A){ int i,j; for(i=0;i<N;i++){ for(j=0;j<N;j++){ printf("%d ",A.a[i][j]); } puts(""); } } int main() { int i,j,n,m; Matri bbs,ans,bs; Matri core; for(i=0;i<N;i++){ core.a[i][i] = 1; for(j=0;j<N;j++) bs.a[i][j] = 1; } bs.a[1][1] = 0; while(scanf("%d%d%d%d",&k,&b,&n,&M)!=EOF){ ans = solve(n,bs); printf("%d\n",ans.a[1][0]); } return 0; }
代码二:方法类似,只是计算的方程式不一样,经过合并
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
const int N = 2;
int M,k;
struct Matri{
int a[N][N];
Matri(){
memset(a,0,sizeof(a));
}
};
Matri multi(Matri A,Matri B){
int i,j,k;
Matri tmp;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
for(k=0;k<N;k++)
tmp.a[i][j] = (tmp.a[i][j] + 1LL*A.a[i][k]*B.a[k][j])%M;
}
}
return tmp;
}
Matri add(Matri A,Matri B){
int i,j;
Matri tmp;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
tmp.a[i][j] = (0LL+A.a[i][j]+B.a[i][j])%M;
}
}
return tmp;
}
Matri pow(Matri A,long long t){
Matri tmp;
int i,j;
for(i=0;i<N;i++)
tmp.a[i][i] = 1;
for(;t>0;t/=2){
if(t&1) tmp = multi(tmp,A);
A = multi(A,A);
}
return tmp;
}
Matri solve(int n,Matri bs){ // bs^n的前n想和
if(n==1)
return bs;
else {
// printf("%d",n);
// system("pause");
Matri t = solve(n/2,bs); //等比数列前一半的和
Matri dt = pow (bs,n/2);
if(n%2==0) return add(t,multi(dt,t));
else {
// printf("%d",n);
// system("pause");
return add(add(t,multi(dt,t)),pow(bs,n));
}
// Matri dt = pow(bs,n/2 * k);
// if(n%2==0)
// return add( t,multi(dt,t) );
// else return add( add(t,multi(dt,t)),pow(bs,1+(n-1)*k) );
}
}
void prit(Matri A){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<N;j++){
printf("%d ",A.a[i][j]);
}
puts("");
}
}
int main()
{
int i,j,n,m,b;
Matri bbs,ans,bs;
Matri core,ans1,ans2,ans3;
for(i=0;i<N;i++){
core.a[i][i] = 1;
for(j=0;j<N;j++)
bbs.a[i][j] = 1;
}
bbs.a[1][1] = 0;
while(scanf("%d%d%d%d",&k,&b,&n,&M)!=EOF){
bs = bbs;
if(n==1){
ans = pow(bs,b);
}
else {
Matri tmp = pow(bs,b);
bs = pow(bs,k);
ans = solve(n-1,bs);
//prit(ans);
ans = multi(tmp,add(core,ans));
}
//prit(ans);
printf("%d\n",ans.a[1][0]);
}
return 0;
}
还有更好地思路可以借鉴:
- 就矩阵这个东西来说,完全可以看作一个等比数列,
- 首项是:A^b
- 公比是:A^k
- 项数是:N
- 呵呵,我们可以把问题进一步简化
- 因为矩阵的加法对乘法也符合分配律,我们提出一个A^b来,形成这样的式子:
- A^b*( I + A^k + (A^k)^2 + .... + (A^k)^(N-1) )
- A^b 和 A^k 显然都可以用我们之前说过的方法计算出来,这剩下一部分累加怎么解决呢
- 简单起见,设A^k=B
- 要求 G(N)=I + ... + B^(N-1),设i=N/2
- 若N为偶数,G(N)=G(i)+G(i)*B^i 意思就是假设N=6 那么G(N)=(I+B1+B2)*(I+B3)=I+B1+B2+B3+B4+B5 B1指B的一次方, I是单位阵
- 若N为奇数,G(N)=I+ G(i)*B + G(i) * (B^(i+1)) G(N)=I+(I+B1+B2)*(B1+B4)=I+B1+B2+B3+B4+B5+B6
- 呵呵,这个方法就是比赛当时ACRush用的
- 而农夫用的则是更精妙的方法,大家可想知道
- 我们来设置这样一个矩阵
- B I
- O I
- 其中O是零矩阵,I是单位矩阵
- 将它乘方,得到
- B^2 I+B
- O I
- 乘三方,得到
- B^3 I+B+B^2
- O I
- 乘四方,得到
- B^4 I+B+B^2+B^3
- O I
- 既然已经转换成矩阵的幂了,继续用我们的二分或者二进制法,直接求出幂就可以了
- 斐波那契数列f(n)=f(n-1)+f(n-2),f(0)=1,f(1)=1
- | 1 1 | mat[0][0]相当于f(2) mat[0][1]相当于f(1)
- mat= | 1 0 | mat[1][0]相当于f(1) mat[1][1]相当于f(0)
- | 1 1| | f(2) f(1)|
- mat= |1 0| = | f(1) f(0)|
- | 1 1| ^b | f(b+1) f(b)|
- mat^b =|1 0 | = | f(b) f(b-1)|
- f(b) = matrix[0][1];