AtCoder Regular Contest 170 C. Prefix Mex Sequence(dp mex性质)

文章讲述了如何使用动态规划解决一个关于01字符串的问题,要求计算满足特定条件(mex规则)的长为n的序列A的方案数,通过将mex不同的方案聚类来降低复杂度,最终给出O(n)时间复杂度的代码实现。
Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

题目

给定一个长为n(n<=5e3)的01字符串s,

求满足以下条件的长为n的序列a的方案数,答案对998244353取模

条件:

对于任意i∈[1,n],

如果s[i]=1,则有A[i]=mex(A[1],A[2],...,A[i-1])

如果s[i]=0,则有A[i]≠mex(A[1],A[2],...,A[i-1])

其中,mex为未出现在集合内的最小正整数

思路来源

官方题解

题解

直观地想,是dp[i][j]表示前i个mex为j的方案数,

然后注意到会选了一些大于mex的数,所以为了转移,

就想办法把这些位置记录下来,实际对mex有贡献的时候再填进去,

但是,需要处理一个用x个位置覆盖了y种值的方案数,

求这个的复杂度很难降下来,应该是O(n^3)的

考虑另辟蹊径,dp[i][j]表示前i个出现了j种不同值的方案数,

可以这么做的原因是,无论mex是何值,

s[i]=1在值种类数确定的时候转移方程是相同的,s[i]=0也是相同的

所以,相当于是把mex不同的方案聚类起来,放在一起统计了

①如果s[i]=1,代表本次一定新选了一种数

[0,m]共m+1种数,只有之前<m+1种本次才可以,

插入之前种类数里不存在的最小正整数的这个空隙,

每种方案对应的选法都是唯一的,dp[i][j]->dp[i+1][j+1]

②如果s[i]=0,本次可以新选,也可以不新选

如果不新选,之前有j种,本次挑一种,有j种方案,j*dp[i][j]->dp[i+1][j+1]

如果新选,之前有j种,

没出现的m+1-j种中,恰有一种不能选(会导致s[i]=1),其余都可以选,(m-j)*dp[i][j]->dp[i+1][j+1]

代码

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
// #include<map>
// #include<random>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
typedef double db;
typedef pair<int,int> P;
#define fi first
#define se second
#define pb push_back
#define dbg(x) cerr<<(#x)<<":"<<x<<" ";
#define dbg2(x) cerr<<(#x)<<":"<<x<<endl;
#define SZ(a) (int)(a.size())
#define sci(a) scanf("%d",&(a))
#define pt(a) printf("%d",a);
#define pte(a) printf("%d\n",a)
#define ptlle(a) printf("%lld\n",a)
#define debug(...) fprintf(stderr, __VA_ARGS__)
const int N=5e3+10,mod=998244353;
int n,m,dp[N][N],v;//dp[i][j]表示前i个出现了j种不同的数字且s[1,i]合法的方案数,前i个最多i种
void add(int &x,int y){
    x=(x+y)%mod;
}
int main(){
    sci(n),sci(m);
    dp[0][0]=1;
    rep(i,0,n-1){
        sci(v);
        if(v==1){//[0,m] 共m+1种 只有之前<m+1种才可以 插入最小空隙 方案唯一
            rep(j,0,min(i,m)){
                add(dp[i+1][j+1],dp[i][j]);
            }
        }
        else{// 要么新增一种,要么没有新增,但是新增一种的时候有一个值不能选,也就是m+1-j种里只能选m-j种,否则s[i]=1不合法
            rep(j,0,min(i,m+1)){
                if(j<m)add(dp[i+1][j+1],1ll*(m-j)*dp[i][j]%mod); // 新增一种
                if(j)add(dp[i+1][j],1ll*j*dp[i][j]%mod);// 不新增
            }
        }
    }
    int ans=0;
    rep(i,0,min(n,m+1)){
        add(ans,dp[n][i]);
    }
    pte(ans);
    return 0;
}

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

链接:https://ac.nowcoder.com/acm/contest/108300/A 来源:牛客网 题目描述 Yuki gives you a sequence of 𝑛 n positive integers 𝑓 1 , … , 𝑓 𝑛 f 1 ​ ,…,f n ​ , where for each 𝑖 i, 1 ≤ 𝑓 𝑖 ≤ 𝑖 1≤f i ​ ≤i holds. She wants you to construct an 𝑛 n-ordered square matrix 𝐴 A such that: For each 1 ≤ 𝑖 , 𝑗 ≤ 𝑛 1≤i,j≤n, 0 ≤ 𝐴 𝑖 , 𝑗 ≤ 𝑛 0≤A i,j ​ ≤n; For each 1 ≤ 𝑖 ≤ 𝑛 1≤i≤n, mex( 𝐴 𝑖 , 1 , 𝐴 𝑖 , 2 , … , 𝐴 𝑖 , 𝑛 ) = mex( 𝐴 1 , 𝑖 , 𝐴 2 , 𝑖 , … , 𝐴 𝑛 , 𝑖 ) = 𝑓 𝑖 mex(A i,1 ​ ,A i,2 ​ ,…,A i,n ​ )=mex(A 1,i ​ ,A 2,i ​ ,…,A n,i ​ )=f i ​ . It can be proven that for any valid 𝑓 1 , … , 𝑓 𝑛 f 1 ​ ,…,f n ​ , a solution always exists. Recall that the mexmex of a sequence 𝑏 1 , … , 𝑏 𝑚 b 1 ​ ,…,b m ​ is the smallest non-negative integer 𝑥 x such that 𝑥 x does not appear in 𝑏 b. 输入描述: Each test contains multiple test cases. The first line of input contains a single integer 𝑡 t ( 1 ≤ 𝑡 ≤ 2 ⋅ 1 0 4 1≤t≤2⋅10 4 ) — the number of test cases. The description of the test cases follows. The first line contains a single integer 𝑛 n ( 1 ≤ 𝑛 ≤ 1    414 1≤n≤1414), denoting the length of the sequence. The second line contains 𝑛 n integers 𝑓 1 , … , 𝑓 𝑛 f 1 ​ ,…,f n ​ ( 1 ≤ 𝑓 𝑖 ≤ 𝑖 1≤f i ​ ≤i), describing the given sequence. It is guaranteed that the sum of 𝑛 2 n 2 over all test cases does not exceed 2 ⋅ 1 0 6 2⋅10 6 . 输出描述: For each test case, output 𝑛 n lines, in which the 𝑖 i-th line contains 𝑛 n non-negative integers 𝐴 𝑖 , 1 , 𝐴 𝑖 , 2 , … , 𝐴 𝑖 , 𝑛 A i,1 ​ ,A i,2 ​ ,…,A i,n ​ in the range [ 0 , 𝑛 ] [0,n]. 示例1 输入 复制 3 3 1 1 2 5 1 1 3 2 5 4 1 2 1 3 输出 复制 0 2 0 0 0 0 0 0 1 3 2 0 0 4 0 0 2 0 3 2 4 1 0 2 0 0 1 1 0 2 0 4 3 1 2 0 2 2 0 1 0 1 2 3 0 0 0 0 2 1 备注: In the first test case, 𝑓 = [ 1 , 1 , 2 ] f=[1,1,2], and a possible square matrix is as follows: 𝐴 = [ 0 2 0 0 0 0 0 0 1 ] . A= ⎣ ⎡ ​ 0 0 0 ​ 2 0 0 ​ 0 0 1 ​ ⎦ ⎤ ​ . In the first row, mex( [ 0 , 2 , 0 ] ) = 𝑓 1 = 1 mex([0,2,0])=f 1 ​ =1, because 0 0 appears in [ 0 , 2 , 0 ] [0,2,0], but 1 1 does not, so 1 1 is the smallest non-negative integer not present; in the first column, similarly, mex( [ 0 , 0 , 0 ] ) = 𝑓 1 = 1 mex([0,0,0])=f 1 ​ =1. It is easy to verify that this matrix also satisfies all other constraints. C++
07-23
# T617559 「TPOI-5A」Luminescence ## 题目背景 ![](https://cdn.luogu.com.cn/upload/image_hosting/ownsj515.png) (图片来自 Phigros 曲绘,侵删。) ## 题目描述 给定 $n$ 与两个长度为 $n$ 的序列 $a,b$。定义一个 $0\sim n-1$ 的排列 $q$ 是 **魔怔的**,当且仅当: - $\forall 1\le k\le n,\min^k_{i=1}q_i=a_k$。 - $\forall 1\le k\le n,\min^n_{i=k}q_i=b_k$。 定义一个排列 $q$ 的权值为 $\sum_{1\le l\le r\le n}\operatorname{mex}_{l\le i\le r}q_i$,求所有魔怔的排列的权值之和。答案对 $998244353$ 取模。 一个集合 $M$ 的 $\operatorname{mex}(M)$ 定义为最小的没有在 $M$ 中出现的自然数。如 $\text{mex}\{1,2,3,4\}=0,\text{mex}\{0,1,3,4\}=2$。 ## 输入格式 本题有多组测试数据。对于每个测试点,先输入一个正整数 $T$,表示数据组数。 对于每一组测试数据,第一行一个正整数 $n$, 第二行输入 $n$ 个整数 $a_1,a_2,\dots,a_n$,第三行 $n$ 个整数 $b_1,b_2,\dots,b_n$。 ## 输出格式 对于每一组测试数据,输出一行一个整数,表示所有魔怔的排列的权值之和。 ## 输入输出样例 #1 ### 输入 #1 ``` 3 4 0 0 0 0 0 1 2 3 4 1 0 0 0 0 0 2 2 4 0 0 0 0 0 1 1 1 ``` ### 输出 #1 ``` 10 11 14 ``` ## 说明/提示 |$\text{Subtask}$|$n\le$|$\sum n\le$|分值| |:-:|:-:|:-:|:-:| |$1$|$8$|$800$|$20$| |$2$|$10^3$|$10^4$|$40$| |$3$|$2\times10^5$|$2\times10^6$|$40$| 对于 $100\%$ 的数据,$1 \le n \le 2 \times 10^5,0 \le a_i,b_i < n, \sum n \le 2 \times 10^6$。 **对于每组数据保证存在至少一个魔怔的排列。** c++,不用vector,注释
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值