# Project Euler 879
# Touch-screen Password: count distinct passwords on an n x n grid.
# Bitmask DP: dp[cur][mask] = number of non-empty continuations.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(p: ptr<void>, v: i32, n: i64) -> ptr<void>
}
function popcount(x: i32) -> i32 {
let mut c: i32 = 0
let mut v: i32 = x
while v != 0 {
c = c + 1
v = v & (v - 1)
}
return c
}
function ctz(x: i32) -> i32 {
let mut v: i32 = x
let mut c: i32 = 0
while (v & 1) == 0 {
c = c + 1
v = v >> 1
}
return c
}
function gcd_i(a0: i32, b0: i32) -> i32 {
let mut a: i32 = a0
let mut b: i32 = b0
while b != 0 {
let t: i32 = a % b
a = b
b = t
}
return a
}
function iabs(x: i32) -> i32 {
if x < 0 {
return -x
}
return x
}
function count_passwords(n: i32) -> i64 {
let N: i32 = n * n
if N < 2 {
return 0
}
let between: ptr<i32> = calloc(256, 4)
if between == null {
return 0
}
# precompute_between
for a in 0..N {
let xa: i32 = a % n
let ya: i32 = a / n
for b in 0..N {
if a == b {
continue
}
let xb: i32 = b % n
let yb: i32 = b / n
let dx: i32 = xb - xa
let dy: i32 = yb - ya
let g: i32 = gcd_i(iabs(dx), iabs(dy))
if g <= 1 {
continue
}
let sx: i32 = dx / g
let sy: i32 = dy / g
let mut mask: i32 = 0
for k in 1..g {
let x: i32 = xa + sx * k
let y: i32 = ya + sy * k
mask = mask | (1 << (y * n + x))
}
between[a * 16 + b] = mask
}
}
let all_mask: i32 = (1 << N) - 1
let masksz: i64 = 65536
# dp flat array: dp[cur * 65536 + mask]
let dp: ptr<i64> = calloc((N as i64) * masksz, 8)
if dp == null {
return 0
}
# buckets by popcount
let buckets: ptr<i32> = calloc(17 * 65536, 4)
let bucket_count: ptr<i32> = calloc(17, 4)
if buckets == null || bucket_count == null {
return 0
}
for mask in 0..(1 << N) {
let pc: i32 = popcount(mask)
buckets[pc * 65536 + bucket_count[pc]] = mask
bucket_count[pc] = bucket_count[pc] + 1
}
for k in 1..(N + 1) {
let kk: i32 = N + 1 - k
for bi in 0..bucket_count[kk] {
let mask: i32 = buckets[kk * 65536 + bi]
let remaining: i32 = all_mask ^ mask
if remaining == 0 {
continue
}
let mut m: i32 = mask
while m != 0 {
let lsb: i32 = m & (-m)
let cur: i32 = ctz(lsb)
m = m ^ lsb
let mut total: i64 = 0
let mut rem: i32 = remaining
while rem != 0 {
let bit: i32 = rem & (-rem)
let nxt: i32 = ctz(bit)
rem = rem ^ bit
if (between[cur * 16 + nxt] & remaining) == 0 {
total = total + 1 + dp[(nxt as i64) * masksz + ((mask | bit) as i64)]
}
}
dp[(cur as i64) * masksz + (mask as i64)] = total
}
}
}
let mut total_passwords: i64 = 0
for start in 0..N {
total_passwords = total_passwords + dp[(start as i64) * masksz + ((1 << start) as i64)]
}
free(dp)
free(buckets)
free(bucket_count)
free(between)
return total_passwords
}
function main() -> i32 {
printf("%lld\n", count_passwords(4))
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; }
int32_t popcount_i32(int32_t x);
int32_t ctz_i32(int32_t x);
int32_t gcd_i_i32_i32(int32_t a0, int32_t b0);
int32_t iabs_i32(int32_t x);
int64_t count_passwords_i32(int32_t n);
int32_t main(void);
int32_t popcount_i32(int32_t x) {
int32_t c = 0;
int32_t v = x;
while (v != 0) {
c = (c + 1);
v = (v & (v - 1));
}
return c;
}
int32_t ctz_i32(int32_t x) {
int32_t v = x;
int32_t c = 0;
while ((v & 1) == 0) {
c = (c + 1);
v = FLOW_CHECKED_SHR((v), (1));
}
return c;
}
int32_t gcd_i_i32_i32(int32_t a0, int32_t b0) {
int32_t a = a0;
int32_t b = b0;
while (b != 0) {
int32_t t = FLOW_CHECKED_MOD((a), (b));
a = b;
b = t;
}
return a;
}
int32_t iabs_i32(int32_t x) {
if (x < 0) {
return (-x);
}
return x;
}
int64_t count_passwords_i32(int32_t n) {
int32_t N = (n * n);
if (N < 2) {
return 0;
}
int32_t* between = (int32_t*)(calloc(256, 4));
if (between == NULL) {
return 0;
}
int32_t __flow_step_1 = 1;
for (int32_t a = 0; (0 <= N) ? a < N : a > N; a += (0 <= N) ? 1 : -1) {
int32_t xa = FLOW_CHECKED_MOD((a), (n));
int32_t ya = FLOW_CHECKED_DIV((a), (n));
int32_t __flow_step_2 = 1;
for (int32_t b = 0; (0 <= N) ? b < N : b > N; b += (0 <= N) ? 1 : -1) {
if (a == b) {
continue;
}
int32_t xb = FLOW_CHECKED_MOD((b), (n));
int32_t yb = FLOW_CHECKED_DIV((b), (n));
int32_t dx = (xb - xa);
int32_t dy = (yb - ya);
int32_t g = gcd_i_i32_i32(iabs_i32(dx), iabs_i32(dy));
if (g <= 1) {
continue;
}
int32_t sx = FLOW_CHECKED_DIV((dx), (g));
int32_t sy = FLOW_CHECKED_DIV((dy), (g));
int32_t mask = 0;
int32_t __flow_step_3 = 1;
for (int32_t k = 1; (1 <= g) ? k < g : k > g; k += (1 <= g) ? 1 : -1) {
int32_t x = (xa + (sx * k));
int32_t y = (ya + (sy * k));
mask = (mask | FLOW_CHECKED_SHL((1), (((y * n) + x))));
}
between[((a * 16) + b)] = mask;
}
}
int32_t all_mask = (FLOW_CHECKED_SHL((1), (N)) - 1);
int64_t masksz = 65536;
int64_t* dp = (int64_t*)(calloc((((int64_t)(N)) * masksz), 8));
if (dp == NULL) {
return 0;
}
int32_t* buckets = (int32_t*)(calloc((17 * 65536), 4));
int32_t* bucket_count = (int32_t*)(calloc(17, 4));
if ((buckets == NULL || bucket_count == NULL)) {
return 0;
}
int32_t __flow_step_4 = 1;
for (int32_t mask = 0; (0 <= FLOW_CHECKED_SHL((1), (N))) ? mask < FLOW_CHECKED_SHL((1), (N)) : mask > FLOW_CHECKED_SHL((1), (N)); mask += (0 <= FLOW_CHECKED_SHL((1), (N))) ? 1 : -1) {
int32_t pc = popcount_i32(mask);
buckets[((pc * 65536) + bucket_count[pc])] = mask;
bucket_count[pc] = (bucket_count[pc] + 1);
}
int32_t __flow_step_5 = 1;
for (int32_t k = 1; (1 <= (N + 1)) ? k < (N + 1) : k > (N + 1); k += (1 <= (N + 1)) ? 1 : -1) {
int32_t kk = ((N + 1) - k);
int32_t __flow_step_6 = 1;
for (int32_t bi = 0; (0 <= bucket_count[kk]) ? bi < bucket_count[kk] : bi > bucket_count[kk]; bi += (0 <= bucket_count[kk]) ? 1 : -1) {
int32_t mask = buckets[((kk * 65536) + bi)];
int32_t remaining = (all_mask ^ mask);
if (remaining == 0) {
continue;
}
int32_t m = mask;
while (m != 0) {
int32_t lsb = (m & (-m));
int32_t cur = ctz_i32(lsb);
m = (m ^ lsb);
int64_t total = 0;
int32_t rem = remaining;
while (rem != 0) {
int32_t bit = (rem & (-rem));
int32_t nxt = ctz_i32(bit);
rem = (rem ^ bit);
if ((between[((cur * 16) + nxt)] & remaining) == 0) {
total = ((total + 1) + dp[((((int64_t)(nxt)) * masksz) + ((int64_t)((mask | bit))))]);
}
}
dp[((((int64_t)(cur)) * masksz) + ((int64_t)(mask)))] = total;
}
}
}
int64_t total_passwords = 0;
int32_t __flow_step_7 = 1;
for (int32_t start = 0; (0 <= N) ? start < N : start > N; start += (0 <= N) ? 1 : -1) {
total_passwords = (total_passwords + dp[((((int64_t)(start)) * masksz) + ((int64_t)(FLOW_CHECKED_SHL((1), (start)))))]);
}
free(dp);
free(buckets);
free(bucket_count);
free(between);
return total_passwords;
}
int32_t main(void) {
printf("%lld\n", count_passwords_i32(4));
return 0;
}