#include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; vector<vector<pair<int, bool>>> parse(string& input) { vector<vector<pair<int, bool>>> result; int i = 0; while (i < input.size()) { if (input[i] == ' ' || input[i] == '(' || input[i] == '^') { ++i; continue; } vector<pair<int, bool>> list; while (input[i] != ')') { if (input[i] != '~' && input[i] != 'x') { ++i; continue; } pair<int, bool> item; if (input[i] == 'x') { item.second = true; ++i; } else { item.second = false; i += 2; } item.first = 0; while (input[i] != ' ' && input[i] != ')') { item.first *= 10; item.first += input[i++] - '0'; } list.push_back(item); } result.push_back(list); ++i; } return result; } bool test(int value, vector<vector<pair<int, bool>>>& statement) { for (auto list : statement) { bool t = false; for (auto it : list) { int mask = 1 << (it.first - 1); if (((value & mask) != 0) == it.second) { t = true; break; } } if (!t) { return false; } } return true; } #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string input; getline(cin, input); getline(cin, input); auto statement = parse(input); int l = 1 << n; int result = 0; REP(x, l) { if (test(x, statement)) { result++; } } cout << result << endl; return 0; } #endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <bits/stdc++.h> using namespace std; typedef long long int64; #define DEBUG(x) cerr << #x << " = " << x << endl; #define REP(x, n) for(__typeof(n) x = 0; x < (n); ++x) #define FOR(x, b, e) for(__typeof(b) x = (b); x != (e); x += 1 - 2 * ((b) > (e))) const int INF = 1000000001; const double EPS = 10e-9; vector<vector<pair<int, bool>>> parse(string& input) { vector<vector<pair<int, bool>>> result; int i = 0; while (i < input.size()) { if (input[i] == ' ' || input[i] == '(' || input[i] == '^') { ++i; continue; } vector<pair<int, bool>> list; while (input[i] != ')') { if (input[i] != '~' && input[i] != 'x') { ++i; continue; } pair<int, bool> item; if (input[i] == 'x') { item.second = true; ++i; } else { item.second = false; i += 2; } item.first = 0; while (input[i] != ' ' && input[i] != ')') { item.first *= 10; item.first += input[i++] - '0'; } list.push_back(item); } result.push_back(list); ++i; } return result; } bool test(int value, vector<vector<pair<int, bool>>>& statement) { for (auto list : statement) { bool t = false; for (auto it : list) { int mask = 1 << (it.first - 1); if (((value & mask) != 0) == it.second) { t = true; break; } } if (!t) { return false; } } return true; } #ifndef CATCH_TEST int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string input; getline(cin, input); getline(cin, input); auto statement = parse(input); int l = 1 << n; int result = 0; REP(x, l) { if (test(x, statement)) { result++; } } cout << result << endl; return 0; } #endif |