E. Product Oriented Recurrence(矩阵快速幂+欧拉降幂)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define maxn 300005
#define ll long long
const int mod = 1e9+7;
const int modd = 1e9+6;       //欧拉降幂

ll n,f1,f2,f3,c;
int b[10][10];
struct Mat{                  //矩阵快速幂
	ll m[10][10];
	Mat(){
		memset(m,0,sizeof(m));
	}
	inline void build(int len){
		for(int i=1;i<=len;i++)m[i][i]=1;
	}
}a;

Mat Mul(Mat x,Mat y,int len){
	Mat c;
	for(int i=1;i<=len;i++)
		for(int j=1;j<=len;j++)
			for(int k=1;k<=len;k++)
				c.m[j][i]=(c.m[j][i]+x.m[j][k]*y.m[k][i]%modd)%modd;     //欧拉降幂
	return c;
}

Mat poww(Mat x,ll y,int len){
	Mat aa;aa.build(len);
	while(y){
		if(y&1)aa=Mul(aa,x,len);
		x=Mul(x,x,len);
		y>>=1;
	}
	return aa;
}

ll poww2(ll x,ll y){
	ll ans=1;
	while(y){
		if(y&1)ans=(ans*x)%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ans%mod;
}

ll wow(Mat s,int k){
	if(k==1)return s.m[3][1];
	else if(k==2)return s.m[2][1];
	else return s.m[1][1];
}

int main()
{
	cin>>n>>f1>>f2>>f3>>c;
	a.m[1][1]=1,a.m[2][1]=1,a.m[3][1]=1;    //构建矩阵
	a.m[1][2]=1,a.m[2][2]=0,a.m[3][2]=0;
	a.m[1][3]=0,a.m[2][3]=1,a.m[3][3]=0;
	Mat ans1=poww(a,n-3,3);
	ll ans11=wow(ans1,1),ans12=wow(ans1,2),ans13=wow(ans1,3);
	a.m[4][1]=1;a.m[5][1]=0;              //构建矩阵2
	a.m[4][2]=0;a.m[5][2]=0;
	a.m[4][3]=0;a.m[5][3]=0;
	a.m[4][4]=1;a.m[5][4]=1;
	a.m[4][5]=0;a.m[5][5]=1;
	Mat ans2=poww(a,n-3,5);
	ll ans22=2*ans2.m[4][1]%modd+2*ans2.m[5][1]%modd;  //欧拉降幂
	ans22%=modd;                                       //欧拉降幂
	printf("%lld",poww2(f1,ans11)*poww2(f2,ans12)%mod*poww2(f3,ans13)%mod*poww2(c,ans22)%mod);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To solve this recurrence relation, we can use the Master Theorem. The recurrence relation can be written in the form: T(n) = aT(n/b) + f(n) where a = 2, b = 2, and f(n) = f(n). We can use the following steps to apply the Master Theorem: 1. Calculate the value of logb(a): log2(2) = 1 2. Compare f(n) with nlogb(a): If f(n) = O(nlogb(a)-ε) for some ε > 0, then T(n) = Θ(nlogb(a)). If f(n) = Θ(nlogb(a)), then T(n) = Θ(nlogb(a) log n). If f(n) = Ω(nlogb(a)+ε) for some ε > 0, and if af(n/b) ≤ cf(n) for some constant c < 1 and sufficiently large n, then T(n) = Θ(f(n)). In this case, we have f(n) = f(n), which is not comparable to nlogb(a) = n. Therefore, we cannot use the Master Theorem to determine the asymptotic growth rate of T(n). However, we can still solve the recurrence relation by using the substitution method. Let T(n) = 2T(n/2) + f(n) Assume that T(n) = O(nlog n). Then, we have: T(n) ≤ 2T(n/2) + f(n) ≤ 2(c(n/2)log(n/2)) + f(n) = cnlogn - cn + f(n) Since f(n) is a non-negative function, we can further simplify: T(n) ≤ cnlogn + f(n) Therefore, T(n) = O(nlogn) is a valid upper bound. Now, assume that T(n) = Ω(nlogn). Then, we have: T(n) ≥ 2T(n/2) + f(n) ≥ 2(c(n/2)log(n/2)) + f(n) = cnlogn - cn + f(n) Since f(n) is a non-negative function, we can further simplify: T(n) ≥ cnlogn + f(n) Therefore, T(n) = Ω(nlogn) is a valid lower bound. Combining the upper and lower bounds, we get: T(n) = Θ(nlogn) Therefore, the solution to the recurrence relation is T(n) = Θ(nlogn).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值