import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Set; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class cnf { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CNFSAT solver = new CNFSAT(); solver.solve(1, in, out); out.close(); } static class CNFSAT { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); CNFSAT.Clause[] claues = parseClauses(in); // System.out.println(Arrays.toString(claues)); int ans = solveBrute(N, claues); out.println(ans); } private int solveBrute(int N, CNFSAT.Clause[] claues) { int M = claues.length; int ans = 0; boolean vars[] = new boolean[N]; for (int mask = 0; mask < 1 << N; mask++) { for (int bit = 0; bit < N; bit++) { if ((mask & (1 << bit)) > 0) { vars[bit] = true; } else { vars[bit] = false; } } if (valid(claues, vars)) ans++; } return ans; } private boolean valid(CNFSAT.Clause[] claues, boolean[] vars) { for (CNFSAT.Clause c : claues) { int ans = 0; for (CNFSAT.Variable v : c.variables) { if (!(v.negated && vars[v.idx] || !v.negated && !vars[v.idx])) { ans++; } } if (ans == 0) return false; } return true; } private CNFSAT.Clause[] parseClauses(InputReader in) { CNFSAT.Clause[] claues; try { String input = in.reader.readLine(); String[] cls = input.split("\\^"); claues = new CNFSAT.Clause[cls.length]; for (int i = 0; i < cls.length; i++) { claues[i] = new CNFSAT.Clause(cls[i]); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } return claues; } private static class Clause { private CNFSAT.Variable[] variables; private Set<CNFSAT.Variable> varIds = new TreeSet<>(); public Clause(String clStr) { clStr = clStr.trim(); clStr = clStr.substring(1, clStr.length() - 1); String[] vars = clStr.split("v"); variables = new CNFSAT.Variable[vars.length]; for (int i = 0; i < vars.length; i++) { variables[i] = new CNFSAT.Variable(vars[i]); varIds.add(variables[i]); } } public String toString() { return "Clause{" + "variables=" + Arrays.toString(variables) + '}'; } } private static class Variable implements Comparable<CNFSAT.Variable> { private int idx; private boolean negated; public Variable(String var) { var = var.trim(); if (var.charAt(0) == '~') { negated = true; var = var.substring(2); } else { var = var.substring(1); } idx = Integer.parseInt(var) - 1; } public String toString() { return "Variable{" + "idx=" + idx + ", negated=" + negated + '}'; } public int compareTo(CNFSAT.Variable that) { if (this.idx == that.idx) { if (this.negated) return +1; if (!this.negated) return -1; return 0; } return this.idx - that.idx; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
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 | import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Set; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class cnf { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CNFSAT solver = new CNFSAT(); solver.solve(1, in, out); out.close(); } static class CNFSAT { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); CNFSAT.Clause[] claues = parseClauses(in); // System.out.println(Arrays.toString(claues)); int ans = solveBrute(N, claues); out.println(ans); } private int solveBrute(int N, CNFSAT.Clause[] claues) { int M = claues.length; int ans = 0; boolean vars[] = new boolean[N]; for (int mask = 0; mask < 1 << N; mask++) { for (int bit = 0; bit < N; bit++) { if ((mask & (1 << bit)) > 0) { vars[bit] = true; } else { vars[bit] = false; } } if (valid(claues, vars)) ans++; } return ans; } private boolean valid(CNFSAT.Clause[] claues, boolean[] vars) { for (CNFSAT.Clause c : claues) { int ans = 0; for (CNFSAT.Variable v : c.variables) { if (!(v.negated && vars[v.idx] || !v.negated && !vars[v.idx])) { ans++; } } if (ans == 0) return false; } return true; } private CNFSAT.Clause[] parseClauses(InputReader in) { CNFSAT.Clause[] claues; try { String input = in.reader.readLine(); String[] cls = input.split("\\^"); claues = new CNFSAT.Clause[cls.length]; for (int i = 0; i < cls.length; i++) { claues[i] = new CNFSAT.Clause(cls[i]); } } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } return claues; } private static class Clause { private CNFSAT.Variable[] variables; private Set<CNFSAT.Variable> varIds = new TreeSet<>(); public Clause(String clStr) { clStr = clStr.trim(); clStr = clStr.substring(1, clStr.length() - 1); String[] vars = clStr.split("v"); variables = new CNFSAT.Variable[vars.length]; for (int i = 0; i < vars.length; i++) { variables[i] = new CNFSAT.Variable(vars[i]); varIds.add(variables[i]); } } public String toString() { return "Clause{" + "variables=" + Arrays.toString(variables) + '}'; } } private static class Variable implements Comparable<CNFSAT.Variable> { private int idx; private boolean negated; public Variable(String var) { var = var.trim(); if (var.charAt(0) == '~') { negated = true; var = var.substring(2); } else { var = var.substring(1); } idx = Integer.parseInt(var) - 1; } public String toString() { return "Variable{" + "idx=" + idx + ", negated=" + negated + '}'; } public int compareTo(CNFSAT.Variable that) { if (this.idx == that.idx) { if (this.negated) return +1; if (!this.negated) return -1; return 0; } return this.idx - that.idx; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } } |