「杂题乱刷」CF1886D

题目链接

简单计数题。

容易看出 \(<,>\) 这两个符号一定只有 \(1\) 种选择,而 \(?\) 就有 \(i-1\) 中选择,总方案数很好推出,这样时间复杂度为 \(O(nm)\),不能通过此题,因此我们考虑用逆元优化,优化后时间复杂度 \(O(m)\)

参考代码:

点击查看代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
*/
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
ll t;
long long pw(long long x,long long y,long long mod)
{
	long long an=1,tmp=x;
	while(y!=0)
	{
		if(y&1)
			an=(an%mod*tmp%mod)%mod;
		tmp=(tmp%mod*tmp%mod)%mod;
		y=y>>1;
	}
	an=an%mod;
	return an%mod;
}
void solve()
{
	ll n,m,x,mod=998244353,ans=1;
	string s;
	char y;
	cin>>n>>m>>s;
	s="  "+s;
	forl(i,3,n)
		if(s[i]=='?')
			ans*=i-2,ans%=mod;
	(s[2]=='?')?cout<<0<<endl:cout<<ans<<endl;
	while(m--)
	{
		cin>>x>>y;
		x++;
		if(x!=2 && s[x]=='?')
			ans*=pw(x-2,mod-2,mod),ans%=mod;
		s[x]=y;
		if(x!=2 && s[x]=='?')
			ans*=x-2,ans%=mod;
		(s[2]=='?')?cout<<0<<endl:cout<<ans<<endl;
	}
}
int main()
{
	IOS;
	t=1;
//	cin>>t;
	while(t--)
		solve();
    /******************/
	/*while(L<q[i].l) */
	/*    del(a[L++]);*/
	/*while(L>q[i].l) */
	/*    add(a[--L]);*/
	/*while(R<q[i].r) */
	/*	  add(a[++R]);*/
	/*while(R>q[i].r) */
	/*    del(a[R--]);*/
    /******************/
	QwQ;
}

posted @ 2024-02-07 20:30  wangmarui  阅读(5)  评论(0编辑  收藏  举报