题目大意
给你两个长度为 的 串 和 ,分别代表一个二进制数(注意低位在右),定义得分为 串所代表的二进制数。
接下来有 次修改,每次给出两个整数 和 ,若 则将 反转,即如果是 则变成 ,如果是 则变成 。若 则对 进行同样的操作。
问每一次操作结束后,两个 串的得分和是多少。
思路
首先我们可以预处理出 的幂,放到 数组里,然后在修改开始前统计两个 串的得分 和 。
修改过程中我们注意到,如果修改 位置的字符,对得分影响只会有加或减 ,那么只需要在修改时加上判断,如果是从 变 ,得分就减去 ,如果是从 变 则反之。最后输出得分和即可。
时间复杂度 ,可以通过本题。
放一下赛时代码。
Code
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int mod = 998244353;
const int maxn = 1e6 + 5;
int pow1[maxn];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
string s1, s2; cin >> s1 >> s2;
pow1[0] = 1;
for (int i = 1; i < n; i ++ ) pow1[i] = (pow1[i-1] * 2) % mod;
int score1 = 0, score2 = 0;
for (int i = 0; i < n; i ++ ){
score1 += (s1[n-i-1] - '0') * pow1[i];
score2 += (s2[n-i-1] - '0') * pow1[i];
}//千万不要忘了低位在右,我就是因为忘了导致理解题意用了20min(绷)。
while(m -- ){
int t, p; cin >> t >> p;
if(t == 1){
if(s1[n-p-1] == '0') s1[n-p-1] = '1', score1 += pow1[p];
else if(s1[n-p-1] == '1') s1[n-p-1] = '0', score1 -= pow1[p];
}
if(t == 2){
if(s2[n-p-1] == '0') s2[n-p-1] = '1', score2 += pow1[p];
else if(s2[n-p-1] == '1') s2[n-p-1] = '0', score2 -= pow1[p];
}//意义见上文。
cout << (score1 + score2) % mod << endl;//记得取模。
}
return 0;//华丽收尾。
}管理员大大求过QAQ