import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class cnf { static class Clause { int from; int to; int mask; public Clause(int from, int to, int mask) { this.from = from; this.to = to; this.mask = mask; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); if (n > 32) { System.out.println("0"); return; } String s = sc.nextLine(); int i = -1; List<Clause> clauses = new ArrayList<>(); while (i < s.length()) { i++; // '(' int minLiteral = 32; int maxLiteral = 0; int mask = 0; while (true) { int literal; i++; char c = s.charAt(i); if (c == '~') { i++; i++; literal = s.charAt(i) - '0'; i++; if (Character.isDigit(s.charAt(i))) { literal *= 10; literal += s.charAt(i) - '0'; i++; } literal--; mask |= (1 << literal); } else { i++; literal = s.charAt(i) - '0'; i++; if (Character.isDigit(s.charAt(i))) { literal *= 10; literal += s.charAt(i) - '0'; i++; } literal--; } minLiteral = Math.min(minLiteral, literal); maxLiteral = Math.max(maxLiteral, literal); if (s.charAt(i) == ')') { clauses.add(new Clause(minLiteral, maxLiteral, mask)); break; } i++; // 'v' i++; // space } if (i < s.length()) { i++; // space i++; // ^ i++; // space } } int result = 1 << n; for (int x = 0; x < (1 << n); x++) { for (Clause c : clauses) { boolean ok = true; for (int j = c.from; j <= c.to && ok; j++) { if ((x & (1 << j)) != (c.mask & (1 << j))) { ok = false; } } if (ok) { result--; break; } } } System.out.println(result); } }
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 | import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class cnf { static class Clause { int from; int to; int mask; public Clause(int from, int to, int mask) { this.from = from; this.to = to; this.mask = mask; } } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.nextLine()); if (n > 32) { System.out.println("0"); return; } String s = sc.nextLine(); int i = -1; List<Clause> clauses = new ArrayList<>(); while (i < s.length()) { i++; // '(' int minLiteral = 32; int maxLiteral = 0; int mask = 0; while (true) { int literal; i++; char c = s.charAt(i); if (c == '~') { i++; i++; literal = s.charAt(i) - '0'; i++; if (Character.isDigit(s.charAt(i))) { literal *= 10; literal += s.charAt(i) - '0'; i++; } literal--; mask |= (1 << literal); } else { i++; literal = s.charAt(i) - '0'; i++; if (Character.isDigit(s.charAt(i))) { literal *= 10; literal += s.charAt(i) - '0'; i++; } literal--; } minLiteral = Math.min(minLiteral, literal); maxLiteral = Math.max(maxLiteral, literal); if (s.charAt(i) == ')') { clauses.add(new Clause(minLiteral, maxLiteral, mask)); break; } i++; // 'v' i++; // space } if (i < s.length()) { i++; // space i++; // ^ i++; // space } } int result = 1 << n; for (int x = 0; x < (1 << n); x++) { for (Clause c : clauses) { boolean ok = true; for (int j = c.from; j <= c.to && ok; j++) { if ((x & (1 << j)) != (c.mask & (1 << j))) { ok = false; } } if (ok) { result--; break; } } } System.out.println(result); } } |