C(HDU-1588矩阵二分快速幂)

C - Gauss Fibonacci
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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
 


又是一道很经典的二分快速幂求矩阵解的题目,主要就是你构造矩阵能不能构造出来,能构造出来。那么你就能够把题目解出来,如果不行的话,那么这道题目,你就一点思路都不会有的。



贴出代码:

/*
构造矩阵:
	   |1  1|      | f(2)    f(1)|
	A= |1  0|   =  | f(1)    f(0)|
		
		  
     |1  1| ^b   | f(b+1)    f(b)|			  
A^b =|1  0|  =   | f(b)    f(b-1)|
				
	f(b) = matrix[0][1]=matrix[1][0];
					  
	首项是:A^b
	公比是:A^k
	项数是:N
	可以把问题进一步简化
	因为矩阵的加法对乘法也符合分配律,我们提出一个A^b来,形成这样的式子:
	A^b*( I + A^k + (A^k)^2 + .... + (A^k)^(N-1) )
	A^b 和 A^k 显然都可以用我们之前说过的方法计算出来,这剩下一部分累加怎么解决呢
	设A^k=B
	要求 G(N)=I + ... + B^(N-1),
	i=N/2
	若N为偶数,G(N)=G(i)+G(i)*B^i = G(i) *( I+B^(i));
	若N为奇数,G(N)=I+ G(i)*B + G(i) * (B^(i+1)) = G(N-1)+B^N; (前一个等式可能要快点,但是后面更简练)
								  
	我们来设置这样一个矩阵
	B I
	O I
	其中O是零矩阵,I是单位矩阵
	将它乘方,得到
	B^2 I+B
	O   I
	乘三方,得到
	B^3 I+B+B^2
	O   I
	乘四方,得到
	B^4  I+B+B^2+B^3
	O    I								  
既然已经转换成矩阵的幂了,继续用我们的二分或者二进制法,直接求出幂就可以了
*/

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;


int K, B, N, M;

struct Matrix
{
	int date[2][2];
	Matrix setE()
	{
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				date[i][j] = (i == j);
			}
		}
	}
	Matrix add(Matrix m)
	{
		Matrix ans;
		memset(ans.date, 0, sizeof(ans.date));
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				ans.date[i][j] = (date[i][j] + m.date[i][j]) % M;
			}
		}
		return ans;
	}
	Matrix mul(Matrix m)
	{
		Matrix e;
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				e.date[i][j] = 0;
				for (int k = 0; k < 2; k++)
				{
					e.date[i][j] += (long long int)date[i][k] * m.date[k][j] % M;
				}
				e.date[i][j] %= M;
			}
		}
		return e;
	}
				
}A;


Matrix quick_pow(Matrix m, int n)
{
	Matrix ans;
	ans.setE();
	while (n)
	{
		if (n & 1)
		{
			ans = ans.mul(m);
		}
		m = m.mul(m);
		n >>= 1;
	}
	return ans;
}

Matrix Binary_search(Matrix m, int n)
{
	if (n == 1)
	{
		return m;
	}
	if (n & 1)
	{
		return Binary_search(m, n - 1).add(quick_pow(m, n));
	}
	else
	{
		Matrix e;
		e.setE();
		return Binary_search(m, n >> 1).mul(quick_pow(m, n >> 1).add(e));
	}
}

int main()
{
	A.date[0][0] = 1;
	A.date[0][1] = 1;
	A.date[1][0] = 1;
	A.date[1][1] = 0;
	while (scanf("%d%d%d%d", &K, &B, &N, &M) != EOF)
	{
		Matrix e;
		e.setE();
		Matrix ans = quick_pow(A, B).mul(e.add(Binary_search(quick_pow(A, K), N - 1)));
		printf("%d\n", ans.date[0][1] % M);
	}
//	system("pause");
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值