CF1178F1 Short Colorful Strip 题解

news2025/4/25 8:48:32

Short Colorful Strip

传送门

题面翻译

题目描述

这是F题的第一个子任务。F1和F2的区别仅在对于m和时间的限制上

有n+1种颜色标号从0到n,我们有一条全部染成颜色0的长为m的纸带。

Alice拿着刷子通过以下的过程来给纸带染色:

我们按照从1到n的顺序进行染色,进行每次染色时,我们选取一个区间[ai,bi],0<=ai<bi<=m,并且这个区间内必定是单种颜色。

染色到最后,纸带上有各种颜色除了颜色0.给出纸带最终的状态,问有多少种不同的染色方案能到达最终状态。输出时结果模998244353。

输入格式

第一行有两个整数n和m,1<=n<=500,并且保证m=n。n代表除了颜色0有多少种颜色,m代表纸带的长度。

第二行有m个用空格分隔的整数c1,c2,…,cm,(1<=ci<=n),代表完成染色后纸带的最终状态。保证最终状态中能在纸带上找到从1到n所有的颜色。

注意,这个子任务保证了n=m,那么就是说最终状态是1到n的一个排列。

输出格式

输入一个单独的整数,即Alice可行的染色方案数模998244353.

题目描述

This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of m m m and the time limit. You need to solve both subtasks in order to hack this one.

There are n + 1 n+1 n+1 distinct colours in the universe, numbered 0 0 0 through n n n . There is a strip of paper m m m centimetres long initially painted with colour 0 0 0 .

Alice took a brush and painted the strip using the following process. For each i i i from 1 1 1 to n n n , in this order, she picks two integers 0 ≤ a i < b i ≤ m 0 \leq a_i < b_i \leq m 0ai<bim , such that the segment [ a i , b i ] [a_i, b_i] [ai,bi] is currently painted with a single colour, and repaints it with colour i i i .

Alice chose the segments in such a way that each centimetre is now painted in some colour other than 0 0 0 . Formally, the segment [ i − 1 , i ] [i-1, i] [i1,i] is painted with colour c i c_i ci ( c i ≠ 0 c_i \neq 0 ci=0 ). Every colour other than 0 0 0 is visible on the strip.

Count the number of different pairs of sequences { a i } i = 1 n \{a_i\}_{i=1}^n {ai}i=1n , { b i } i = 1 n \{b_i\}_{i=1}^n {bi}i=1n that result in this configuration.

Since this number may be large, output it modulo 998244353 998244353 998244353 .

输入格式

The first line contains a two integers n n n , m m m ( 1 ≤ n ≤ 500 1 \leq n \leq 500 1n500 , n = m n = m n=m ) — the number of colours excluding the colour $ 0 $ and the length of the paper, respectively.

The second line contains m m m space separated integers c 1 , c 2 , … , c m c_1, c_2, \ldots, c_m c1,c2,,cm ( 1 ≤ c i ≤ n 1 \leq c_i \leq n 1cin ) — the colour visible on the segment [ i − 1 , i ] [i-1, i] [i1,i] after the process ends. It is guaranteed that for all j j j between 1 1 1 and n n n there is an index k k k such that c k = j c_k = j ck=j .

Note that since in this subtask n = m n = m n=m , this means that c c c is a permutation of integers 1 1 1 through n n n .

输出格式

Output a single integer — the number of ways Alice can perform the painting, modulo $ 998244353 $ .

样例 #1

样例输入 #1

3 3
1 2 3

样例输出 #1

5

样例 #2

样例输入 #2

7 7
4 5 1 6 2 3 7

样例输出 #2

165

提示

In the first example, there are 5 5 5 ways, all depicted in the figure below. Here, 0 0 0 is white, 1 1 1 is red, 2 2 2 is green and 3 3 3 is blue.

Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour 2 2 2 .

解题思路

有题目描述可知, n = m n=m n=m,所以每个颜色只能涂一次。又可知,颜色要从编号小的到编号大的涂,所以可以用搜索算法,每次找出带子上颜色编号最小的位置作为基准点,算出 d f s ( l , m i n i d − 1 ) dfs(l,minid-1) dfs(l,minid1) d f s ( m i n i d + 1 , r ) dfs(minid+1,r) dfs(minid+1,r)。而题目要求求涂色方案数,所以可以用乘法原理,算出 d f s ( l , m i n i d − 1 ) = d f s ( l , x − 1 ) × d f s ( x , m i n i d − 1 ) dfs(l,minid-1)=dfs(l,x-1) \times dfs(x,minid-1) dfs(l,minid1)=dfs(l,x1)×dfs(x,minid1) d f s ( m i d i d + 1 , r ) = d f s ( m i n i d , x ) × d f s ( x + 1 , r ) dfs(midid+1,r)=dfs(minid,x) \times dfs(x+1,r) dfs(midid+1,r)=dfs(minid,x)×dfs(x+1,r),因而 d f s ( l , r ) = d f s ( l , m i n i d − 1 ) × d f s ( m i d i d + 1 , r ) = d f s ( m i n i d , x ) × d f s ( x + 1 , r ) dfs(l,r)=dfs(l,minid-1) \times dfs(midid+1,r)=dfs(minid,x) \times dfs(x+1,r) dfs(l,r)=dfs(l,minid1)×dfs(midid+1,r)=dfs(minid,x)×dfs(x+1,r)

为了避免做重复涂色操作,所以要记忆化,用 t o n g l , r tong_{l,r} tongl,r
记录 l l l~ r r r 区间的涂色方案数。

AC Code

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
	#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
	#include <ccomplex>
	#include <cfenv>
	#include <cinttypes>
	#include <cstdalign>
	#include <cstdbool>
	#include <cstdint>
	#include <ctgmath>
	#include <cwchar>
	#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
	#include <array>
	#include <atomic>
	#include <chrono>
	#include <condition_variable>
	#include <forward_list>
	#include <future>
	#include <initializer_list>
	#include <mutex>
	#include <random>
	#include <ratio>
	#include <regex>
	#include <scoped_allocator>
	#include <system_error>
	#include <thread>
	#include <tuple>
	#include <typeindex>
	#include <type_traits>
	#include <unordered_map>
	#include <unordered_set>
#endif
using namespace std;
#define int long long
const int Mod = 998244353;
const int Maxn = 500 + 5;
int n, m, a[Maxn];
int tong[Maxn][Maxn];//区块l~r的方案数
inline int dfs(int l, int r) {
	if (tong[l][r]) {
		return tong[l][r];
	} else if (l >= r) {
		tong[l][r] = 1;
	} else {
		int minid = l, tmp1 = 0, tmp2 = 0;
		for (int i = l; i <= r; i++) {//求当前区块编号最小颜色
			if (a[i] < a[minid]) {
				minid = i;
			}
		}
		for (int i = l; i <= minid; i++) {
			tmp1 = (tmp1 + dfs(l, i - 1) * dfs(i, minid - 1) % Mod) % Mod;
		}
		for (int i = minid; i <= r; i++) {
			tmp2 = (tmp2 + dfs(minid + 1, i) * dfs(i + 1, r) % Mod) % Mod;
		}
		tong[l][r] = tmp1 * tmp2 % Mod;
	}
	return tong[l][r];
}
inline void work() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	dfs(1, m);
	cout << tong[1][m] % Mod << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	work();
	return 0;
}

没看懂?戳我

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1382167.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

redis夯实之路-主从复制详解

Redis中可以通过执行slaveof命令或者设置slaveof选项&#xff0c;让一个服务器区复制另一个服务器&#xff0c;被复制的为主服务器&#xff0c;复制的为从服务器。 复制 Redis中可以通过执行slaveof命令或者设置slaveof选项&#xff0c;让一个服务器区复制另一个服务器&#…

科大讯飞星火大模型接入API js 部分 接口 安装注意事项

下载以下链接例子运行程序 https://xfyun-doc.xfyun.cn/static%2F16968175055332330%2Fspark_js_demo.zip 官网给的说明 准备 1.在demo中填写APPID、APISecret、APIKey&#xff0c;可到控制台-我的应用-大模型页面获取 2.安装nodejs 本地运行 1.打开cmd&#xff0c;进入dem…

2024三掌柜赠书活动第三期:Rust系统编程

目录 前言 Rust语言概念 关于《Rust系统编程》 Rust系统编程的核心点 Rust系统编程的关键技术和工具 编辑推荐 内容简介 作者简介 图书目录 书中前言/序言 《Rust系统编程》全书速览 结束语 前言 在技术圈&#xff0c;最近的编程语言新秀当属Rust莫属&#xff0c;R…

Center审计策略表安装和策略添加(事务)——(Linux/Windows版本)

本博客主要讲述Center的审计策略表安装和策略添加 使用事务添加 1、开启事务 my->StartTransaction(); 2、编写sql语句 //清除原来数据&#xff0c;防止数据污染my->Query("DROP TABLE IF EXISTS t_strategy");string sql "CREATE TABLE t_strategy (…

虚幻引擎nDisplay教程:如何同步nDisplay节点与Switchboard + Helix Core

对于使用大型LED屏幕进行拍摄的虚拟制作团队来说&#xff0c;虚幻&#xff08;Unreal&#xff09;的nDisplay是一个重要的工具。但是&#xff0c;在nDisplay中将正确版本的文件发送到每个节点会非常耗时。立即阅读本文&#xff0c;您将了解到如何使用Perforce Helix Core版本控…

Jenkins集成Sonar Qube

下载插件 重启Jenkins 容器 sonarqube 使用令牌 Jenkins 配置 重新构建

Flask类视图的基本用法及高级技巧详解

概要 当我们谈论Web开发时&#xff0c;Flask是Python世界中最受欢迎的微框架之一。简洁灵活的设计让它在开发小型到中型的Web应用程序时尤其受欢迎。在Flask中处理URL路由时&#xff0c;我们常常会使用基于函数的视图。尽管这很简单直接&#xff0c;但是随着应用的增长&#x…

Linux驱动学习—I2C总线

1、应用层实现I2C通信 1.1 I2C简介 I2C是很常见的一种总线协议&#xff0c;I2C是NXP公司设计的&#xff0c;I2C使用两条线在主控制器和从机之间进行数据通信。一条是SCL&#xff08;串行时钟线&#xff09;&#xff0c;另外一条是SDA(串行数据线)&#xff0c;因为I2C这两条数…

如何使用创建时间给文件重命名,简单的批量操作教程

在处理大量文件时&#xff0c;有时要按照规则对文件重命名&#xff0c;根据文件的创建时间来重命名。那如何批量操作呢&#xff1f;现在一起来看云炫文件管理器如何用文件的创建时间来批量重命名。 按创建时间重命名文件的前后对比图。 用创建时间批量给文件重命名的步骤&…

定时器问题(vue的问题)

我在a页面写一个定时&#xff0c;让他每秒钟打印一个1&#xff0c;然后跳转到b页面&#xff0c;此时可以看到&#xff0c;定时器依然在执行。这样是非常消耗性能的。如下图所示&#xff1a; 解决方法1 首先我在data函数里面进行定义定时器名称&#xff1a; data() {return {t…

六、新建窗体时,几种窗体的区别

新建窗体时&#xff0c;会有几种类型的选项&#xff0c;很多同学不明白其中的意思&#xff0c;我们在本章节中详细介绍一下几种窗体的区别。 窗体的类型分以下几种 Dialog with Buttons Bottom 带按钮的对话框&#xff0c;按钮在底部 Dialog with Buttons Right 带按钮的对话框…

【Java SE语法篇】8.面向对象三大特征——封装、继承和多态

&#x1f4da;博客主页&#xff1a;爱敲代码的小杨. ✨专栏&#xff1a;《Java SE语法》 ❤️感谢大家点赞&#x1f44d;&#x1f3fb;收藏⭐评论✍&#x1f3fb;&#xff0c;您的三连就是我持续更新的动力❤️ 文章目录 1. 封装1.1 封装的概念1.2 为什么封装1.3 封装的实现…

Vue基知识四

本文对前边几章所学习的内容&#xff0c;以案例的形式做一个总结 一 TodoList案例 即待办事项案例&#xff0c;效果如下 1.1 组件化编码流程&#xff08;通用&#xff09; 这是编码时的通用流程&#xff0c;以后也可以按照这个流程来写代码&#xff08;熟悉后这个流程的顺…

Flutter开发进阶之动画

Flutter开发进阶之动画 在Flutter中&#xff0c;动画是至关重要的一个部分&#xff0c;它能够为应用程序提供更加丰富和生动的用户体验&#xff0c;Flutter中的动画系统是UI框架的核心功能之一&#xff0c;也是开发者学习Flutter框架的重要部分&#xff0c;由于动画原理在所有…

Python基本语法与变量的相关介绍

python基本语法与变量 python语句的缩进 Python代码块使用缩进对齐表示代码逻辑&#xff0c;Python每段代码块缩进的空白数量可以任意&#xff0c;但要确保同段代码块语句必须包含相同的缩进空白数量。建议在代码块的每个缩进层次使用单个制表符或两个空格或四个空格 , 切记不…

GPU云服务器使用教程、运行YOLOV5项目并连接到本地VSCode(Pycharm)

编程如画&#xff0c;我是panda&#xff01; 之前已经教过大家如何在自己的电脑中配置Pytorch深度学习环境&#xff0c;但是有些小伙伴没有英伟达的GPU&#xff0c;所以用CPU的话训练模型会比较慢&#xff0c;所以这次出一期使用GPU云服务器的教程。 码字不易&#xff0c;如果对…

金南瓜SECS/GEM发送event、VID

金南瓜SECS/GEM发送事件&#xff08;CEID&#xff09;很简单&#xff0c;只需一步就完成。 最重要是简单易懂&#xff0c;任何人一看就上手。无需懂得内部逻辑&#xff0c;以及一大堆的导入问题。 代码如下 C#的代码&#xff1a; // 扫码成功 private void buttonReadBarco…

一个简易的PHP论坛系统

一个简易的PHP论坛系统 php课程设计&#xff0c;毕业设计 预览 技术 bootstrap 4.x jquery css php mysql 5.7 目录结构 登录 管理员 admin/123456 测试用户 user1/123456 更多文章和源码获取查看

51-13 多模态论文串讲—BEiT v3 论文精读

BEIT-3的核心思想是将图像建模为一种语言&#xff0c;这样我们就可以对图像、文本以及图像-文本对进行统一的mask modeling。Multi-way transformer模型可以有效地完成不同的视觉和视觉语言任务&#xff0c;使其成为通用建模的一个有效选择。 同时&#xff0c;本文也对多模态大…

深入 Move 生态,探秘铭文热潮背后的思考

Move 语言是 Meta&#xff08;Facebook&#xff09;在 2018 年开发的新一代智能合约编程语言。回顾过去的一年&#xff0c;Aptos 与 Sui 主网上线&#xff0c;为整个 Web3 开启了下一个十亿用户服务的新征程。Rooch、Initia、MoveMent 等多条使用 Move 语言的区块链网络涌现&am…