【矩阵乘法】OpenJ_POJ - C17F - A Simple Math Problem

算(7+4*sqrt(3))^n的整数部分(mod 1e9+7)。

容易想到矩乘快速幂,但是怎么算整数部分呢?

(7+4*sqrt(3))^n一定可以写成a+b*sqrt(3),同理(7-4*sqrt(3))^n一定可以写成a-b*sqrt(3),于是,

(7+4*sqrt(3))^n

= (7+4*sqrt(3))^n + (7-4*sqrt(3))^n - (7-4*sqrt(3))^n

= 2*a - (7-4*sqrt(3))^n/*必然小于1*/

所以其整数部分 = 2*a - 1

#include<vector>
#include<cstdio>
using namespace std;
typedef long long ll;
#define MOD 1000000007ll
typedef vector<ll> vec;
typedef vector<vec> mat;
mat I;
mat operator * (const mat &a,const mat &b){
	mat c(a.size(),vec(b[0].size()));
	for(int i=0;i<a.size();++i){
		for(int k=0;k<b.size();++k){
			for(int j=0;j<b[0].size();++j){
				c[i][j]=(c[i][j]+a[i][k]*b[k][j]%MOD)%MOD;
			}
		}
	}
	return c;
}
mat Quick_Pow(mat a,ll p){
	if(!p){
		return I;
	}
	mat res=Quick_Pow(a,p>>1);
	res=res*res;
	if(p&1ll){
		res=res*a;
	}
	return res;
}
int T,n;
int main(){
//	freopen("f.in","r",stdin);
	I.assign(2,vec(2));
	for(int i=0;i<2;++i){
		for(int j=0;j<2;++j){
			if(i==j){
				I[i][j]=1;
			}
			else{
				I[i][j]=0;
			}
		}
	}
	mat A(2,vec(2));
	A[0][0]=7; A[0][1]=12;
	A[1][0]=4; A[1][1]=7;
	mat B(2,vec(1));
	B[0][0]=1;
	B[1][0]=0;
	scanf("%d",&T);
	for(;T;--T){
		scanf("%d",&n);
		printf("%lld\n",((Quick_Pow(A,n)*B)[0][0]*2ll%MOD+MOD-1ll)%MOD);
	}
	return 0;
}
时间: 2024-11-12 09:40:21

【矩阵乘法】OpenJ_POJ - C17F - A Simple Math Problem的相关文章

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

矩阵十题【八】 HDU 1715 A Simple Math Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: If x < 10   ,则  f(x) = x. If x >= 10 ,则  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 给出k,m和a0~a9,求f(k)%m,  k<2*10^9 , m < 10^5 这是一个递推式,故可以用矩阵乘法来求 和上题类似,具体思路过程见上题

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu1757 A Simple Math Problem(矩阵快速幂)

题目: A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f

HDU - 1757 A Simple Math Problem (构造矩阵)

Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); And ai(0<=i<=9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two positive in

hdu1757-- A Simple Math Problem(矩阵快速幂优化)

A Simple Math Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2

hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2791    Accepted Submission(s): 1659 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) =

hdu 1757 A Simple Math Problem 矩阵

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2831    Accepted Submission(s): 1693 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x)

HDU 1757 A Simple Math Problem(矩阵快速幂)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2930    Accepted Submission(s): 1762 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x)