码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj-1588-Gauss Fibonacci【矩阵快速幂】

时间:2015-07-29 19:15:51      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Gauss Fibonacci

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2794 Accepted Submission(s): 1156


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

Author
DYGG

Source

Recommend
linle | We have carefully selected several similar problems for you: 1575 3117 2604 2294 2276

#include<iostream>
#include<stdio.h>
#include<string.h>
struct matrix{
  __int64 m[2][2];//之前定义的 int 类型,结果WA了好多次,以后不能不加思索地看到数据范围:Each of them will not exceed 1,000,000,000.就主观认为。。。<img alt="尴尬" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/awkward.gif" />
}ans;
__int64 M;
matrix e={1,0,0,1};
/*void out(matrix a){
	printf("****\n");
	for(int i=0;i<2;++i){
		for(int j=0;j<2;++j)
		printf("%d ",a.m[i][j]);
		printf("\n");
	}
	printf("*****\n");
}*/
matrix multi(matrix a,matrix b){
	matrix temp;
	for(int i=0;i<2;++i){
		for(int j=0;j<2;++j){
			temp.m[i][j]=0;
			for(int k=0;k<2;++k)
			  temp.m[i][j]=(temp.m[i][j]+a.m[i][k]*b.m[k][j])%M;
		}
	}
	return temp;
}
matrix fast(matrix base,int n){
	ans.m[0][0]=ans.m[1][1]=1;
	ans.m[0][1]=ans.m[1][0]=0;
	while(n){
		if(n&1) ans=multi(ans,base);
		base=multi(base,base);
		n>>=1;
	}
	return ans;
}
matrix add(matrix a,matrix b){
	matrix c;
	memset(c.m,0,sizeof(c.m));
	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 op(matrix a,int k){
	if(k==1) return a;	
	if(k&1) return add(op(a,k-1),fast(a,k));
	return  multi(op(a,k>>1),add(e,fast(a,k>>1)));
}
int main(){
	__int64 k,b,n;
	matrix base;
	base.m[0][0]=base.m[0][1]=base.m[1][0]=1; 
	base.m[1][1]=0;
	while(~scanf("%I64d%I64d%I64d%I64d",&k,&b,&n,&M)){
		matrix x=fast(base,k);  //A^k
		matrix y=fast(base,b);  //A^b
		matrix z=op(x,n-1);   //S(A^[k*(n-1)])
		// Sn: A^(k*0+b) + A^(k*1+b) + A^(k*2+b) +...+ A^(k*(n-1)+b);      
		matrix u=multi(y,add(z,e));
		printf("%I64d\n",u.m[0][1]);
	}
	return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

hdoj-1588-Gauss Fibonacci【矩阵快速幂】

标签:

原文地址:http://blog.csdn.net/qq_18062811/article/details/47131907

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!