40分 求助 P1349
  • 板块灌水区
  • 楼主FChang
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/1/25 14:50
  • 上次更新2024/1/25 15:00:39
查看原帖
40分 求助 P1349
744853
FChang楼主2024/1/25 14:50
#include<bits/stdc++.h>
//#define int long long
using namespace std;
long long t,n[10],q,p,nn,m,mod;
long long ans;
struct node{
	int a;
	int b;
	long long s[5][5];
	void init(int x,int y) {
		a=x;
		b=y;
		for(int i=0;i<=a;i++)
			for(int j=0;j<=b;j++)
				s[i][j] = 0;
		return ;
	}
};
node mul(node a,node b) {
	node c;
	c.init(a.a,b.b);
	for(int i=1;i<=a.a;++i)
		for(int j=1;j<=b.b;++j)
			for(int k=1;k<=a.b;++k)
				c.s[i][j]=(c.s[i][j]+(a.s[i][k]*b.s[k][j])%mod)%mod;
	return c;
}
void quick_pow(int m) {
	node a,b,c;
	a.init(2,2);
	b.init(2,2);
	c.init(1,2);
	c.s[1][1]=n[2] % mod;
	c.s[1][2]=n[1] % mod;
	b.s[1][1] = p % mod;
	b.s[1][2] = q % mod;
	b.s[2][1] = 1;
	a.s[1][1] = a.s[2][2] = 1;
	while(m) {
		if(m&1) a = mul(a,b);
		b = mul(b,b);
		m>>=1;
	}
	c = mul(c,a);
	ans = c.s[1][1] % mod;
	return ;
}

signed main() {
	cin>>p>>q;
	cin>>n[1]>>n[2]>>nn>>mod;
	if(nn<=2) {
		cout<<n[nn]%mod<<"\n";
		return 0;
	}
	quick_pow(nn-2);
	cout<<ans;
	return 0;
}
2024/1/25 14:50
加载中...