/* Quadratic equation solver */ /* Last edited on 2007-01-04 00:23:27 by stolfi */ #include #include typedef struct Eqn { int n; /* Number of variables. */ long int *c; /* Coefficient matrix, packed: {c[i,j] = c[i+j*(j+1)/2]}. */ long int *m; /* Variable {x[i]} has range {[0..m[i]]} for {i>0}, {[1..1]} for {i=0}. */ } Eqn; typedef struct Sol { int n; /* Number of variables. */ long int x; /* Variable values. */ } Sol; Sol *solve(Eqn *eq); /* Tries to find a solution {so} to the equation {eq}, where {so.x[0] = 1} and each {so.x[i]}, for {i > 0}, ranges in {[0..eq.b[i]}. The equation is {SUM{eq.c[i,j]*x[i]*x[j] : 0 \leq i \leq j < n}}. Returns NULL if {eq} has no solution. */ Sol *solve(Eqn *eq) { Sol *so; if ((so = trivial_solution(eq)) != NULL) { return so; } else { solve(reduce(... /* Reduce the equation modulo 2. */ /* Express solutions in terms of new binary variables. */ ... } bool_t solve_mod_2(Eqn *eq, Sol *so) /* Finds a 0-1 solution to the question {eq}, interpreted modulo 2. */ { /* Enqueue all equations as having uncertain status. */ while (/* queue is not empty */) { /* remove an equation from the queue. */ /* Check all } void main(int argc, char **argv) {