zoukankan      html  css  js  c++  java
  • hdu1588之经典矩阵乘法

    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[n]=A;//A是矩阵,A的某个元素是F[n]
    若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
    ",ans[1][0]); 
    	}
    	return 0;
    }
  • 相关阅读:
    ruby 中的respond_to (转)
    outlook最小化到托盘的设置方法
    FCKeditor 在VS2008下的用法
    暴风影音2009开机启动关闭方法
    构建自己的不可替代性
    MySQL安装1045错解决办法(绝对经典)
    如何学好C++,用好类库很重要
    转:什么是重构
    转:C++堆与栈的区别
    SQL Server 中的索引
  • 原文地址:https://www.cnblogs.com/riskyer/p/3246542.html
Copyright © 2011-2022 走看看