비트마스크 명령어를 구현하는 문제이다.
비트마스크 공부하기 싫어서 미뤄뒀었는데 오늘 공부해보니 의외로 재밌다!!!
S = S | (1 << N)
S = S & ~(1 << N)
S & (1 << N)
S = S ^ (1 << N)
{0, 1, ..., N} 집합은 다음과 같이 표현할 수 있다.
S = (1 << N + 1) - 1
S = 0
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int m, s = 0;
cin >> m;
while (m--) {
string command;
int num;
cin >> command;
if (command == "add") {
cin >> num;
s = s | (1 << num);
}
else if (command == "remove") {
cin >> num;
s = s & ~(1 << num);
}
else if (command == "check") {
cin >> num;
if (s & (1 << num))
cout << 1 << '\n';
else
cout << 0 << '\n';
}
else if (command == "toggle") {
cin >> num;
s = s ^ (1 << num);
}
else if (command == "all")
s = (1 << 21) - 1;
else if (command == "empty")
s = 0;
}
return 0;
}