E. Product Oriented Recurrence(四个矩阵快速幂)

http://codeforces.com/contest/1182/problem/E

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let fx=c2x−6⋅fx−1⋅fx−2⋅fx−3fx=c2x−6⋅fx−1⋅fx−2⋅fx−3 for x≥4x≥4.

You have given integers nn, f1f1, f2f2, f3f3, and cc. Find fnmod(109+7)fnmod(109+7).

Input

The only line contains five integers nn, f1f1, f2f2, f3f3, and cc (4≤n≤10184≤n≤1018, 1≤f11≤f1, f2f2, f3f3, c≤109c≤109).

Output

Print fnmod(109+7)fnmod(109+7).

Examples

input

Copy

5 1 2 5 3

output

Copy

72900

input

Copy

17 97 41 37 11

output

Copy

317451037

Note

In the first example, f4=90f4=90, f5=72900f5=72900.

In the second example, f17≈2.28×1029587f17≈2.28×1029587.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=3;
ll mod,mod1;
ll powmod(ll a,ll b)
{
	ll res=1;
	for(;b;b>>=1)
	{
		if(b&1) res=res*a%mod1;
		a=a*a%mod1;
	}
	return res;
}
struct Matrix
{
	ll mat[MAXN][MAXN];
	Matrix() {}
	Matrix operator*(Matrix const &b)const
	{
		Matrix res;
		memset(res.mat, 0, sizeof(res.mat));
		for (int i = 0 ;i < MAXN; i++)
		for (int j = 0; j < MAXN; j++)
		for (int k = 0; k < MAXN; k++)
			res.mat[i][j] = (res.mat[i][j]+this->mat[i][k] * b.mat[k][j]%mod)%mod;
		return res;
	}
};
struct Matrix0 
{
	ll mat[5][5];
	Matrix0() {}
	Matrix0 operator*(Matrix0 const &b)const
	{
		Matrix0 res;
		memset(res.mat, 0, sizeof(res.mat));
		for (int i = 0 ;i < 5; i++)
		for (int j = 0; j < 5; j++)
		for (int k = 0; k < 5; k++)
			res.mat[i][j] = (res.mat[i][j]+this->mat[i][k] * b.mat[k][j]%mod+mod)%mod;
		return res;
	}
};
Matrix pow_mod(Matrix base, ll n)
{
	Matrix res;
	memset(res.mat, 0, sizeof(res.mat));
	for (int i = 0; i < MAXN; i++)
		res.mat[i][i] = 1;
	while (n > 0)
	{
		if (n & 1) res = res*base;
		base = base*base;
		//printf("res.mat:%lld\n",res.mat[0][0]);
		n >>= 1;
	}
	return res;
}
Matrix0 pow_mod0(Matrix0 base, ll n)
{
	Matrix0 res;
	memset(res.mat, 0, sizeof(res.mat));
	for (int i = 0; i < 5; i++)
		res.mat[i][i] = 1;
	while (n > 0)
	{
		if (n & 1) res = res*base;
		base = base*base;
		n >>= 1;
	}
	return res;
}
Matrix base,fi1,fi2,fi3;
Matrix0 fi0,base0;
ll p0,p1,p2,p3,res,n,f1,f2,f3,c;
int main()
{
	
	/*base0.mat={1,1,1,2,-4,
				1,0,0,0,0,
				0,1,0,0,0,
				0,0,0,1,1,
				0,0,0,0,1};*/
				base0.mat[0][0]=base0.mat[0][1]=base0.mat[0][2]=base0.mat[1][0]=1;
				base0.mat[2][1]=base0.mat[3][4]=base0.mat[4][4]=base0.mat[3][3]=1;
				
				base0.mat[0][3]=2,base0.mat[0][4]=-4;
	mod=1e9+6;
	mod1=1e9+7;
 	cin>>n>>f1>>f2>>f3>>c;
 	/*
 	67025 50601 5471 94645 97571
 	*/
 	//cin>>n;
 	if(n<6)
 	{
 		if(n==4)	p0=2,p1=1,p2=1,p3=1;
		else	p0=6,p1=1,p2=2,p3=2;
		res=powmod(c,p0)%mod1*powmod(f1,p1)%mod*powmod(f2,p2)%mod1*powmod(f3,p3)%mod1;
		printf("%lld\n",res);
 		return 0;
	}
 	//cin>>n>>f1>>f2>>f3>>c;
	base.mat[0][0] = 1;base.mat[0][1] = 1;base.mat[0][2] = 1;  
	base.mat[1][0] = 1; 
	base.mat[2][1] = 1;
	
	fi0.mat[0][0]=14;
	fi0.mat[1][0]=6;
	fi0.mat[2][0]=2;
	fi0.mat[3][0]=6;
	fi0.mat[4][0]=1;
	
	fi1.mat[0][0]=2;
	fi1.mat[1][0]=1;
	fi1.mat[2][0]=1;
	
	fi2.mat[0][0]=3;
	fi2.mat[1][0]=2;
	fi2.mat[2][0]=1;
	
	fi3.mat[0][0]=4;
	fi3.mat[1][0]=2;
	fi3.mat[2][0]=1;
	
	Matrix ans = pow_mod(base, n-6);
	Matrix a1,a2,a3;
	Matrix0 a0;
	Matrix0 ans0=pow_mod0(base0,n-6);
	a0=ans0*fi0;
	//for(int i=0;i<5;++i) printf("%d\n",a0.mat[i][0]);
	a1=ans*fi1;
	a2=ans*fi2;
	a3=ans*fi3;
	
	p0=a0.mat[0][0]%mod,p1=a1.mat[0][0]%mod,p2=a2.mat[0][0]%mod,p3=a3.mat[0][0]%mod;
	f1%=mod1;
	f2%=mod1;
	f3%=mod1;
	c%=mod1;
	//printf("%lld %lld %lld %lld\n", p0,p1,p2,p3);
	res=1;
	res=res*powmod(f3,p3)%mod1;
	//printf("f3:%lld p3:%lld res:%lld\n",f3,p3,res);
	res=res*powmod(f1,p1)%mod1;
	res=res*powmod(f2,p2)%mod1;
	//printf("******%lld %lld\n",c,p0);
	res=res*powmod(c,p0)%mod1;
	
	printf("%lld\n",res%mod1);
	return 0;
}
/*
67025 50601 5471 94645 97571
*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ccsu_deer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值