import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * Created by konrad on 26.11.16. */ public class cnf { public static long MOD = 1_000_000_000 + 7; public static int extractLiteral(String literal) { int start = 1; int mul = 1; if (literal.startsWith("(")) { start = 2; } if (literal.charAt(start -1) == '~') { mul = -1; start ++; } int end = literal.length(); if (literal.endsWith(")")) { end --; } return mul * Integer.parseInt(literal.substring(start, end)); } public static Clause parseClause(StringTokenizer st) { Clause clause = new Clause(); String current = st.nextToken(); while (!current.endsWith(")")) { if ("v".equals(current) || "^".equals(current)) { current = st.nextToken(); continue; } clause.addLiteral(extractLiteral(current)); current = st.nextToken(); } clause.addLiteral(extractLiteral(current)); return clause; } public static class Clause { Set<Integer> literals = new HashSet<>(); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; public void addLiteral(int i) { int ai = Math.abs(i); max = Math.max(max, ai); min = Math.min(min, ai); literals.add(i); } public int size() { return literals.size(); } public boolean isSubsetOf(Clause other) { return other.min <= min && max <= other.max; } public boolean containsLiteral(int literal) { return literals.contains(literal); } public boolean makesTrue(int i) { return literals.contains(i); } public String toString() { ArrayList<Integer> tmp = new ArrayList<>(literals); Collections.sort(tmp); return tmp.toString(); } } static class Pair<T, P> { T fst; P snd; public Pair(T fst, P snd) { this.fst = fst; this.snd = snd; } public String toString() { return "(" + fst + "," + snd + ")"; } } static public class SatisfactionKeeper { HashSet<Integer> satisfiedClauses = new HashSet<>(); Stack<Pair<Integer, Integer>> history = new Stack<>(); void markSatisfied(int atLiteral, int clauseId) { satisfiedClauses.add(clauseId); history.add(new Pair<>(atLiteral, clauseId)); } void dropTillIncluding(int atLiteral) { while (history.size() >0 && history.peek().fst >= atLiteral) { satisfiedClauses.remove(history.pop().snd); } } boolean isSatisfied(int clauseId) { return satisfiedClauses.contains(clauseId); } public String toString() { return history.toString(); } } static ArrayList<Clause> clauses = new ArrayList<>(); static ArrayList<ArrayList<Integer>> literalToClauses = new ArrayList<>(); static SatisfactionKeeper satisfactionKeeper = new SatisfactionKeeper(); static int[] starts; static int n; static int N; static long pow2[]; private static int computeJump(int literalSetToTrue) { int literal = Math.abs(literalSetToTrue); int maxJump = literal + 1; for (Integer ci : literalToClauses.get(literal)) { Clause c = clauses.get(ci); if (c.makesTrue(literalSetToTrue) && !satisfactionKeeper.isSatisfied(ci)) { satisfactionKeeper.markSatisfied(literal, ci); } if (satisfactionKeeper.isSatisfied(ci)) { maxJump = Math.max(c.max + 1, maxJump); } else if (c.max == literal) { // System.out.println("NEG:" + literalSetToTrue + " " + c.literals); return 0; } else { maxJump = literal + 1; break; } } maxJump = Math.min(starts[literal], maxJump); // System.out.println("computed jump for: " + literalSetToTrue + " : " + maxJump); return maxJump; } private static long multShift(int jump, long val) { // System.out.println("Using shift: " + (jump - 1)); return (pow2[jump - 1] * val) % MOD; } private static long subSolve(int literalSetToTrue, ArrayList<Long> multiplies) { // System.out.println("subsolve: " + literalSetToTrue); int literal = Math.abs(literalSetToTrue); int maxJump = computeJump(literalSetToTrue); if (maxJump == 0) return 0; long subSolve = solve(maxJump, multiplies); long ret = multShift(maxJump - literal, subSolve); // System.out.println(subSolve + " ------ " + subSolve); return ret; } private static long solve(int literal, ArrayList<Long> multiplies) { // System.out.println("Solve for: " + literal + " : " + multiplies); // System.out.println(satisfactionKeeper); if (literal > n) { return 1; } if (multiplies.get(literal) == -1) { long positiveCase = subSolve(literal, multiplies); satisfactionKeeper.dropTillIncluding(literal); long negativeCase = subSolve(-literal, multiplies); satisfactionKeeper.dropTillIncluding(literal); long multiplier = (positiveCase + negativeCase) % MOD; multiplies.set(literal, multiplier); } // System.out.println(literal + " : " + multiplies); return multiplies.get(literal); } public static void computeStarts() { starts = new int[N]; for (Clause c: clauses) { starts[c.min] = 1; } int lastStart = Integer.MAX_VALUE; for (int i=n; i>0;i--) { int val = starts[i]; starts[i] = lastStart; if (val == 1) { lastStart = i; } } // System.out.println("STARTS" + Arrays.toString(starts)); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); n = Integer.parseInt(line); N = n +1; pow2 = new long[N]; pow2[0] = 1; for (int i=1;i<N;i++) { pow2[i] = (pow2[i-1] << 1) % MOD; } line = br.readLine(); StringTokenizer st = new StringTokenizer(line); for (int i=0; i < N; i++) { literalToClauses.add(new ArrayList<>()); } while (st.hasMoreTokens()) { Clause clause = parseClause(st); int m = clauses.size(); clauses.add(clause); for (Integer literal : clause.literals) { ArrayList<Integer> literalClauses = literalToClauses.get(Math.abs(literal)); literalClauses.add(m); } } computeStarts(); ArrayList<Long> multipliers = new ArrayList<>(N); for (int i=0; i< N;i++) { multipliers.add(-1L); } System.out.println(solve(1, multipliers)); // long baseMultiplier = 1; // ArrayList<Integer> currentLiterals = new ArrayList<>(); // for (int i=1;i<=n;i++) { // ArrayList<Integer> iClauses = literalToClauses.get(i); // // multiply on non-present literals // if (iClauses.isEmpty()) { // baseMultiplier = (baseMultiplier << 1) % MOD; // continue; // } // // remove single literals // int pos = 0; // int neg = 0; // for (Integer c : iClauses) { // Clause cl = clauses.get(c); // if (cl.size() > 1) { // currentLiterals.add(i); // continue; // } // if (cl.makesTrue(i)) { // pos ++; // } else { // neg++; // } // } // if (neg > 0 && pos > 0) { // System.out.println(0); // System.exit(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 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 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** * Created by konrad on 26.11.16. */ public class cnf { public static long MOD = 1_000_000_000 + 7; public static int extractLiteral(String literal) { int start = 1; int mul = 1; if (literal.startsWith("(")) { start = 2; } if (literal.charAt(start -1) == '~') { mul = -1; start ++; } int end = literal.length(); if (literal.endsWith(")")) { end --; } return mul * Integer.parseInt(literal.substring(start, end)); } public static Clause parseClause(StringTokenizer st) { Clause clause = new Clause(); String current = st.nextToken(); while (!current.endsWith(")")) { if ("v".equals(current) || "^".equals(current)) { current = st.nextToken(); continue; } clause.addLiteral(extractLiteral(current)); current = st.nextToken(); } clause.addLiteral(extractLiteral(current)); return clause; } public static class Clause { Set<Integer> literals = new HashSet<>(); int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; public void addLiteral(int i) { int ai = Math.abs(i); max = Math.max(max, ai); min = Math.min(min, ai); literals.add(i); } public int size() { return literals.size(); } public boolean isSubsetOf(Clause other) { return other.min <= min && max <= other.max; } public boolean containsLiteral(int literal) { return literals.contains(literal); } public boolean makesTrue(int i) { return literals.contains(i); } public String toString() { ArrayList<Integer> tmp = new ArrayList<>(literals); Collections.sort(tmp); return tmp.toString(); } } static class Pair<T, P> { T fst; P snd; public Pair(T fst, P snd) { this.fst = fst; this.snd = snd; } public String toString() { return "(" + fst + "," + snd + ")"; } } static public class SatisfactionKeeper { HashSet<Integer> satisfiedClauses = new HashSet<>(); Stack<Pair<Integer, Integer>> history = new Stack<>(); void markSatisfied(int atLiteral, int clauseId) { satisfiedClauses.add(clauseId); history.add(new Pair<>(atLiteral, clauseId)); } void dropTillIncluding(int atLiteral) { while (history.size() >0 && history.peek().fst >= atLiteral) { satisfiedClauses.remove(history.pop().snd); } } boolean isSatisfied(int clauseId) { return satisfiedClauses.contains(clauseId); } public String toString() { return history.toString(); } } static ArrayList<Clause> clauses = new ArrayList<>(); static ArrayList<ArrayList<Integer>> literalToClauses = new ArrayList<>(); static SatisfactionKeeper satisfactionKeeper = new SatisfactionKeeper(); static int[] starts; static int n; static int N; static long pow2[]; private static int computeJump(int literalSetToTrue) { int literal = Math.abs(literalSetToTrue); int maxJump = literal + 1; for (Integer ci : literalToClauses.get(literal)) { Clause c = clauses.get(ci); if (c.makesTrue(literalSetToTrue) && !satisfactionKeeper.isSatisfied(ci)) { satisfactionKeeper.markSatisfied(literal, ci); } if (satisfactionKeeper.isSatisfied(ci)) { maxJump = Math.max(c.max + 1, maxJump); } else if (c.max == literal) { // System.out.println("NEG:" + literalSetToTrue + " " + c.literals); return 0; } else { maxJump = literal + 1; break; } } maxJump = Math.min(starts[literal], maxJump); // System.out.println("computed jump for: " + literalSetToTrue + " : " + maxJump); return maxJump; } private static long multShift(int jump, long val) { // System.out.println("Using shift: " + (jump - 1)); return (pow2[jump - 1] * val) % MOD; } private static long subSolve(int literalSetToTrue, ArrayList<Long> multiplies) { // System.out.println("subsolve: " + literalSetToTrue); int literal = Math.abs(literalSetToTrue); int maxJump = computeJump(literalSetToTrue); if (maxJump == 0) return 0; long subSolve = solve(maxJump, multiplies); long ret = multShift(maxJump - literal, subSolve); // System.out.println(subSolve + " ------ " + subSolve); return ret; } private static long solve(int literal, ArrayList<Long> multiplies) { // System.out.println("Solve for: " + literal + " : " + multiplies); // System.out.println(satisfactionKeeper); if (literal > n) { return 1; } if (multiplies.get(literal) == -1) { long positiveCase = subSolve(literal, multiplies); satisfactionKeeper.dropTillIncluding(literal); long negativeCase = subSolve(-literal, multiplies); satisfactionKeeper.dropTillIncluding(literal); long multiplier = (positiveCase + negativeCase) % MOD; multiplies.set(literal, multiplier); } // System.out.println(literal + " : " + multiplies); return multiplies.get(literal); } public static void computeStarts() { starts = new int[N]; for (Clause c: clauses) { starts[c.min] = 1; } int lastStart = Integer.MAX_VALUE; for (int i=n; i>0;i--) { int val = starts[i]; starts[i] = lastStart; if (val == 1) { lastStart = i; } } // System.out.println("STARTS" + Arrays.toString(starts)); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); n = Integer.parseInt(line); N = n +1; pow2 = new long[N]; pow2[0] = 1; for (int i=1;i<N;i++) { pow2[i] = (pow2[i-1] << 1) % MOD; } line = br.readLine(); StringTokenizer st = new StringTokenizer(line); for (int i=0; i < N; i++) { literalToClauses.add(new ArrayList<>()); } while (st.hasMoreTokens()) { Clause clause = parseClause(st); int m = clauses.size(); clauses.add(clause); for (Integer literal : clause.literals) { ArrayList<Integer> literalClauses = literalToClauses.get(Math.abs(literal)); literalClauses.add(m); } } computeStarts(); ArrayList<Long> multipliers = new ArrayList<>(N); for (int i=0; i< N;i++) { multipliers.add(-1L); } System.out.println(solve(1, multipliers)); // long baseMultiplier = 1; // ArrayList<Integer> currentLiterals = new ArrayList<>(); // for (int i=1;i<=n;i++) { // ArrayList<Integer> iClauses = literalToClauses.get(i); // // multiply on non-present literals // if (iClauses.isEmpty()) { // baseMultiplier = (baseMultiplier << 1) % MOD; // continue; // } // // remove single literals // int pos = 0; // int neg = 0; // for (Integer c : iClauses) { // Clause cl = clauses.get(c); // if (cl.size() > 1) { // currentLiterals.add(i); // continue; // } // if (cl.makesTrue(i)) { // pos ++; // } else { // neg++; // } // } // if (neg > 0 && pos > 0) { // System.out.println(0); // System.exit(0); // } // } } } |