跳到正文

题解

Terrorists 题解

Firsry约 4 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 4 分钟

版本

  1. v1当前

诈骗题。

对每一天枚举点对,O(N⋅m2)O(N \cdot m^2),可以过 Subtask 0\text{Subtask 0},白给的分;

汗水的奖励,如果你被三维偏序骗进去了的话,写 CDQ\text{CDQ} 是可以拿下 O(N⋅mlog⁡m)O(N \cdot m \log m) 的 Subtask 1\text{Subtask 1} 的分的,期望 40 pts\text{40 pts}。

当然,过分容易观察到的是,转移是固定的,所以可以矩阵加速,而且 m≤100m \leq 100,完全没有必要大费周章写一个 CDQ\text{CDQ},m2m^2 轻松跑,总复杂度 m3⋅log⁡Nm^3 \cdot \log N,期望得分 100 pts\text{100 pts}。

C++
#include<bits/stdc++.h>
#define LL long long

using namespace std;

const LL mod = 988244353;
const int MAXM = 105;

struct Man {
	string g;
	int t, c, l;
};
struct Matrix {
	int row, col;
	vector< vector<LL> > mat;
	inline void resize(int x, int y) {
		row = x, col = y;
		mat.assign(row, vector<LL>(col, 0));
		return;
	}
	inline Matrix operator * (const Matrix& a) const {
		Matrix res;
		res.resize(row, a.col);
		for (int i = 0; i < res.row; ++i)
			for (int j = 0; j < res.col; ++j)
				for (int k = 0; k < col; ++k)
					res.mat[i][j] = (res.mat[i][j] + mat[i][k] * a.mat[k][j]) % mod;
		return res;
	}
};

int m, n;
Man man[MAXM];
Matrix vecDP, trans;
LL ans;

inline Matrix Pow(Matrix base, LL expo) {
	Matrix res;
	res.resize(base.row, base.col);
	for (int i = 0; i < res.row; ++i)
		res.mat[i][i] = 1;
	while (expo) {
		if (expo & 1)
			res = res * base;
		base = base * base;
		expo >>= 1;
	}
	return res;
}

int main() {
#ifndef DEBUG
	freopen("terrorists.in", "r", stdin);
	freopen("terrorists.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
#endif
	cin >> m >> n;
	for (int i = 0; i < m; ++i)
		cin >> man[i].g >> man[i].t >> man[i].c >> man[i].l;
	vecDP.resize(1, m);
	for (int i = 0; i < m; ++i)
		vecDP.mat[0][i] = 1;
	trans.resize(m, m);
	for (int i = 0; i < m; ++i)
		for (int j = 0; j < m; ++j) {
			if (man[i].t > man[j].t)	continue;
			if (man[i].c < man[j].c)	continue;
			if (man[i].l > man[j].l && man[i].g == "b")	continue;
			trans.mat[j][i]++;
		}
	trans = Pow(trans, n);
	vecDP = vecDP * trans;
	for (int i = 0; i < m; ++i)
		ans = (ans + vecDP.mat[0][i]) % mod;
	cout << ans;
	return 0;
}
原文