int main(void) { unsigned long i; unsigned j; unsigned index, count; unsigned long permutations; /* maximum of 2^32 - 2*/ unsigned T = 1u, N, J; unsigned long long divisors[9]; char jamcoin_str[32 + 1]; uint32_t jamcoin; (void)getchar(); /* consume T */ (void)getchar(); /* consume newline */ scanf("%u %u", &N, &J); puts("Case #1:"); jamcoin = 1u; /* jamcoins must end with 1 */ jamcoin |= 1u << (N - 1); /* jamcoins must begin with 1 */ permutations = 1u << (N - 2u); /* 2^(N - 2), -2 as first and last are fixed */ count = 0u; for (i = 0u; i < permutations; i++) { for (j = 2u; j <= 10u; j++) { index = j - 2u; divisors[index] = getdivisor(tobase10(jamcoin, j)); if (0u == divisors[index]) break; /* inner for loop */ } if (j < 10u || divisors[8] == 0u) { jamcoin += 2; /* 2 is 10 in binary */ continue; /* outer for loop */ } else { (void)bintostr(jamcoin, sizeof jamcoin_str, jamcoin_str); printf("%s ", jamcoin_str); for (j = 0u; j < 8u; j++) printf("%llu ", divisors[j]); printf("%llu\n", divisors[8]); count++; if (count == J) break; /* outer for loop */ jamcoin += 2; } } return 0; }
// Constructs a DFA for the inequation coeffs*variables+constant<0 // in two's complement arithmetic DFA *build_DFA_ineq_2sc(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i; unsigned long j, transitions; struct map_ent *map; char *statuces; DFA *inequality, *temp; //int write1, overbits, label1, label2, co; int write1, label1, label2, co; preprocess(vars, coeffs, &constant, 1); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = max - min + 1; //overbits= ceil(log(states)/log(2)); states *= 2; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].sr = 0; map[i].i = -1; map[i].ir = -1; } map[constant].sr = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = -1; map[constant].ir = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states, vars, indices); while (next_label < max + 1) { //there is a state to expand (excuding sink) if (map[next_label].i == count) map[next_label].s = 2; else map[next_label].sr = 2; dfaAllocExceptions(transitions); for (j = 0; j < transitions; j++) { co = count_ones(j, vars, coeffs); result = next_label + co; if (result >= 0) target = result / 2; else target = (result - 1) / 2; write1 = result & 1; label1 = next_label; label2 = target; while (label1 != label2) { label1 = label2; result = label1 + co; if (result >= 0) label2 = result / 2; else label2 = (result - 1) / 2; write1 = result & 1; } if (write1) { if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i, bintostr(j, vars)); } else { if (map[target].sr == 0) { map[target].sr = 1; next_index++; map[target].ir = next_index; } dfaStoreException(map[target].ir, bintostr(j, vars)); } } dfaStoreState(count); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count) && (map[next_label].ir != count); next_label++) ; //find next state to expand } for (i = count; i < states; i++) { dfaAllocExceptions(0); dfaStoreState(i); } //define accepting and rejecting states statuces = (char *) malloc(states + 1); for (i = 0; i < states; i++) statuces[i] = '-'; for (next_label = min; next_label <= max; next_label++) if (map[next_label].s == 2) statuces[map[next_label].i] = '+'; statuces[states] = '\0'; temp = dfaBuild(statuces); temp->ns -= states - count; inequality = dfaMinimize(temp); return inequality; }
//Constructs a DFA for the inequation coeffs*variables+constant<0 DFA *build_DFA_ineq_new(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i, transitions; unsigned long j; struct map_ent *map; char *statuces; DFA *inequality, *temp; preprocess(vars, coeffs, &constant, 1); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = max - min + 1; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].i = -1; } map[constant].s = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states + 1, vars, indices); dfaAllocExceptions(0); dfaStoreState(1); //count++; while (next_label < max + 1) { //there is a state to expand map[next_label].s = 2; dfaAllocExceptions(transitions); for (j = 0; j < transitions; j++) { result = next_label + count_ones(j, vars, coeffs); if (result >= 0) target = result / 2; else target = (result - 1) / 2; if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i + 1, bintostr(j, vars)); } dfaStoreState(count); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count); next_label++) ; //find next state to expand } for (i = count; i <= states; i++) { dfaAllocExceptions(0); dfaStoreState(i); } //define accepting and rejecting states statuces = (char *) malloc(states + 2); for (i = 0; i <= count; i++) statuces[i] = '-'; for (; i <= states; i++) statuces[i] = '0'; for (i = min; i < 0; i++) if (map[i].s == 2) statuces[map[i].i + 1] = '+'; statuces[states + 1] = '\0'; temp = dfaBuild(statuces); temp->ns -= states - count; inequality = dfaMinimize(temp); return inequality; }
// Constructs a DFA for the equation coeffs*variables+constant=0 // in two's complement arithmetic DFA *build_DFA_eq_2sc(int vars, int *coeffs, int constant, int *indices) { int min, max, states, next_index, next_label, result, target, count; long i; unsigned long j, transitions; struct map_ent *map; char *statuces; DFA *equality, *temp; if (preprocess(vars, coeffs, &constant, 0)) return dfaFalse(); //initialization min = 0; max = 0; for (i = 0; i < vars; i++) if (coeffs[i] > 0) max += coeffs[i]; else min += coeffs[i]; if (constant > max) max = constant; else if (constant < min) min = constant; states = 2 * max - 2 * min + 3; //This array maps state labels (carries) to state indices map = (struct map_ent *) malloc(states * sizeof(struct map_ent)) - min; for (i = min; i < max + 1; i++) { map[i].s = 0; map[i].sr = 0; map[i].i = -1; map[i].ir = -1; } map[constant].sr = 1; //the first state to be expanded next_index = 0; //the next available state index next_label = constant; //the next available state label map[constant].i = -1; map[constant].ir = 0; count = 0; transitions = 1 << vars; //number of transitions from each state //Begin building dfaSetup(states, vars, indices); while (next_label < max + 1) { //there is a state to expand (excuding sink) if (map[next_label].i == count) map[next_label].s = 2; else map[next_label].sr = 2; dfaAllocExceptions(transitions / 2); for (j = 0; j < transitions; j++) { result = next_label + count_ones(j, vars, coeffs); if (!(result & 1)) { target = result / 2; if (target == next_label) { if (map[target].s == 0) { map[target].s = 1; next_index++; map[target].i = next_index; } dfaStoreException(map[target].i, bintostr(j, vars)); } else { if (map[target].sr == 0) { map[target].sr = 1; next_index++; map[target].ir = next_index; } dfaStoreException(map[target].ir, bintostr(j, vars)); } } } dfaStoreState(states - 1); count++; for (next_label = min; (next_label <= max) && (map[next_label].i != count) && (map[next_label].ir != count); next_label++) ; //find next state to expand } for (; count < states; count++) { dfaAllocExceptions(0); dfaStoreState(states - 1); } //define accepting and rejecting states statuces = (char *) malloc(states + 1); for (i = 0; i < states; i++) statuces[i] = '-'; for (next_label = min; next_label <= max; next_label++) if (map[next_label].s == 2) statuces[map[next_label].i] = '+'; statuces[states] = '\0'; temp = dfaBuild(statuces); equality = dfaMinimize(temp); dfaFree(temp); return equality; }