hdu 5667


输入样例
1
5 3 3 3 233
输出样例
190

发现f序列就是a的不同指数的形式,所以对每一个f对a取对数。发现就是f[n]=b+c*f[n-1]+f[n-2]。

构造矩阵,快速幂搞。

注意因为是在指数上,所以模的值需要是欧拉函数p,因为p是质数,所以直接是p-1。



代码如下:


#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x333f3f3f
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n))

const ll mod = 100000007;
const int maxn = 5e5 + 5;
const double PI = acos(-1.0);

ll n, a, b, c, p;

struct ma
{
	ll val[4][4];
	ma operator *(const ma &b)
	{
		int i, j, k;
		ma res;
		memset(res.val, 0, sizeof(res.val));

		for (k = 1; k <= 3; k++)
		{
			for (i = 1; i <= 3; i++)
			{
				for (j = 1; j <= 3; j++)
				{
					res.val[i][j] += (this->val[i][k] * b.val[k][j]) % (p - 1);
					res.val[i][j] %= (p - 1);
				}
			}
		}
		return res;
	}
};

ll po(ll x, ll y)
{
	ll res = 1;
	while (y)
	{
		if (y & 1)
			res = res*x%p;
		x = x*x%p;
		y >>= 1;
	}
	return res;
}

ma po_matrix(ma &x, ll y)
{
	ma res;
	res.val[1][1] = 1, res.val[1][2] = 0, res.val[1][3] = 0;
	res.val[2][1] = 0, res.val[2][2] = 1, res.val[2][3] = 0;
	res.val[3][1] = 0, res.val[3][2] = 0, res.val[3][3] = 1;
	while (y)
	{
		if (y & 1)
			res = res*x;
		x = x*x;
		y >>= 1;
	}
	return res;
}

void solve()
{
	ll i, j, k;
	scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &p);
	
	ll res;
	ma r;
	if (n == 1)
	{
		puts("1");
	}
	else if (n == 2)
	{
		res = po(a, b);
		printf("%lld\n", res);
	}
	else
	{
		r.val[1][1] = c, r.val[1][2] = 1, r.val[1][3] = b;
		r.val[2][1] = 1, r.val[2][2] = 0, r.val[2][3] = 0;
		r.val[3][1] = 0, r.val[3][2] = 0, r.val[3][3] = 1;

		r = po_matrix(r, n - 2);
		res = r.val[1][3] + r.val[1][1] * b;
		res = po(a, res);
		printf("%lld\n", res);
	}
}

int main()
{
#ifndef ONLINE_JUDGE  
	freopen("i.txt", "r", stdin);
	freopen("o.txt", "w", stdout);
#endif

	int t;
	scanf("%d", &t);

	while (t--)
	{
		solve();
	}

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值