还是不怎么懂,,,,,,
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2217 Accepted Submission(s): 943
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.
Each of them will not exceed 1,000,000,000.
2 1 4 100 2 0 4 100
2112
/******************************************************************************************* 首先我们看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)) 需要注意的一点是:当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) 用递归写 ***************************************************************************************/
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<iostream> using namespace std; long long k, b, n, M; struct Matrix { long long m[2][2]; }B, E; void init() { B.m[0][0] = B.m[0][1] = B.m[1][0] = 1; B.m[1][1] = 0; E.m[0][0] = E.m[1][1] = 1; E.m[0][1] = E.m[1][0] = 0; } Matrix multi(Matrix a, Matrix b) { Matrix c; for(int i =0; i<2; i++){ for(int j = 0; j<2; j++){ c.m[i][j] = 0; for(int t = 0; t<2; t++){ c.m[i][j] = c.m[i][j] + a.m[i][t]*b.m[t][j]%M; } c.m[i][j] %= M; } } return c; } Matrix Power(Matrix A, int k) { Matrix ans = E; while(k){ if(k & 1) ans = multi(ans, A); k /= 2; A = multi(A, A); } return ans; } Matrix add(Matrix a, Matrix b) { Matrix c; for(int i = 0; i<2; i++){ for(int j = 0; j<2; j++){ c.m[i][j] = (a.m[i][j]+b.m[i][j])%M; } } return c; } Matrix Sum(Matrix a, long long n) { Matrix ans; if(n == 0) { ans.m[0][0] = ans.m[0][1] = ans.m[1][0] = ans.m[1][1] = 0; return ans; } if(n == 1) return a; else// 这部分还不怎么懂 { Matrix res, t; t = Sum(a, n/2); res = add(t, multi(Power(a, n/2), t)); if(n & 1) res = add(res, Power(a, n)); return res; } } int main() { while(~scanf("%I64d%I64d%I64d%I64d", &k, &b, &n, &M)){ init(); Matrix M1 = Power(B, k); Matrix M2 = Power(B, b); Matrix res = multi(M2, add(E, Sum(M1, n-1))); printf("%I64d\n", res.m[0][1]); } return 0; }