D. Monocarp and the Set

本题需要按照字符串中的要求去按个放数。那么我们需要换个角度去看,我们假设放的数按顺序分别是a1,a2,a3,a4,a5,a6。那么按照题目中的要求去放就可以得到如下图:

从图中结论可知种类数其实就是所有?的可插入数的乘积。那么你可能回想其他符号的数不会产生一些种类吗,其实如果先将某些数当做?放入之后,'<'和‘>’的位置中放什么数其实已经固定了。所以在这里只有?不同的插入位置来决定种类数。

在这道题里面还需要注意我们每次都去遍历这个字符串求结果的话会超时。所以我们需要计算出一个最初的值之后后面的结果根据某处字符的变动来进行调整。那么调整的话如果是?变成了'<'或'>'的话就需要将产生的数除掉。但题目中又要求最后结果取余。众所周知取余运算里面不能穿插除法运算的。

所以在这里我们需要求出除数的逆元,将其转化成乘法来计算。

因为题目中所给的mod是一个奇数,所以直接使用费马小定理求逆元。

如下:

int binpow(int a, int n) {
	int res = 1;
	while (n) {
		if (n & 1) res = (res*a)%mod;
		a = (a*a)%mod;
		n>>=1;
	}
	return res;
}

那么最终的代码为:

#include <bits/stdc++.h>

using namespace std;
#define int long long
string s;
int n, m;
int ans;
const int mod = 998244353;

int binpow(int a, int n) {
	int res = 1;
	while (n) {
		if (n & 1) res = (res*a)%mod;
		a = (a*a)%mod;
		n>>=1;
	}
	return res;
}

void solve(string str) {
	if (str[1]=='?') {
		return ;
	}
	ans = 1;
	for (int i=2;i<=n;i++) {
		if (str[i]=='?') {
			ans = (ans*(i-1))%mod;
		}
	}
}

void solve2(string str) {
	ans = 1;
	for (int i=2;i<=n;i++) {
		if (str[i]=='?') {
			ans = (ans*(i-1))%mod;
		}
	}
}

signed main() {
	int pos;
	cin>>n>>m;
	cin>>s;
	s = " "+s;
	solve(s);
	if (s[1]=='?') cout<<0<<endl;
	else cout<<ans<<endl;
	char ch;
	while (m--) {
		cin>>pos>>ch;
//		cout<<pos<<" "<<ch<<" "<<s[pos]<<endl;
		if (pos!=1&&ch!='?'&&s[pos]=='?') {
//			cout<<"寄哪里了"<<endl;
			ans = (ans*binpow(pos-1, mod-2))%mod;
		}
		if (pos!=1&&ch=='?'&&s[pos]!='?'){
			ans = (ans*(pos-1))%mod;
		}
		if ((pos==1&&ch=='?')||(s[1]=='?'&&(pos!=1||ch=='?'))) cout<<0<<endl;
//		else if ((s[pos]==ch)||(s[pos]!=ch&&ch!='?'&&s[pos]!='?')) cout<<ans<<endl;
		else {
//			cout<<"s"<<endl;
			if (ans==0) {
				s[pos] = ch;
				solve(s);
				cout<<ans<<endl;
				continue;
			}
			cout<<ans<<endl;
		}
		s[pos] = ch;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值