# Project Euler 673
# Beds and Desks: count permutations commuting with two involutions.
# Build graph from B/D edges, find connected components, classify, compute automorphism count.
extern {
function fopen(path: string, mode: string) -> ptr<void>
function fgetc(f: ptr<void>) -> i32
function fclose(f: ptr<void>) -> i32
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 999999937
const MAXN: i32 = 501
function mod_pow(base: i64, exp: i64, m: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base % m
let mut e: i64 = exp
while e > 0 {
if (e & 1) != 0 {
r = r * b % m
}
b = b * b % m
e = e >> 1
}
return r
}
# Parse a file of "a,b" pairs into an involution array.
function parse_file(path: string, inv: ptr<i32>) -> void {
let f: ptr<void> = fopen(path, "r")
let mut c: i32 = fgetc(f)
while c >= 0 {
# Skip non-digit characters until we find the first digit
while c >= 0 && (c < 48 || c > 57) {
c = fgetc(f)
}
if c < 0 { break }
# Parse first number a
let mut a: i32 = 0
while c >= 48 && c <= 57 {
a = a * 10 + (c - 48)
c = fgetc(f)
}
# Skip non-digit characters until second number
while c >= 0 && (c < 48 || c > 57) {
c = fgetc(f)
}
if c < 0 { break }
# Parse second number b
let mut b: i32 = 0
while c >= 48 && c <= 57 {
b = b * 10 + (c - 48)
c = fgetc(f)
}
if a > 0 && b > 0 {
inv[a] = b
inv[b] = a
}
}
fclose(f)
}
function main() -> i32 {
let n: i32 = 500
let B: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let D: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let visited: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let stack_arr: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let comp: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let fact: ptr<i64> = calloc((MAXN as i64), 8) as ptr<i64>
let mut i: i32 = 0
while i <= n {
B[i] = i
D[i] = i
visited[i] = 0
i = i + 1
}
parse_file("data/p673_beds.txt", B)
parse_file("data/p673_desks.txt", D)
fact[0] = 1
let mut fi: i32 = 1
while fi <= n {
fact[fi] = fact[fi - 1] * (fi as i64) % MOD
fi = fi + 1
}
# Component type arrays
let tc_cycle: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let tc_k: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let tc_color: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let tc_count: ptr<i32> = calloc((MAXN as i64), 4) as ptr<i32>
let tc_aut: ptr<i64> = calloc((MAXN as i64), 8) as ptr<i64>
let mut num_types: i32 = 0
let mut start: i32 = 1
while start <= n {
if visited[start] != 0 {
start = start + 1
continue
}
let mut sp: i32 = 0
let mut cp: i32 = 0
stack_arr[sp] = start
sp = sp + 1
visited[start] = 1
while sp > 0 {
sp = sp - 1
let v: i32 = stack_arr[sp]
comp[cp] = v
cp = cp + 1
let u1: i32 = B[v]
let u2: i32 = D[v]
if visited[u1] == 0 {
visited[u1] = 1
stack_arr[sp] = u1
sp = sp + 1
}
if visited[u2] == 0 {
visited[u2] = 1
stack_arr[sp] = u2
sp = sp + 1
}
}
let k: i32 = cp
let mut has_loop: bool = false
let mut ci: i32 = 0
while ci < cp {
let v: i32 = comp[ci]
if B[v] == v || D[v] == v {
has_loop = true
break
}
ci = ci + 1
}
let mut is_cycle: i32 = 0
if !has_loop { is_cycle = 1 }
let mut end_color: i32 = 0
let mut aut: i64 = 0
if is_cycle != 0 {
aut = k as i64
} else {
if k % 2 == 1 {
aut = 1
} else {
let mut ci2: i32 = 0
while ci2 < cp {
let v: i32 = comp[ci2]
if B[v] == v && D[v] != v {
end_color = 1
break
}
if D[v] == v && B[v] != v {
end_color = 2
break
}
ci2 = ci2 + 1
}
aut = 2
}
}
let mut found: i32 = -1
let mut t: i32 = 0
while t < num_types {
if tc_cycle[t] == is_cycle && tc_k[t] == k && tc_color[t] == end_color {
found = t
break
}
t = t + 1
}
if found >= 0 {
tc_count[found] = tc_count[found] + 1
} else {
tc_cycle[num_types] = is_cycle
tc_k[num_types] = k
tc_color[num_types] = end_color
tc_aut[num_types] = aut
tc_count[num_types] = 1
num_types = num_types + 1
}
start = start + 1
}
let mut answer: i64 = 1
let mut t: i32 = 0
while t < num_types {
answer = answer * mod_pow(tc_aut[t], (tc_count[t] as i64), MOD) % MOD
answer = answer * fact[tc_count[t]] % MOD
t = t + 1
}
printf("%lld\n", answer)
free(B as ptr<void>)
free(D as ptr<void>)
free(visited as ptr<void>)
free(stack_arr as ptr<void>)
free(comp as ptr<void>)
free(fact as ptr<void>)
free(tc_cycle as ptr<void>)
free(tc_k as ptr<void>)
free(tc_color as ptr<void>)
free(tc_count as ptr<void>)
free(tc_aut as ptr<void>)
return 0
}
Generated C
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
while (flow_temp_head) {
flow_temp_node* n = flow_temp_head;
flow_temp_head = n->next;
free(n);
}
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
if (!node) return NULL;
node->next = flow_temp_head;
flow_temp_head = node;
if (!flow_temp_atexit_set) {
flow_temp_atexit_set = 1;
atexit(flow_temp_free_all);
}
return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
char* r = (char*)flow_temp_alloc(la + lb + 1);
if (!r) return NULL;
if (la) memcpy(r, a, la);
if (lb) memcpy(r + la, b, lb);
r[la + lb] = '\0';
return r;
}
#define __flow_in_arr(arr, val) __extension__ ({ \
int _found = 0; \
size_t _n = sizeof(arr)/sizeof((arr)[0]); \
for (size_t _i = 0; _i < _n; _i++) { \
if ((arr)[_i] == (val)) { _found = 1; break; } \
} _found; })
/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
abort();
#if defined(__GNUC__) || defined(__clang__)
__builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")
#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#include <math.h>
void* _ui_state = NULL;
static inline float i32_to_f32(int32_t v) { return (float)v; }
/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t m);
void parse_file_string_ptr_i32(char* path, int32_t* inv);
int32_t main(void);
static const int64_t MOD = 999999937;
static const int32_t MAXN = 501;
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t m) {
int64_t r = 1;
int64_t b = FLOW_CHECKED_MOD((base), (m));
int64_t e = exp;
while (e > 0) {
if ((e & 1) != 0) {
r = FLOW_CHECKED_MOD(((r * b)), (m));
}
b = FLOW_CHECKED_MOD(((b * b)), (m));
e = FLOW_CHECKED_SHR((e), (1));
}
return r;
}
void parse_file_string_ptr_i32(char* path, int32_t* inv) {
void* f = (void*)(fopen(path, "r"));
int32_t c = fgetc(f);
while (c >= 0) {
while ((c >= 0 && (c < 48 || c > 57))) {
c = fgetc(f);
}
if (c < 0) {
break;
}
int32_t a = 0;
while ((c >= 48 && c <= 57)) {
a = ((a * 10) + (c - 48));
c = fgetc(f);
}
while ((c >= 0 && (c < 48 || c > 57))) {
c = fgetc(f);
}
if (c < 0) {
break;
}
int32_t b = 0;
while ((c >= 48 && c <= 57)) {
b = ((b * 10) + (c - 48));
c = fgetc(f);
}
if ((a > 0 && b > 0)) {
inv[a] = b;
inv[b] = a;
}
}
fclose(f);
}
int32_t main(void) {
int32_t n = 500;
int32_t* B = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* D = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* visited = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* stack_arr = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* comp = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int64_t* fact = (int64_t*)(((int64_t*)(calloc(((int64_t)(MAXN)), 8))));
int32_t i = 0;
while (i <= n) {
B[i] = i;
D[i] = i;
visited[i] = 0;
i = (i + 1);
}
parse_file_string_ptr_i32("data/p673_beds.txt", B);
parse_file_string_ptr_i32("data/p673_desks.txt", D);
fact[0] = 1;
int32_t fi = 1;
while (fi <= n) {
fact[fi] = FLOW_CHECKED_MOD(((fact[(fi - 1)] * ((int64_t)(fi)))), (MOD));
fi = (fi + 1);
}
int32_t* tc_cycle = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* tc_k = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* tc_color = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int32_t* tc_count = (int32_t*)(((int32_t*)(calloc(((int64_t)(MAXN)), 4))));
int64_t* tc_aut = (int64_t*)(((int64_t*)(calloc(((int64_t)(MAXN)), 8))));
int32_t num_types = 0;
int32_t start = 1;
while (start <= n) {
if (visited[start] != 0) {
start = (start + 1);
continue;
}
int32_t sp = 0;
int32_t cp = 0;
stack_arr[sp] = start;
sp = (sp + 1);
visited[start] = 1;
while (sp > 0) {
sp = (sp - 1);
int32_t v = stack_arr[sp];
comp[cp] = v;
cp = (cp + 1);
int32_t u1 = B[v];
int32_t u2 = D[v];
if (visited[u1] == 0) {
visited[u1] = 1;
stack_arr[sp] = u1;
sp = (sp + 1);
}
if (visited[u2] == 0) {
visited[u2] = 1;
stack_arr[sp] = u2;
sp = (sp + 1);
}
}
int32_t k = cp;
bool has_loop = 0;
int32_t ci = 0;
while (ci < cp) {
int32_t v = comp[ci];
if ((B[v] == v || D[v] == v)) {
has_loop = 1;
break;
}
ci = (ci + 1);
}
int32_t is_cycle = 0;
if ((!(has_loop))) {
is_cycle = 1;
}
int32_t end_color = 0;
int64_t aut = 0;
if (is_cycle != 0) {
aut = ((int64_t)(k));
} else {
if (FLOW_CHECKED_MOD((k), (2)) == 1) {
aut = 1;
} else {
int32_t ci2 = 0;
while (ci2 < cp) {
int32_t v = comp[ci2];
if ((B[v] == v && D[v] != v)) {
end_color = 1;
break;
}
if ((D[v] == v && B[v] != v)) {
end_color = 2;
break;
}
ci2 = (ci2 + 1);
}
aut = 2;
}
}
int32_t found = (-1);
int32_t t = 0;
while (t < num_types) {
if (((tc_cycle[t] == is_cycle && tc_k[t] == k) && tc_color[t] == end_color)) {
found = t;
break;
}
t = (t + 1);
}
if (found >= 0) {
tc_count[found] = (tc_count[found] + 1);
} else {
tc_cycle[num_types] = is_cycle;
tc_k[num_types] = k;
tc_color[num_types] = end_color;
tc_aut[num_types] = aut;
tc_count[num_types] = 1;
num_types = (num_types + 1);
}
start = (start + 1);
}
int64_t answer = 1;
int32_t t = 0;
while (t < num_types) {
answer = FLOW_CHECKED_MOD(((answer * mod_pow_i64_i64_i64(tc_aut[t], ((int64_t)(tc_count[t])), MOD))), (MOD));
answer = FLOW_CHECKED_MOD(((answer * fact[tc_count[t]])), (MOD));
t = (t + 1);
}
printf("%lld\n", answer);
free(((void*)(B)));
free(((void*)(D)));
free(((void*)(visited)));
free(((void*)(stack_arr)));
free(((void*)(comp)));
free(((void*)(fact)));
free(((void*)(tc_cycle)));
free(((void*)(tc_k)));
free(((void*)(tc_color)));
free(((void*)(tc_count)));
free(((void*)(tc_aut)));
return 0;
}