hdu1588之经典矩阵乘法
2013-08-08 20:30
260 查看
Gauss Fibonacci
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1706 Accepted Submission(s): 741
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.
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.
Output
For each line input, out the value described above.
Sample Input
2 1 4 100
2 0 4 100
Sample Output
21
12
题目要求求出f(g(i))的总和,i是0~n-1
代码中详细思路+注释
/*f(g(i))=f(k*i+b) 令f =A;//A是矩阵,A的某个元素是F 若i=0~n-1,则sum(f(k*i+b)) =A^b+A^(k+b)+A^(2k+b)+A^(3k+b)+...+A^((n-1)k+b) =A^b+A^b(A^k+A^2k+A^3k+A^4k+...+A^(n-1)k) 将A^k看成一个新的矩阵B,则原式: =A^b+A^b(B^1+B^2+B^3+...+B^(n-1));//A^b,A^k用矩阵快速幂求出,括号中的用二分矩阵可求 所谓二分矩阵:A^1+A^2+A^3+A^4+A^5+A^6=(A^1+A^2+A^3)+A^3(A^1+A^2+A^3) */ #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<iomanip> #define INF 99999999 using namespace std; const int MAX=2; __int64 array[MAX][MAX],sum[MAX][MAX],temp[MAX][MAX],ans[MAX][MAX]; //array相当于A,sum记录每次幂乘后的矩阵,temp是临时矩阵,ans是B^1+B^2+B^3+...+B^n void MatrixInit(__int64 a[MAX][MAX],bool flag){//初始化矩阵 for(int i=0;i<MAX;++i){ for(int j=0;j<MAX;++j){ if(flag)a[i][j]=array[i][j];//a=A else a[i][j]=(i == j);//a=1 } } } void MatrixAdd(__int64 a[MAX][MAX],__int64 b[MAX][MAX],int &mod){//矩阵相加 for(int i=0;i<MAX;++i){//a=a+b for(int j=0;j<MAX;++j){ a[i][j]=(a[i][j]+b[i][j])%mod; } } } void MatrixMult(__int64 a[MAX][MAX],__int64 b[MAX][MAX],int &mod){//矩阵相乘 __int64 c[MAX][MAX]={0}; for(int i=0;i<MAX;++i){//a=a*b for(int j=0;j<MAX;++j){ for(int k=0;k<MAX;++k){ c[i][j]+=a[i][k]*b[k][j]; } } } for(int i=0;i<MAX;++i){ for(int j=0;j<MAX;++j)a[i][j]=c[i][j]%mod; } } void MatrixPow(int k,int &mod){//矩阵幂乘,sum=A^k MatrixInit(sum,0);//sum=1 MatrixInit(temp,1);//temp=A while(k){ if(k&1)MatrixMult(sum,temp,mod); MatrixMult(temp,temp,mod); k>>=1; } } void MatrixSum(int k,int &mod){//矩阵求和 if(k == 1){MatrixInit(ans,1);return;} MatrixSum(k/2,mod); MatrixPow((k+1)/2,mod); if(k&1){//k为奇数则A+(A+A^m)*(A+A^2+A^3...),m=(k+1)/2 MatrixInit(temp,1);//temp=A MatrixAdd(sum,temp,mod);//sum=A+A^m MatrixMult(ans,sum,mod);//ans=sum*ans MatrixAdd(ans,temp,mod);//ans=A+ans } else{//k为偶数则(1+A^m)*(A+A^2+A^3...),m=(k+1)/2 MatrixInit(temp,0);//temp=1 MatrixAdd(temp,sum,mod);//temp=1+A^m MatrixMult(ans,temp,mod);//ans=ans*temp; } } int main(){ int k,b,n,m; while(scanf("%d%d%d%d",&k,&b,&n,&m)!=EOF){ array[0][0]=array[0][1]=array[1][0]=1; array[1][1]=0; MatrixPow(k,m);//求A^k MatrixInit(array,0); MatrixMult(array,sum,m);//将array构造成B,即A^k MatrixSum(n-1,m);//求矩阵和 array[0][0]=array[0][1]=array[1][0]=1; array[1][1]=0; MatrixPow(b,m);//求A^b; MatrixMult(ans,sum,m);//求A^b*ans MatrixAdd(ans,sum,m);//求A^b+A^b+ans printf("%I64d\n",ans[1][0]); } return 0; }
相关文章推荐
- hdu_1588_矩阵_求和_乘法_快速幂_斐波那契数列求前n项和
- hdu 1588 Gauss Fibonacci(矩阵乘法,二分)
- hdu 1588 Gauss Fibonacci(矩阵乘法)
- hdu 1588 Gauss Fibonacci(矩阵乘法+二分)
- hdu 2243 poj 2778 AC自动机 + 经典矩阵乘法
- hdu 2157 How many ways??(矩阵乘法经典应用)
- 十个利用矩阵乘法解决的经典题目
- C(HDU-1588矩阵二分快速幂)
- Fast Matrix Calculation HDU - 4965 优化矩阵乘法
- [caioj 1479,利用矩阵乘法解决的经典题目一]点的变换
- hdu2802之矩阵乘法
- HDU 1588 斐波那契数列数列变形和矩阵连乘
- 【矩阵快速幂】HDU 2157 How many ways??(矩阵快速幂经典问题)
- hdu 1588 Gauss Fibonacci(函数嵌套、转换、等比矩阵求和)
- 十个利用矩阵乘法解决的经典题目(转Matrix67)
- HDU 1575(数论,矩阵乘法+求幂)
- 十个利用矩阵乘法解决的经典题目
- HDU 2232 机器人的舞蹈 矩阵乘法
- HDU 3802 Ipad,IPhone 数论 矩阵乘法
- hdu 1588(矩阵好题+递归求解等比数列)