HDU1588Gauss Fibonacci(矩陣)
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2076 Accepted Submission(s): 898
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
分析:
列出前n項分別為f(b),f(k+b),f(2*k+b)......f((n-1)*k+b)
結合斐波那契數列
設A為斐波那契數列的遞推矩陣則原式課化為
A^b(E,A^k,A^2K,......A^(n-1)k)*(0,1);
設R=A^k 原式課化為 A^b(E,R^,R^2,......R^(n-1))*(0,1)
P=R,E ,p^n 右上角的子矩陣為 E,R^,R^2,......R^(n-1)
0,E
剩下的事情就很簡單了。
程式碼如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N =4;
int k,b,n,M;
struct matrix
{
long long m[4][4];
};
matrix I={
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
matrix multi(matrix a,matrix b)
{
matrix c;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++){
c.m[i][j]=0;
for(int k=0;k<N;k++)
c.m[i][j]+=(a.m[i][k]%M*b.m[k][j]%M)%M;
c.m[i][j]%=M;
}
return c;
}
matrix pow(matrix A,int k)
{
matrix ans=I,p=A;
while(k){
if(k&1){
ans=multi(ans,p);
k--;
}
k>>=1;
p=multi(p,p);
}
return ans;
}
int main(){
while(cin>>k>>b>>n>>M){
matrix A={
0,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0
};
matrix B=pow(A,b);
matrix R=pow(A,k);
matrix W=R;
W.m[0][2]=1,W.m[1][3]=1,W.m[2][2]=1,W.m[3][3]=1;
matrix Q=pow(W,n);
matrix P;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
P.m[i][j]=0;
P.m[0][0]=Q.m[0][2],P.m[0][1]=Q.m[0][3];
P.m[1][0]=Q.m[1][2],P.m[1][1]=Q.m[1][3];
matrix ans=multi(P,B);
cout<<(ans.m[0][1]+M)%M<<endl;
/*cout<<"*****B*****"<<endl;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<B.m[i][j]<<" ";
cout<<endl;
}
cout<<"*****R*****"<<endl;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<R.m[i][j]<<" ";
cout<<endl;
}
cout<<"*****Q*****"<<endl;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<Q.m[i][j]<<" ";
cout<<endl;
}
cout<<"*****P*****"<<endl;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cout<<P.m[i][j]<<" ";
cout<<endl;
}*/
}
return 0;
}
相關文章
- HDU 3059 Fibonacci數列與矩陣求和 矩陣大小不固定矩陣
- HDU 3117 Fibonacci Numbers(Fibonacci矩陣加速遞推+公式)矩陣公式
- POJ3070 Fibonacci[矩陣乘法]【學習筆記】矩陣筆記
- 生成螺旋矩陣(方陣、矩陣)矩陣
- 鄰接矩陣、度矩陣矩陣
- 巨大的矩陣(矩陣加速)矩陣
- 奇異矩陣,非奇異矩陣,偽逆矩陣矩陣
- 矩陣矩陣
- 資料結構:陣列,稀疏矩陣,矩陣的壓縮。應用:矩陣的轉置,矩陣相乘資料結構陣列矩陣
- 3D圖形:矩陣的行列式,矩陣的逆、正交矩陣、齊次矩陣3D矩陣
- 矩陣中最大的二維矩陣矩陣
- 求任意矩陣的伴隨矩陣矩陣
- 機器學習中的矩陣向量求導(五) 矩陣對矩陣的求導機器學習矩陣求導
- 矩陣和陣列矩陣陣列
- 理解矩陣矩陣
- 矩陣相乘矩陣
- 矩陣分解矩陣
- 稀疏矩陣矩陣
- Numpy 矩陣矩陣
- 穿越矩陣矩陣
- 混淆矩陣矩陣
- 魔方矩陣矩陣
- 海浪矩陣矩陣
- 8.6 矩陣?矩陣
- 螺旋矩陣矩陣
- 找矩陣矩陣
- 矩陣乘法矩陣
- 快手矩陣管理平臺,矩陣管理有方法矩陣
- Wannafly模擬賽 矩陣 二維矩陣hash矩陣
- 矩陣:如何使用矩陣操作進行 PageRank 計算?矩陣
- 演算法學習:矩陣快速冪/矩陣加速演算法矩陣
- 視訊矩陣矩陣
- 高斯消除矩陣矩陣
- 矩陣轉置矩陣
- 矩陣樹定理矩陣
- 矩陣消除遊戲矩陣遊戲
- 08:矩陣加法矩陣
- 旋轉矩陣矩陣