首页 > 其他分享 >LY1162 [ 20230323 CQYC省选模拟赛 T3 ] 跳!跳!跳!

LY1162 [ 20230323 CQYC省选模拟赛 T3 ] 跳!跳!跳!

时间:2024-03-06 09:11:06浏览次数:39  
标签:20230323 省选 LY1162 int rg bool array include define

题意

给定 \(n\) 个长度为 \(m\) 的字符串,进行若干操作,求每个字符串 \(S_a\) 到 \(S_b\) 的方案数。

另外,你还有一个模式串 \(T\),由 \({1, ..., n}\) 与 \(0\)(通配符) 组成。

  • 从 \(S_x\) 右边的串开始,不断向右移动,直到 \(S_y\) 与 \(T\) 匹配。
  • 从 \(S_x\) 左边的串开始,不断向左移动,直到 \(S_y\) 与 \(T\) 匹配。
  • 将 \(T\) 任意一个字符为 \(0\) 的位置改为 \({1, ..., n}\)。从当前串开始,不断往右移动,直到 \(S_y\) 与 \(T\) 匹配。
  • 将 \(T\) 任意一个字符改为 \(0\),不移动。

\(m \le 5, n \le 500\)

Sol

trivial 地,考虑如何用一个状态表示当前的 \(T\)。

不难发现当前 \(T\) 一定和 \(S_x\) 匹配,那么我们只需要状压记录当前 \(T\) 哪些位置是 \(0\) 就行了。

考虑对于每个起点跑 \(bfs\)。

前两个操作可以简单预处理,而第三个操作需要 \(n \times m\) 的复杂度进行转移。

总复杂度为 \(n ^ 3 2 ^ m m\)。

直接卡常草过去。

Code

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#include <vector>
#include <bitset>
/* #define int long long */
#define pii pair <int, int>
#define il inline
#define rg register
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
il int read() {
	rg int p = 0, flg = 1;
	rg char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
bool _stmer;

#define fi first
#define se second

const int N = 505, M = 6, K = 1 << 5;

array <array <int, M>, N> s;

array <array <int, K>, N> pre, suf;

il bool check(int T, int m, int x, int y){
	rg bool flg = 1;
	for (rg int k = 1; k <= m; k++)
		if (!(T & (1 << (k - 1))))
			flg &= (s[x][k] == s[y][k]);
	return flg;
}

array <array <array <vector <int>, M>, K>, N> isl;

array <array <int, K>, N> dis;

queue <pii> q;

il void bfs(int st, int n, int m) {
	for (int i = 1; i <= n; i++)
		dis[i].fill(-1);
	for (int i = 0; i < 1 << m; i++)
		q.push(make_pair(st, i)), dis[st][i] = 0;
	while (!q.empty()) {
		rg pii u = q.front();
		q.pop();
		if (!~dis[pre[u.fi][u.se]][u.se])
			dis[pre[u.fi][u.se]][u.se] = dis[u.fi][u.se] + 1, q.push(make_pair(pre[u.fi][u.se], u.se));
		if (!~dis[suf[u.fi][u.se]][u.se])
			dis[suf[u.fi][u.se]][u.se] = dis[u.fi][u.se] + 1, q.push(make_pair(suf[u.fi][u.se], u.se));
		for (rg int i = 1; i <= m; i++) {
			int tp = u.se ^ (1 << (i - 1));
			if (!~dis[u.fi][tp])
				dis[u.fi][tp] = dis[u.fi][u.se] + 1, q.push(make_pair(u.fi, tp)), tot++;
			if (u.se & (1 << (i - 1))) continue;
			for (auto t : isl[u.fi][u.se][i]) {
				if (~dis[t][tp]) continue;
				dis[t][tp] = dis[u.fi][u.se] + 1;
				q.push(make_pair(t, tp));
			}
		}
	}
}

array <array <int, N>, N> ans;
array <bitset <N>, M> vis;

bool _edmer;
int main() {
	cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
	/* freopen("string.in", "r", stdin); */
	/* freopen("string.out", "w", stdout); */
	rg int n = read(), m = read();
	for (rg int i = 1; i <= n; i++)
		for (rg int j = 1; j <= m; j++)
			s[i][j] = read();
	for (rg int i = 1; i <= n; i++) {
		for (rg int T = 0; T < 1 << m; T++) {
			for (rg int j = 1; j < n; j++) {
				if (check(T, m, i, (i - j + n - 1) % n + 1)) {
					pre[i][T] = (i - j + n - 1) % n + 1;
					break;
				}
			}
			for (rg int j = 1; j < n; j++) {
				if (check(T, m, i, (i + j - 1) % n + 1)) {
					suf[i][T] = (i + j - 1) % n + 1;
					break;
				}
			}
			for (rg int j = 1; j <= m; j++)
				vis[j] = 0, vis[j][s[i][j]] = 1;
			for (rg int j = 1; j < n; j++) {
				rg int t = (i + j - 1) % n + 1;
				for (rg int k = 1; k <= m; k++)
					if (check(T, m, i, t) && (T & (1 << (k - 1))) && !vis[k][s[t][k]])
						isl[t][T ^ (1 << (k - 1))][k].push_back(i), vis[k][s[t][k]] = 1;
			}
		}
	}
	for (rg int i = 1; i <= n; i++) {
		bfs(i, n, m);
		for (rg int j = 1; j <= n; j++)
			ans[j][i] = dis[j][(1 << m) - 1];
	}
	for (rg int i = 1; i <= n; i++) {
		for (rg int j = 1; j <= n; j++)
			write(ans[i][j]), putchar(32);
		puts("");
	}
	return 0;
}

标签:20230323,省选,LY1162,int,rg,bool,array,include,define
From: https://www.cnblogs.com/cxqghzj/p/18055749

相关文章

  • 联合省选 2024 游记
    Day0酒店很好,午餐和晚餐都很好。试机,发现不会配置sublime,因为不会配置g++。晚上奔波1km去吃M记。Day1配置sublime长达7min。先看T1,大概花了40min,想出做法,具体是对每天独立分析,一次函数拆绝对值后二分零点。写完大概1.5h,去看T2,先打了暴力12pts.看T3,指数塔......
  • 联合省选 2024 游记
    Day0考前一天本来放假的,但是父母报了来校自习,被迫来到学校,但是机房又被占了,跑到二楼古董机房自习,体会了一下\(\times6\)常数的缓慢感。自习的时候一直在看数据结构、多项式和数学之类的东西,然后省选一个没考。Day1早上起来,感到了已经很久没有过了的睡饱了的十分清醒的感觉......
  • 联合省选2024游记
    day-???THUWC和NOIWC都结束了,一个2=一个Cu,太失败了。面基了HE的其他几个oier,大家都好厉害。回家摆烂,跟上了NFLS的模拟赛,天天被吊打/jk在省选前三周下载米哈游最新力作崩坏星穹铁道然后愤怒开玩,两周过完了主线返校了。day-2教练问高一选手有没有想去体验一下省选的,竟然还可......
  • 题解 P10220【[省选联考 2024] 迷宫守卫】
    \(\text{Link}\)葬送了我2024省选的一题。题意有一颗深度为\(n+1\)的完全二叉树,其叶子上依次标有一个长为\(2^n\)排列\(a\),非叶结点有选择代价\(b_i\)。Alice、Bob两人进行游戏。Alice可以选择一些选择代价和不超过\(m\)的非叶结点,此后Bob会从根开始深度优先搜索......
  • 2023 NOIP + 2024 陕西省选 游记
    前言:陕西省选\(1\)月就考完了,而联合省选要等到\(3\)月。现在写这篇文章的时间正好是\(2024.3.5\),联合省选结束后第一天。2023.11.1xmd怎么还不让我去体验NOIP,是不是看不起人。几天后:好的CCF最牛逼。2023.11.18考NOIP力。人员变化不大,基本上都来了。又是周六,继......
  • 联合省选 2024 游记
    Day-2打了一场CFdiv.2,很平常地切了4题结果一看排行居然排到了26名?省选信心赛!第二天上紫名了,洛谷个签可以改了()Day0上午狠狠地学习了线段树优化建图,过了板子题。然后还想复习一下整体二分,于是找到了P4602[CTSC2018]混合果汁打算写一下。然而下午直接pvz启动,什......
  • [省选联考 2024] 题解
    D1T1P10217季风题意有点抽象,大概就是要求我们对两个有若干次重复的序列进行操作,每次可以将这两个序列都向上或向下调整一个值,但是调整的绝对值的总和有限制,问能否最终将总和调整至固定值。由于\(m\)不一定是\(n\)的倍数,因此序列在重复若干次之后可能会遗留一些散块,这是不......
  • P10217 [省选联考 2024] 季风 题解
    [省选联考2024]季风Description给定\(n,k,x,y\)和\(2n\)个整数\(x_0,y_0,x_1,y_1,\dots,x_{n-1},y_{n-1}\)。找到最小的非负整数\(m\),使得存在\(2m\)个实数\(x_0',y_0',x_1',y_1',\dots,x_{m-1}',y_{m-1}'\)满足以下条件,或报告不存在这样的\(m\):\(\s......
  • 联合省选 2024 游记
    2024-02-26(DAY-5)终于收到通知能去联合省选颓废了!2024-02-27(DAY-4)早上翘课打FSB的模拟赛,写了个比赛记录2024-02-27省选模拟赛。2024-03-02(DAY0)省选第一天,早上6:40起床,吃了早饭和好多NB巨佬去考场,考场楼下又面到XHGua了。到考场刚好7:45,准考证上说要......
  • 2024联合省选 游记
    \(\texttt{Day-1}\)下午最后一场省选模拟赛,直接开始摆烂模式,真的让人非常放松。被指隐藏实力。倒数第二天了,\(6\)个参加省选的同学除了我全部都提前上机房了,而我一个人悠哉游哉的坐在教室做文化课作业。被力老师看见了。她问我,你就这么自信吗,你就觉得你随便考就能考过他们......