/* 2016 * Maciej Szeptuch */ #include <cstdio> #include <vector> #include <cctype> const int MAX_BUFFER = 16384; const int MAX_VARIABLES = 1048576; const long long int MOD = 1000000007LL; struct Variable { std::vector<int> clauses; }; struct Clause { int start; int end; int length; }; class iTree { private: static const int TREE_SIZE = MAX_VARIABLES; int data[TREE_SIZE * 2]; std::pair<int, int> max[TREE_SIZE * 2]; public: iTree(); void add(int start, int end); void remove(int start, int end); void insert(int start, int end, int value, int iStart=0, int iEnd=TREE_SIZE-1, int iPos=1); std::pair<int, int> get_max(void) const; private: void propagate(int iPos); }; Clause clause[MAX_VARIABLES]; int clauses; iTree tree; Variable variable[MAX_VARIABLES]; char buffer[MAX_BUFFER]; int variables; long long int power_table[MAX_VARIABLES]; std::vector<int> dropped_clauses; bool clause_dropped[MAX_VARIABLES]; void feed(const char *input); long long int solve(int variables_set=0); int main(void) { scanf("%d", &variables); power_table[0] = 1; for(int v = 1; v < variables; ++ v) { power_table[v] = power_table[v - 1] * 2; if(power_table[v] > MOD) power_table[v] %= MOD; } while(scanf("%16383s", buffer) != -1) feed(buffer); printf("%lld\n", solve()); return 0; } long long int solve(int variables_set) { auto best = tree.get_max(); if(best.second == 0) return power_table[variables - variables_set]; Variable &var = variable[best.first]; char singleton = 0; for(auto clause_id: var.clauses) { bool negation = false; if(clause_id < 0) { negation = true; clause_id *= -1; } if(clause_dropped[clause_id]) continue; -- clause_id; if(clause[clause_id].length == 1) { if(singleton == 1 + !negation) return 0; singleton = 1 + negation; } } for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id < 0) clause_id *= -1; -- clause[clause_id - 1].length; } int drop = 0; int result = 0; if(singleton == 0 || singleton == 1) { // Try x_n = T for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id < 0) continue; if(clause_dropped[clause_id]) continue; dropped_clauses.push_back(clause_id); clause_dropped[clause_id] = true; tree.remove(clause[clause_id - 1].start, clause[clause_id - 1].end); ++ drop; } tree.insert(best.first, best.first, -var.clauses.size()); result += solve(variables_set + 1); tree.insert(best.first, best.first, var.clauses.size()); for(int d = 0; d < drop; ++ d) { int clause_id = dropped_clauses.back(); dropped_clauses.pop_back(); clause_dropped[clause_id] = false; tree.add(clause[clause_id - 1].start, clause[clause_id - 1].end); } } if(singleton == 0 || singleton == 2) { // Try x_n = F drop = 0; for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id > 0) continue; clause_id *= -1; if(clause_dropped[clause_id]) continue; dropped_clauses.push_back(clause_id); clause_dropped[clause_id] = true; tree.remove(clause[clause_id - 1].start, clause[clause_id - 1].end); ++ drop; } tree.insert(best.first, best.first, -var.clauses.size()); result += solve(variables_set + 1); tree.insert(best.first, best.first, var.clauses.size()); if(result > MOD) result %= MOD; for(int d = 0; d < drop; ++ d) { int clause_id = dropped_clauses.back(); dropped_clauses.pop_back(); clause_dropped[clause_id] = false; tree.add(clause[clause_id - 1].start, clause[clause_id - 1].end); } } for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id < 0) clause_id *= -1; ++ clause[clause_id - 1].length; } return result; } inline iTree::iTree() :data() ,max() { for(int s = 0; s < TREE_SIZE; ++ s) max[s + TREE_SIZE].first = s; for(int s = TREE_SIZE - 1; s > 0; -- s) max[s] = max[s * 2 + (max[s * 2 + 1].second > max[s * 2].second ? 1 : 0)]; } inline void iTree::add(int start, int end) { insert(start, end, 1); } inline void iTree::remove(int start, int end) { insert(start, end, -1); } inline std::pair<int, int> iTree::get_max(void) const { return max[1]; } inline void iTree::insert(int start, int end, int value, int iStart, int iEnd, int iPos) { if(start == iStart && end == iEnd) { data[iPos] += value; max[iPos].second += value; propagate(iPos / 2); return; } int iMid = (iStart + iEnd) / 2; if(end <= iMid) insert(start, end, value, iStart, iMid, iPos * 2); else if(start > iMid) insert(start, end, value, iMid + 1, iEnd, iPos * 2 + 1); else { insert(start, iMid, value, iStart, iMid, iPos * 2); insert(iMid + 1, end, value, iMid + 1, iEnd, iPos * 2 + 1); } } inline void iTree::propagate(int iPos) { while(iPos > 0) { max[iPos] = max[iPos * 2 + (max[iPos * 2 + 1].second > max[iPos * 2].second ? 1 : 0)]; max[iPos].second += data[iPos]; iPos /= 2; } } void feed(const char *input) { enum Parser_state { CLAUSE_BEGIN, CLAUSE_END, VARIABLE_BEGIN, VARIABLE_ID, }; static Parser_state parser_state = CLAUSE_BEGIN; static int variable_id = 0; static bool negation = false; for(; *input; ++ input) { if(isspace(*input)) continue; switch(parser_state) { case CLAUSE_BEGIN: parser_state = VARIABLE_BEGIN; clauses[clause].start = MAX_VARIABLES; clauses[clause].end = 0; break; case VARIABLE_BEGIN: if(*input == '~') negation = true; else { parser_state = VARIABLE_ID; variable_id = 0; } break; case VARIABLE_ID: if('0' <= *input && *input <= '9') variable_id = variable_id * 10 + *input - '0'; else { -- variable_id; clause[clauses].start = std::min(clause[clauses].start, variable_id); clause[clauses].end = std::max(clause[clauses].end, variable_id); clause[clauses].length += 1; variable[variable_id].clauses.push_back((clauses + 1) * (negation ? -1 : 1)); negation = false; if(*input == 'v') parser_state = VARIABLE_BEGIN; else if(*input == ')') { parser_state = CLAUSE_END; tree.add(clause[clauses].start, clause[clauses].end); ++ clauses; } } break; case CLAUSE_END: parser_state = CLAUSE_BEGIN; break; } } }
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | /* 2016 * Maciej Szeptuch */ #include <cstdio> #include <vector> #include <cctype> const int MAX_BUFFER = 16384; const int MAX_VARIABLES = 1048576; const long long int MOD = 1000000007LL; struct Variable { std::vector<int> clauses; }; struct Clause { int start; int end; int length; }; class iTree { private: static const int TREE_SIZE = MAX_VARIABLES; int data[TREE_SIZE * 2]; std::pair<int, int> max[TREE_SIZE * 2]; public: iTree(); void add(int start, int end); void remove(int start, int end); void insert(int start, int end, int value, int iStart=0, int iEnd=TREE_SIZE-1, int iPos=1); std::pair<int, int> get_max(void) const; private: void propagate(int iPos); }; Clause clause[MAX_VARIABLES]; int clauses; iTree tree; Variable variable[MAX_VARIABLES]; char buffer[MAX_BUFFER]; int variables; long long int power_table[MAX_VARIABLES]; std::vector<int> dropped_clauses; bool clause_dropped[MAX_VARIABLES]; void feed(const char *input); long long int solve(int variables_set=0); int main(void) { scanf("%d", &variables); power_table[0] = 1; for(int v = 1; v < variables; ++ v) { power_table[v] = power_table[v - 1] * 2; if(power_table[v] > MOD) power_table[v] %= MOD; } while(scanf("%16383s", buffer) != -1) feed(buffer); printf("%lld\n", solve()); return 0; } long long int solve(int variables_set) { auto best = tree.get_max(); if(best.second == 0) return power_table[variables - variables_set]; Variable &var = variable[best.first]; char singleton = 0; for(auto clause_id: var.clauses) { bool negation = false; if(clause_id < 0) { negation = true; clause_id *= -1; } if(clause_dropped[clause_id]) continue; -- clause_id; if(clause[clause_id].length == 1) { if(singleton == 1 + !negation) return 0; singleton = 1 + negation; } } for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id < 0) clause_id *= -1; -- clause[clause_id - 1].length; } int drop = 0; int result = 0; if(singleton == 0 || singleton == 1) { // Try x_n = T for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id < 0) continue; if(clause_dropped[clause_id]) continue; dropped_clauses.push_back(clause_id); clause_dropped[clause_id] = true; tree.remove(clause[clause_id - 1].start, clause[clause_id - 1].end); ++ drop; } tree.insert(best.first, best.first, -var.clauses.size()); result += solve(variables_set + 1); tree.insert(best.first, best.first, var.clauses.size()); for(int d = 0; d < drop; ++ d) { int clause_id = dropped_clauses.back(); dropped_clauses.pop_back(); clause_dropped[clause_id] = false; tree.add(clause[clause_id - 1].start, clause[clause_id - 1].end); } } if(singleton == 0 || singleton == 2) { // Try x_n = F drop = 0; for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id > 0) continue; clause_id *= -1; if(clause_dropped[clause_id]) continue; dropped_clauses.push_back(clause_id); clause_dropped[clause_id] = true; tree.remove(clause[clause_id - 1].start, clause[clause_id - 1].end); ++ drop; } tree.insert(best.first, best.first, -var.clauses.size()); result += solve(variables_set + 1); tree.insert(best.first, best.first, var.clauses.size()); if(result > MOD) result %= MOD; for(int d = 0; d < drop; ++ d) { int clause_id = dropped_clauses.back(); dropped_clauses.pop_back(); clause_dropped[clause_id] = false; tree.add(clause[clause_id - 1].start, clause[clause_id - 1].end); } } for(int c = 0; c < (int) var.clauses.size(); ++ c) { int clause_id = var.clauses[c]; if(clause_id < 0) clause_id *= -1; ++ clause[clause_id - 1].length; } return result; } inline iTree::iTree() :data() ,max() { for(int s = 0; s < TREE_SIZE; ++ s) max[s + TREE_SIZE].first = s; for(int s = TREE_SIZE - 1; s > 0; -- s) max[s] = max[s * 2 + (max[s * 2 + 1].second > max[s * 2].second ? 1 : 0)]; } inline void iTree::add(int start, int end) { insert(start, end, 1); } inline void iTree::remove(int start, int end) { insert(start, end, -1); } inline std::pair<int, int> iTree::get_max(void) const { return max[1]; } inline void iTree::insert(int start, int end, int value, int iStart, int iEnd, int iPos) { if(start == iStart && end == iEnd) { data[iPos] += value; max[iPos].second += value; propagate(iPos / 2); return; } int iMid = (iStart + iEnd) / 2; if(end <= iMid) insert(start, end, value, iStart, iMid, iPos * 2); else if(start > iMid) insert(start, end, value, iMid + 1, iEnd, iPos * 2 + 1); else { insert(start, iMid, value, iStart, iMid, iPos * 2); insert(iMid + 1, end, value, iMid + 1, iEnd, iPos * 2 + 1); } } inline void iTree::propagate(int iPos) { while(iPos > 0) { max[iPos] = max[iPos * 2 + (max[iPos * 2 + 1].second > max[iPos * 2].second ? 1 : 0)]; max[iPos].second += data[iPos]; iPos /= 2; } } void feed(const char *input) { enum Parser_state { CLAUSE_BEGIN, CLAUSE_END, VARIABLE_BEGIN, VARIABLE_ID, }; static Parser_state parser_state = CLAUSE_BEGIN; static int variable_id = 0; static bool negation = false; for(; *input; ++ input) { if(isspace(*input)) continue; switch(parser_state) { case CLAUSE_BEGIN: parser_state = VARIABLE_BEGIN; clauses[clause].start = MAX_VARIABLES; clauses[clause].end = 0; break; case VARIABLE_BEGIN: if(*input == '~') negation = true; else { parser_state = VARIABLE_ID; variable_id = 0; } break; case VARIABLE_ID: if('0' <= *input && *input <= '9') variable_id = variable_id * 10 + *input - '0'; else { -- variable_id; clause[clauses].start = std::min(clause[clauses].start, variable_id); clause[clauses].end = std::max(clause[clauses].end, variable_id); clause[clauses].length += 1; variable[variable_id].clauses.push_back((clauses + 1) * (negation ? -1 : 1)); negation = false; if(*input == 'v') parser_state = VARIABLE_BEGIN; else if(*input == ')') { parser_state = CLAUSE_END; tree.add(clause[clauses].start, clause[clauses].end); ++ clauses; } } break; case CLAUSE_END: parser_state = CLAUSE_BEGIN; break; } } } |