题解:CF2170E Binary Strings and Blocks
thousands_of_years · · 题解
如果你是容斥玩家,快快来看这篇。
solution
因为是求每个区间内的数都存在两种的方案数,我们考虑正难则反,找不满足的情况。
容易想到
状态还是太差了,发现最后算贡献是
枚举区间时,我们设
还有对于
否则
考虑优化转移,对于
对于
复杂度
code
不想写,给个没加优化的
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace fast_IO {
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *p111 = ibuf, *p222 = ibuf, *p333 = obuf;
#define getchar() ((p111==p222)and(p222=(p111=ibuf)+fread(ibuf,1,IOSIZE,stdin),p111==p222)?(EOF):(*p111++))
#define putchar(x) ((p333==obuf+IOSIZE)&&(fwrite(obuf,p333-obuf,1,stdout),p333=obuf),*p333++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T readdd() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool readdd(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void printtt(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) printtt(x / 10); putchar(x % 10 + 48); }
inline bool readdd(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool readdd(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void printtt(char x) { putchar(x); }
inline void printtt(char *x) { while (*x) putchar(*x++); }
inline void printtt(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool readdd(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void printtt(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool readdd(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void printtt(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int readdd(T& a, T1&... other) { return readdd(a) + readdd(other...); }
template<typename T, typename... T1> inline void printtt(T a, T1... other) { printtt(a), printtt(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p333 - obuf, 1, stdout); } } iooo;
template<typename T> Fast_IO& operator >> (Fast_IO &iooo, T &b) { return readdd(b), iooo; }
template<typename T> Fast_IO& operator << (Fast_IO &iooo, T b) { return printtt(b), iooo; }
#define cout iooo
#define cin iooo
#define endl '\n'
} using namespace fast_IO;
const int N=3e5+9,mod=998244353;
int n,m,dp[2][N],inc[N],f[2][N],inv[N];
struct node{
int l,r;
}e[N];
bool cmp(node aa,node bb)
{
return aa.l<bb.l;
}
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
void solve()
{
cin>>n>>m;
for(int i=1;i<=m;i++)
cin>>e[i].l>>e[i].r;
sort(e+1,e+1+m,cmp);
inc[0]=1;
inv[0]=1;
for(int i=1;i<=n+2;i++)
inc[i]=inc[i-1]*2%mod,inv[i]=qpow(inc[i],mod-2);
for(int i=0;i<=max(n,m)+1;i++)
dp[0][i]=dp[1][i]=f[0][i]=f[1][i]=0;
f[0][0]=dp[0][0]=inc[n];
for(int i=1;i<=m;i++)
{
for(int j=0;j<e[i].l;j++)
{
f[1][e[i].r]+=dp[0][j]*inv[e[i].r-e[i].l]%mod;
f[1][e[i].r]%=mod;
f[0][e[i].r]+=dp[1][j]*inv[e[i].r-e[i].l]%mod;
f[0][e[i].r]%=mod;
}
for(int j=e[i].l;j<e[i].r;j++)
{
f[1][e[i].r]+=dp[0][j]*inv[e[i].r-j]%mod;
f[1][e[i].r]%=mod;
f[0][e[i].r]+=dp[1][j]*inv[e[i].r-j]%mod;
f[0][e[i].r]%=mod;
}
for(int j=e[i].r;j<=n;j++)
{
f[1][j]+=dp[0][j];
f[1][j]%=mod;
f[0][j]+=dp[1][j];
f[0][j]%=mod;
}
for(int j=0;j<=n;j++)
{
dp[0][j]=f[0][j];
dp[1][j]=f[1][j];
// cout<<dp[0][j]<<" "<<dp[1][j]<<endl;
}
}
int ans=inc[n];
for(int i=1;i<=n;i++)
{
ans=(ans-f[1][i]+f[0][i])%mod;
}
ans=(ans+mod)%mod;
cout<<ans<<endl;
}
signed main()
{
int T;
cin>>T;
while(T--)
solve();
}