import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigInteger; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class cnf { static String readInput() throws IOException { BufferedInputStream bis = new BufferedInputStream(System.in); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while (result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString(); } static class Formula { Set<Set<Integer>> clauses = new HashSet<>(); Set<Integer> hiddenVars = new HashSet<>(); public Formula() { this.hiddenVars = new HashSet<>(); } public Formula(Set<Integer> hiddenVars) { this.hiddenVars = hiddenVars; } Set<Integer> allVars() { Set<Integer> all = new HashSet<>(); for (Set<Integer> clause : clauses) { all.addAll(clause.stream().map(Math::abs).collect(Collectors.toSet())); } return all; } @Override public String toString() { return clauses + ":" + hiddenVars; } } static Formula parseFormula(String input) { Pattern formulaPattern = Pattern.compile("\\(([^\\(\\)]+)\\)"); Pattern clausePattern = Pattern.compile("~?x\\d+"); Matcher formulaMatcher = formulaPattern.matcher(input); Formula formula = new Formula(); while (formulaMatcher.find()) { Matcher clauseMatcher = clausePattern.matcher(formulaMatcher.group(1)); Set<Integer> clause = new HashSet<>(); while (clauseMatcher.find()) { String token = clauseMatcher.group(0); String[] tArray = token.split("x"); int variable = Integer.parseInt(tArray[tArray.length - 1]) - 1; if (tArray[0].equals("~")) variable = -variable; clause.add(variable); } formula.clauses.add(clause); } return formula; } static Formula simplify(Formula formula) { boolean modified = false; Formula simplified = new Formula(formula.hiddenVars); for (Set<Integer> clause : formula.clauses) { if (clause.size() == 1) { int var = clause.iterator().next(); simplified.hiddenVars.remove(Math.abs(var)); for (Set<Integer> c2 : formula.clauses) { if (c2.contains(var)) { simplified.hiddenVars.addAll(c2.stream().map(Math::abs).collect(Collectors.toSet())); simplified.hiddenVars.remove(Math.abs(var)); modified = true; } else if (c2.contains(-var)) { Set<Integer> moved = new HashSet<>(c2); moved.remove(-var); simplified.clauses.add(moved); modified = true; } else { simplified.clauses.add(c2); } } break; } } if (!modified) simplified = formula; Iterator<Set<Integer>> it = simplified.clauses.iterator(); while (it.hasNext()) { if (it.next().isEmpty()) it.remove(); } if (modified) { return simplify(simplified); } else { formula.hiddenVars.removeAll(formula.allVars()); return simplified; } } static Formula normalize(Formula formula) { Map<Integer, Integer> m = new HashMap<>(); int next = 0; for (Set<Integer> c : formula.clauses) { for (int v : c) { if (!m.keySet().contains(Math.abs(v))) m.put(Math.abs(v), next++); } } Formula normalized = new Formula(); for (Set<Integer> c : formula.clauses) { Set<Integer> newClause = new HashSet<>(); for (int v : c) { boolean positive = v > 0; int mapped = m.get(Math.abs(v)); newClause.add(positive ? mapped : -mapped); } normalized.clauses.add(newClause); } normalized.hiddenVars = formula.hiddenVars; return normalized; } static boolean eval(Formula formula, BigInteger values) { boolean formulaEval = true; for (Set<Integer> clause : formula.clauses) { boolean clauseEval = false; for (int var : clause) { boolean negate = var < 0; var = Math.abs(var); boolean varEval = values.testBit(var); if (negate) varEval = !varEval; clauseEval |= varEval; } formulaEval &= clauseEval; } return formulaEval; } static int countTrueConfigurations(Formula formula) { BigInteger loopBoundary = BigInteger.valueOf(2).pow(formula.allVars().size()); BigInteger i = BigInteger.ZERO; BigInteger counter = BigInteger.ZERO; while (i.compareTo(loopBoundary) < 0) { if (eval(formula, i)) counter = counter.add(BigInteger.ONE); i = i.add(BigInteger.ONE); } counter = counter.multiply(BigInteger.valueOf(2).pow(formula.hiddenVars.size())); counter = counter.mod(BigInteger.valueOf(1000000007)); return counter.intValue(); } public static void main(String[] args) throws IOException { String[] input = readInput().split("\n"); // System.out.println(Arrays.toString(input)); Formula formula = parseFormula(input[1]); // System.out.println(formula); formula = normalize(simplify(formula)); // System.out.println(formula); System.out.println(countTrueConfigurations(formula)); } }
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 | import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.math.BigInteger; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; public class cnf { static String readInput() throws IOException { BufferedInputStream bis = new BufferedInputStream(System.in); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while (result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString(); } static class Formula { Set<Set<Integer>> clauses = new HashSet<>(); Set<Integer> hiddenVars = new HashSet<>(); public Formula() { this.hiddenVars = new HashSet<>(); } public Formula(Set<Integer> hiddenVars) { this.hiddenVars = hiddenVars; } Set<Integer> allVars() { Set<Integer> all = new HashSet<>(); for (Set<Integer> clause : clauses) { all.addAll(clause.stream().map(Math::abs).collect(Collectors.toSet())); } return all; } @Override public String toString() { return clauses + ":" + hiddenVars; } } static Formula parseFormula(String input) { Pattern formulaPattern = Pattern.compile("\\(([^\\(\\)]+)\\)"); Pattern clausePattern = Pattern.compile("~?x\\d+"); Matcher formulaMatcher = formulaPattern.matcher(input); Formula formula = new Formula(); while (formulaMatcher.find()) { Matcher clauseMatcher = clausePattern.matcher(formulaMatcher.group(1)); Set<Integer> clause = new HashSet<>(); while (clauseMatcher.find()) { String token = clauseMatcher.group(0); String[] tArray = token.split("x"); int variable = Integer.parseInt(tArray[tArray.length - 1]) - 1; if (tArray[0].equals("~")) variable = -variable; clause.add(variable); } formula.clauses.add(clause); } return formula; } static Formula simplify(Formula formula) { boolean modified = false; Formula simplified = new Formula(formula.hiddenVars); for (Set<Integer> clause : formula.clauses) { if (clause.size() == 1) { int var = clause.iterator().next(); simplified.hiddenVars.remove(Math.abs(var)); for (Set<Integer> c2 : formula.clauses) { if (c2.contains(var)) { simplified.hiddenVars.addAll(c2.stream().map(Math::abs).collect(Collectors.toSet())); simplified.hiddenVars.remove(Math.abs(var)); modified = true; } else if (c2.contains(-var)) { Set<Integer> moved = new HashSet<>(c2); moved.remove(-var); simplified.clauses.add(moved); modified = true; } else { simplified.clauses.add(c2); } } break; } } if (!modified) simplified = formula; Iterator<Set<Integer>> it = simplified.clauses.iterator(); while (it.hasNext()) { if (it.next().isEmpty()) it.remove(); } if (modified) { return simplify(simplified); } else { formula.hiddenVars.removeAll(formula.allVars()); return simplified; } } static Formula normalize(Formula formula) { Map<Integer, Integer> m = new HashMap<>(); int next = 0; for (Set<Integer> c : formula.clauses) { for (int v : c) { if (!m.keySet().contains(Math.abs(v))) m.put(Math.abs(v), next++); } } Formula normalized = new Formula(); for (Set<Integer> c : formula.clauses) { Set<Integer> newClause = new HashSet<>(); for (int v : c) { boolean positive = v > 0; int mapped = m.get(Math.abs(v)); newClause.add(positive ? mapped : -mapped); } normalized.clauses.add(newClause); } normalized.hiddenVars = formula.hiddenVars; return normalized; } static boolean eval(Formula formula, BigInteger values) { boolean formulaEval = true; for (Set<Integer> clause : formula.clauses) { boolean clauseEval = false; for (int var : clause) { boolean negate = var < 0; var = Math.abs(var); boolean varEval = values.testBit(var); if (negate) varEval = !varEval; clauseEval |= varEval; } formulaEval &= clauseEval; } return formulaEval; } static int countTrueConfigurations(Formula formula) { BigInteger loopBoundary = BigInteger.valueOf(2).pow(formula.allVars().size()); BigInteger i = BigInteger.ZERO; BigInteger counter = BigInteger.ZERO; while (i.compareTo(loopBoundary) < 0) { if (eval(formula, i)) counter = counter.add(BigInteger.ONE); i = i.add(BigInteger.ONE); } counter = counter.multiply(BigInteger.valueOf(2).pow(formula.hiddenVars.size())); counter = counter.mod(BigInteger.valueOf(1000000007)); return counter.intValue(); } public static void main(String[] args) throws IOException { String[] input = readInput().split("\n"); // System.out.println(Arrays.toString(input)); Formula formula = parseFormula(input[1]); // System.out.println(formula); formula = normalize(simplify(formula)); // System.out.println(formula); System.out.println(countTrueConfigurations(formula)); } } |