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
|
#include <ios>
#include <iostream>
#include <vector>
using namespace std;
struct ConvertILPToSat {
vector< vector<int> > A;
vector<int> b;
ConvertILPToSat(int n, int m) : A(n, vector<int>(m)), b(n)
{}
void printEquisatisfiableSatFormula() {
int vars = A[0].size();
int eqs = A.size();
vector<vector<int>> clauses;
// 5x1 + 2x2 + 3x3 ≤ 6
// vector that are no solutions : (1,1,1)=10, (1,1,0)=7, (1,0,1)=8
// corresponding clauses (!x || !y || !z), (!x || !y || z), (!x || y || !z)
vector<int> coefficients(vars, -1);
for (int i = 0; i < eqs; i++) {
vector<int> equ = A[i];
// collect non 0 coefficients
coefficients.clear();
for (int j = 0; j < vars; j++) {
if (equ[j] != 0) coefficients.push_back(j);
}
vector<int> clause;
// for each bit combinaison
for (int j = 0; j < (1<<coefficients.size()); j++) {
// sum the coefficients using the bit map
int v = 0;
clause.clear();
for (int k = 0; k < coefficients.size(); k++) {
bool b = ((j & (1<<k)) > 0);
int w = coefficients[k];
if (b) v += equ[w];
w += 1;
clause.push_back(b ? -w: w);
}
if (v > b[i]) {
clauses.push_back(clause);
}
}
}
int n = clauses.size();
if (n == 0) {
cout << "1 1\n1 -1 0"<< endl;
} else {
cout << clauses.size() << " " << vars << endl;
for (vector<int>& c : clauses) {
for (int v : c) {
cout << v << " ";
}
cout << "0" << endl;
}
cout << endl;
}
}
};
int main() {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
ConvertILPToSat converter(n, m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> converter.A[i][j];
}
}
for (int i = 0; i < n; i++) {
cin >> converter.b[i];
}
converter.printEquisatisfiableSatFormula();
return 0;
}
|