import org.gnu.glpk.*; public class A379759 { public final static int TERMS_SOUGHT = 100; public final static int MIN_COVERING = 3; public final static int MAX_NEIGHBORS = 9; public final static boolean VERBOSE = true; public static void main(String[] args) { for (int n = 2; n < 2+TERMS_SOUGHT; n++) System.out.println(a(n)); } public static int a(int n) { GLPK.glp_term_out(GLPK.GLP_OFF); glp_prob lp = GLPK.glp_create_prob(); GLPK.glp_add_cols(lp, n*n); for (int col = 1; col <= n*n; col++) { GLPK.glp_set_col_name(lp, col, "col" + col); GLPK.glp_set_col_kind(lp, col, GLPKConstants.GLP_BV); GLPK.glp_set_col_bnds(lp, col, GLPKConstants.GLP_DB, 0, 1); } GLPK.glp_add_rows(lp, n*n); for (int row = 1; row <= n*n; row++) { GLPK.glp_set_row_name(lp, row, "row" + row); GLPK.glp_set_row_bnds(lp, row, GLPKConstants.GLP_LO, MIN_COVERING, MAX_NEIGHBORS); SWIGTYPE_p_int ind = GLPK.new_intArray((n*n) + 1); SWIGTYPE_p_double val = GLPK.new_doubleArray((n*n) + 1); for (int col = 1; col <= n*n; col++) { GLPK.intArray_setitem(ind, col, col); GLPK.doubleArray_setitem(val, col, areNeighbors(row, col, n) ? 1 : 0); } GLPK.glp_set_mat_row(lp, row, n*n, ind, val); GLPK.delete_intArray(ind); GLPK.delete_doubleArray(val); } GLPK.glp_set_obj_name(lp, "obj"); GLPK.glp_set_obj_dir(lp, GLPKConstants.GLP_MIN); for (int col = 1; col <= n*n; col++) { GLPK.glp_set_obj_coef(lp, col, 1); } glp_iocp iocp = new glp_iocp(); GLPK.glp_init_iocp(iocp); iocp.setMsg_lev(GLPKConstants.GLP_JAVA_MSG_LVL_OFF ); iocp.setPresolve(GLPKConstants.GLP_ON); GLPK.glp_intopt(lp, iocp); int status = GLPK.glp_mip_status(lp); int sum = 0; if (status == GLPKConstants.GLP_OPT) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double value = GLPK.glp_mip_col_val(lp, (j+(i*n))+1); if (value == 1.0) { sum++; if (VERBOSE) { System.out.print('k'); } } else { if (VERBOSE) { System.out.print('o'); } } } if (VERBOSE) { System.out.println(); } } } else { System.out.println("Error"); } GLPK.glp_delete_prob(lp); return sum; } public static boolean areNeighbors(int k, int j, int n) { int dx = Math.abs(((k-1) % n) - ((j-1) % n)); int dy = Math.abs(((k-1) / n) - ((j-1) / n)); return dx <= 1 && dy <= 1; } }