题解:P16184 [LBA-OI R1 A] 数据交锋

· · 题解

传送门

题目大意

给你两个长度为 n 的 01 串 A 和 B,分别代表一个二进制数(注意低位在右),定义得分为 01 串所代表的二进制数。

接下来有 m 次修改,每次给出两个整数 t 和 p,若 t = 1 则将 A_p 反转,即如果是 0 则变成 1,如果是 1 则变成 0。若 t = 2 则对 B_p 进行同样的操作。

问每一次操作结束后,两个 01 串的得分和是多少。

思路

首先我们可以预处理出 2 的幂,放到 pow1 数组里,然后在修改开始前统计两个 01 串的得分 score1 和 score2。

修改过程中我们注意到,如果修改 p 位置的字符,对得分影响只会有加或减 2^p,那么只需要在修改时加上判断,如果是从 1 变 0,得分就减去 2^p,如果是从 0 变 1 则反之。最后输出得分和即可。

时间复杂度 O(m),可以通过本题。

放一下赛时代码。 :::info[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