HDU 5667 :Sequence

面对Lcomyn提出的特殊数列挑战,本篇详细解析了如何通过构造矩阵和快速幂的方法来高效求解该数列第n项模p的结果。针对给定的数列公式,文章介绍了如何将问题转化为矩阵运算,并特别强调了在指数运算中取模操作的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sequence

 
 Accepts: 59
 
 Submissions: 650
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描写叙述
\ \ \ \    Lcomyn 是个非常厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到了一个数列:

f_n=\left\{\begin{matrix} 1 ,&n=1 \\ a^b,&n=2 \\ a^bf_{n-1}^cf_{n-2},&otherwise \end{matrix}\right.fn=1,ab,abfn1cfn2,n=1n=2otherwise

\ \ \ \    他给了你几个数:nn,aa,bb,cc,你须要告诉他f_nfnpp后的数值.
输入描写叙述
\ \ \ \    第一行一个数T,为測试数据组数.

\ \ \ \    每组数据一行,一行五个正整数,按顺序为nn,aa,bb,cc,pp.

\ \ \ \ 1\le T \le 10,1\le n\le 10^{18}    1T10,1n1018,1\le a,b,c\le 10^91a,b,c109,p是质数且p\le 10^9+7p109+7.
输出描写叙述
\ \ \ \    对每组数据输出一行一个数,输出f_nfnpp取模后的数值.
输入例子
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;
}


HDU1160是一个经典的动态规划问题,题目描述如下: 给定一组老鼠的体重和速度,要求找出最长的老鼠序列,使得序列中每只老鼠的体重和速度都严格递增。 我们可以使用动态规划来解决这个问题。具体步骤如下: 1. 将老鼠按体重或速度排序。 2. 使用动态规划数组`dp`,其中`dp[i]`表示以第`i`只老鼠结尾的最长序列长度。 3. 使用一个数组`pre`来记录每个位置的前驱老鼠,以便最后可以重建序列。 以下是使用Java实现的代码: ```java import java.util.*; class Mouse implements Comparable<Mouse> { int weight, speed, index; Mouse(int weight, int speed, int index) { this.weight = weight; this.speed = speed; this.index = index; } public int compareTo(Mouse other) { if (this.weight != other.weight) { return this.weight - other.weight; } else { return this.speed - other.speed; } } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Mouse> mice = new ArrayList<>(); while (scanner.hasNext()) { int weight = scanner.nextInt(); int speed = scanner.nextInt(); mice.add(new Mouse(weight, speed, mice.size())); } scanner.close(); Collections.sort(mice); int n = mice.size(); int[] dp = new int[n]; int[] pre = new int[n]; int maxLen = 1, endIndex = 0; Arrays.fill(pre, -1); Arrays.fill(dp, 1); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (mice.get(i).weight > mice.get(j).weight && mice.get(i).speed > mice.get(j).speed) { if (dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; pre[i] = j; } } } if (dp[i] > maxLen) { maxLen = dp[i]; endIndex = i; } } List<Integer> sequence = new ArrayList<>(); while (endIndex != -1) { sequence.add(mice.get(endIndex).index + 1); endIndex = pre[endIndex]; } System.out.println(maxLen); for (int i = sequence.size() - 1; i >= 0; i--) { System.out.println(sequence.get(i)); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值