CH0104 起床困难综合征 【二进制】

很简单很简单的一个模拟。请务必不要出奇怪的错误。注意保证编写过程思路严谨,想好再下手。

#include <bits/stdc++.h>
using namespace std;

const int N = 100000 + 5;

int n, m, val[N]; char opt[N][3];

bool can_use (int wei, bool cmd) {
    int x1 = 0, x2 = 1;
    for (int i = 1; i <= n; ++i) {
        int nw = (val[i] >> wei) & 1;
        // cout << "val[" << i << "], wei " << i << " = " << nw << endl;
        if (opt[i][0] == 'A') {
            x1 = x1 & nw;
            x2 = x2 & nw;
        }
        if (opt[i][0] == 'O') {
            x1 = x1 | nw;
            x2 = x2 | nw;
        }
        if (opt[i][0] == 'X') {
            x1 = x1 ^ nw;
            x2 = x2 ^ nw;
        }
    }
    // cout << "wei = " << wei << ", x1 = " << x1 << ", x2 = " << x2 << endl;
    if (cmd == true) {
        return x2;
    } else {
        return x1;
    }
}

void oprint (int x) {
    for (int i = 29; i >= 0; --i) {
        printf ("%d", (x >> i) & 1);
    }
    printf ("\n");
}

int main () {
    // freopen ("input", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        cin >> opt[i] >> val[i];
    }
    int res = 0, beg = 0;
    for (int i = 29; i >= 0; --i) {
        if (can_use (i, 0)) {
            // cout << "wei " << i << ", can1" << endl;
            res += (1 << i);
            continue;
        }
        if (beg + (1 << i) <= m) {
            if (can_use (i, 1)) {
                res += (1 << i);
                beg += (1 << i);
            }
        }
    }
    cout << res << endl;
}
posted @ 2020-10-08 16:38  maomao9173  阅读(161)  评论(0编辑  收藏  举报