题意:
给你一个序列,该序列的满足题目所给条件的任意子序列数目是多少,答案对998244353取模。
思路:
当前的数x只能加入到mex的值为x-1,x,x+1的序列中,因而我们记dp[x][1]为mex的值等于x-1的序列的数目,dp[x][0]为mex的值等于x的值的数目。
由此:
假设当前要加入的数为x,那么mex为x+1的序列数dp[x+1][0]=dp[x+1][0]*2。
而mex为x的序列数dp[x+1][1]=dp[x+1][1]*2+dp[x-1][0]
还要注意x对x+1的mex序列也有贡献,即dp[x+1][0]=dp[x+1][0]*2。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int mod = 998244353;
ll vis[500010];
ll qpow(ll a,ll b)
{
ll res = 1;
while(b)
{
if(b&1)res = res*a%mod;
a = a*a%mod;
b>>=1;
}
return res;
}
int a[500010];
ll dp[500010][2];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
for(int i = 1; i <= n; i++)scanf("%d",&a[i]);
int mex = 0;
ll ans = 0;
dp[0][0] = 1;
for(int i = 1; i <= n; i++)
{
a[i]++;
dp[a[i]][0] = (dp[a[i]-1][0]+dp[a[i]][0]*2%mod)%mod;
if(a[i]>1)
dp[a[i]][1] = (dp[a[i]][1]*2+dp[a[i]-2][0])%mod;
else dp[a[i]][1] = dp[a[i]][1]*2%mod;
dp[a[i]+2][1] = (dp[a[i]+2][1]*2)%mod;
}
for(int i = 1; i <= n+1; i++)
ans = (ans+dp[i][0]+dp[i][1])%mod;
cout<<ans<<endl;
for(int i = 0; i <= n+3; i++)vis[a[i]] = 0,dp[i][0] = dp[i][1] = 0;
}
}