本题需要按照字符串中的要求去按个放数。那么我们需要换个角度去看,我们假设放的数按顺序分别是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;
}