#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; const int kMaxN = 1000000; const int kModulus = 1000 * 1000 * 1000 + 7; vector<bool> read_buffer(kMaxN); vector<bool> clause_buffer; class Clause { public: Clause() : first(kMaxN), last(0) {} static Clause Read() { Clause clause; char c; do { if (scanf("%c", &c) == -1) c = '\n'; } while (c != '(' && c != '\n'); if (c == '\n') return clause; while (c != ')') { while (c != '~' && c != 'x') scanf("%c", &c); const bool negated = c == '~'; int i = 0; while (c != ' ' && c != ')') { scanf("%c", &c); if (c >= '0' && c <= '9') { // cerr << "Digit " << c << endl; i *= 10; i += c; i -= '0'; // cerr << i << endl; } } clause.AddLiteral(negated, --i); } clause.Finalize(); return clause; } bool Empty() const { return first > last; } void Print() const { cerr << first << ' '; for (int i = first; i <= last; ++i) cerr << clause_buffer[offset + i - first]; cerr << endl; } bool operator<(const Clause& rhs) const { return first == rhs.first ? last > rhs.last : first < rhs.first; } int first; int last; int offset; private: void AddLiteral(const bool negated, const int i) { read_buffer[i] = negated; first = min(first, i); last = max(last, i); } void Finalize() { offset = clause_buffer.size(); for (int i = first; i <= last; ++i) clause_buffer.push_back(read_buffer[i]); } }; class Tree { public: Tree(const int n) : n_(n) { levels_.resize(n_ + 2); for (int i = 0; i <= n_ + 1; ++i) { levels_[i].push_back(value_.size()); value_.push_back(0); left_.push_back(i + 1); right_.push_back(i + 1); } // All-clauses-satisfied state. left_[n_] = n_; right_[n_] = n_; // Some-clause-failed state. left_[n_ + 1] = n_ + 1; right_[n_ + 1] = n_ + 1; } void AddClause(const Clause& clause) { int state = clause.first; int original_state = state; for (int i = clause.first; i < clause.last; ++i) { // Update tree for non-terminal letter. if (clause_buffer[clause.offset + i - clause.first]) { left_[state] = left_[original_state]; original_state = right_[original_state]; right_[state] = value_.size(); state = right_[state]; } else { right_[state] = right_[original_state]; original_state = left_[original_state]; left_[state] = value_.size(); state = left_[state]; } levels_[i + 1].push_back(value_.size()); value_.push_back(0); left_.push_back(-1); right_.push_back(-1); } // Update tree for terminal letter. if (clause_buffer[clause.offset + clause.last - clause.first]) { left_[state] = left_[original_state]; right_[state] = n_ + 1; } else { left_[state] = n_ + 1; right_[state] = right_[original_state]; } } int GetValue() { for (int i = 0; i < value_.size(); ++i) value_[i] = 0; // Propagate counts. value_[0] = 1; for (int i = 0; i < n_; ++i) for (int j = 0; j < levels_[i].size(); ++j) { const int state = levels_[i][j]; AddTo(state, left_[state]); AddTo(state, right_[state]); } return value_[n_]; } void Print() { for (int i = 0; i < n_; ++i) for (int j = 0; j < levels_[i].size(); ++j) { const int state = levels_[i][j]; cerr << state << " -> " << left_[state] << ' ' << right_[state] << endl; } cerr << "value: " << GetValue() << endl; } private: void AddTo(const int src, const int dst) { value_[dst] += value_[src]; if (value_[dst] >= kModulus) value_[dst] -= kModulus; } int n_; vector<int> left_; vector<int> right_; vector<int> value_; vector<vector<int> > levels_; }; bool ReadClause(vector<Clause>* v) { const Clause clause = Clause::Read(); if (clause.Empty()) return false; // clause.Print(); v->push_back(clause); return true; } int main() { int n; scanf("%d\n", &n); vector<Clause> v; while (ReadClause(&v)); sort(v.rbegin(), v.rend()); Tree tree(n); // tree.Print(); for (int i = 0; i < v.size(); ++i) { // v[i].Print(); tree.AddClause(v[i]); // tree.Print(); } printf("%d\n", tree.GetValue()); return 0; }
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; const int kMaxN = 1000000; const int kModulus = 1000 * 1000 * 1000 + 7; vector<bool> read_buffer(kMaxN); vector<bool> clause_buffer; class Clause { public: Clause() : first(kMaxN), last(0) {} static Clause Read() { Clause clause; char c; do { if (scanf("%c", &c) == -1) c = '\n'; } while (c != '(' && c != '\n'); if (c == '\n') return clause; while (c != ')') { while (c != '~' && c != 'x') scanf("%c", &c); const bool negated = c == '~'; int i = 0; while (c != ' ' && c != ')') { scanf("%c", &c); if (c >= '0' && c <= '9') { // cerr << "Digit " << c << endl; i *= 10; i += c; i -= '0'; // cerr << i << endl; } } clause.AddLiteral(negated, --i); } clause.Finalize(); return clause; } bool Empty() const { return first > last; } void Print() const { cerr << first << ' '; for (int i = first; i <= last; ++i) cerr << clause_buffer[offset + i - first]; cerr << endl; } bool operator<(const Clause& rhs) const { return first == rhs.first ? last > rhs.last : first < rhs.first; } int first; int last; int offset; private: void AddLiteral(const bool negated, const int i) { read_buffer[i] = negated; first = min(first, i); last = max(last, i); } void Finalize() { offset = clause_buffer.size(); for (int i = first; i <= last; ++i) clause_buffer.push_back(read_buffer[i]); } }; class Tree { public: Tree(const int n) : n_(n) { levels_.resize(n_ + 2); for (int i = 0; i <= n_ + 1; ++i) { levels_[i].push_back(value_.size()); value_.push_back(0); left_.push_back(i + 1); right_.push_back(i + 1); } // All-clauses-satisfied state. left_[n_] = n_; right_[n_] = n_; // Some-clause-failed state. left_[n_ + 1] = n_ + 1; right_[n_ + 1] = n_ + 1; } void AddClause(const Clause& clause) { int state = clause.first; int original_state = state; for (int i = clause.first; i < clause.last; ++i) { // Update tree for non-terminal letter. if (clause_buffer[clause.offset + i - clause.first]) { left_[state] = left_[original_state]; original_state = right_[original_state]; right_[state] = value_.size(); state = right_[state]; } else { right_[state] = right_[original_state]; original_state = left_[original_state]; left_[state] = value_.size(); state = left_[state]; } levels_[i + 1].push_back(value_.size()); value_.push_back(0); left_.push_back(-1); right_.push_back(-1); } // Update tree for terminal letter. if (clause_buffer[clause.offset + clause.last - clause.first]) { left_[state] = left_[original_state]; right_[state] = n_ + 1; } else { left_[state] = n_ + 1; right_[state] = right_[original_state]; } } int GetValue() { for (int i = 0; i < value_.size(); ++i) value_[i] = 0; // Propagate counts. value_[0] = 1; for (int i = 0; i < n_; ++i) for (int j = 0; j < levels_[i].size(); ++j) { const int state = levels_[i][j]; AddTo(state, left_[state]); AddTo(state, right_[state]); } return value_[n_]; } void Print() { for (int i = 0; i < n_; ++i) for (int j = 0; j < levels_[i].size(); ++j) { const int state = levels_[i][j]; cerr << state << " -> " << left_[state] << ' ' << right_[state] << endl; } cerr << "value: " << GetValue() << endl; } private: void AddTo(const int src, const int dst) { value_[dst] += value_[src]; if (value_[dst] >= kModulus) value_[dst] -= kModulus; } int n_; vector<int> left_; vector<int> right_; vector<int> value_; vector<vector<int> > levels_; }; bool ReadClause(vector<Clause>* v) { const Clause clause = Clause::Read(); if (clause.Empty()) return false; // clause.Print(); v->push_back(clause); return true; } int main() { int n; scanf("%d\n", &n); vector<Clause> v; while (ReadClause(&v)); sort(v.rbegin(), v.rend()); Tree tree(n); // tree.Print(); for (int i = 0; i < v.size(); ++i) { // v[i].Print(); tree.AddClause(v[i]); // tree.Print(); } printf("%d\n", tree.GetValue()); return 0; } |