Problem 509
Divisor Nim — g(n)=v2(n); winning triples <= 123456787654321 mod 1234567890.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n log log n) |
| Space complexity | O(1) | O(n) |
| Approach | Flow solution | Sieve-based divisor sums |
| Verdict | Suboptimal |
Flow source
# Project Euler 509
# Divisor Nim — g(n)=v2(n); winning triples <= 123456787654321 mod 1234567890.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let MOD: i64 = 1234567890
let N: i64 = 123456787654321
let f: ptr<i64> = calloc(64, 8)
if f == null { return 1 }
let mut k: i64 = 0
while k < 63 {
f[k] = (N >> k) - (N >> (k + 1))
k = k + 1
}
let mut losing: i64 = 0
let mut i: i64 = 0
while i < 64 {
let ci: i64 = f[i] % MOD
if ci != 0 {
let mut j: i64 = 0
while j < 64 {
let cj: i64 = f[j] % MOD
if cj != 0 {
let x: i64 = i ^ j
let cx: i64 = f[x] % MOD
losing = (losing + ((ci * cj) % MOD) * cx) % MOD
}
j = j + 1
}
}
i = i + 1
}
let nmod: i64 = N % MOD
let total: i64 = (((nmod * nmod) % MOD) * nmod) % MOD
let mut winning: i64 = (total - losing) % MOD
if winning < 0 {
winning = winning + MOD
}
printf("%lld\n", winning)
free(f)
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 main(void);
int32_t main(void) {
int64_t MOD = 1234567890;
int64_t N = 123456787654321;
int64_t* f = (int64_t*)(calloc(64, 8));
if (f == NULL) {
return 1;
}
int64_t k = 0;
while (k < 63) {
f[k] = (FLOW_CHECKED_SHR((N), (k)) - FLOW_CHECKED_SHR((N), ((k + 1))));
k = (k + 1);
}
int64_t losing = 0;
int64_t i = 0;
while (i < 64) {
int64_t ci = FLOW_CHECKED_MOD((f[i]), (MOD));
if (ci != 0) {
int64_t j = 0;
while (j < 64) {
int64_t cj = FLOW_CHECKED_MOD((f[j]), (MOD));
if (cj != 0) {
int64_t x = (i ^ j);
int64_t cx = FLOW_CHECKED_MOD((f[x]), (MOD));
losing = FLOW_CHECKED_MOD(((losing + (FLOW_CHECKED_MOD(((ci * cj)), (MOD)) * cx))), (MOD));
}
j = (j + 1);
}
}
i = (i + 1);
}
int64_t nmod = FLOW_CHECKED_MOD((N), (MOD));
int64_t total = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((nmod * nmod)), (MOD)) * nmod)), (MOD));
int64_t winning = FLOW_CHECKED_MOD(((total - losing)), (MOD));
if (winning < 0) {
winning = (winning + MOD);
}
printf("%lld\n", winning);
free(f);
return 0;
}
Generated MLIR
module {
llvm.func @printf(!llvm.ptr, ...) -> i32
llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
func.func private @calloc(i64, i64) -> !llvm.ptr
func.func private @free(!llvm.ptr) -> ()
func.func @main() -> i32 {
%0 = arith.constant 1234567890 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = arith.constant 123452492687025 : i32
%3 = arith.extsi %2 : i32 to i64
%5 = arith.constant 64 : i32
%6 = arith.constant 8 : i32
%7 = arith.extsi %5 : i32 to i64
%8 = arith.extsi %6 : i32 to i64
%4 = func.call @calloc(%7, %8) : (i64, i64) -> !llvm.ptr
%9 = llvm.mlir.zero : !llvm.ptr
%10 = llvm.icmp "eq" %4, %9 : !llvm.ptr
cf.cond_br %10, ^bb0, ^bb1
^bb0:
%11 = arith.constant 1 : i32
func.return %11 : i32
^bb1:
cf.br ^bb2
^bb2:
%12 = arith.constant 0 : i32
%13 = arith.extsi %12 : i32 to i64
%14 = llvm.mlir.constant(1 : i64) : i64
%15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
llvm.store %13, %15 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%16 = llvm.load %15 : !llvm.ptr -> i64
%17 = arith.constant 63 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi slt, %16, %19 : i64
cf.cond_br %18, ^bb4, ^bb5
^bb4:
%20 = llvm.load %15 : !llvm.ptr -> i64
%21 = arith.shrsi %3, %20 : i64
%22 = llvm.load %15 : !llvm.ptr -> i64
%23 = arith.constant 1 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.addi %22, %25 : i64
%26 = arith.shrsi %3, %24 : i64
%27 = arith.subi %21, %26 : i64
%28 = llvm.load %15 : !llvm.ptr -> i64
%29 = llvm.getelementptr %4[%28] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %27, %29 : i64, !llvm.ptr
%30 = llvm.load %15 : !llvm.ptr -> i64
%31 = arith.constant 1 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.addi %30, %33 : i64
llvm.store %32, %15 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%34 = arith.constant 0 : i32
%35 = arith.extsi %34 : i32 to i64
%36 = llvm.mlir.constant(1 : i64) : i64
%37 = llvm.alloca %36 x i64 : (i64) -> !llvm.ptr
llvm.store %35, %37 : i64, !llvm.ptr
%38 = arith.constant 0 : i32
%39 = arith.extsi %38 : i32 to i64
%40 = llvm.mlir.constant(1 : i64) : i64
%41 = llvm.alloca %40 x i64 : (i64) -> !llvm.ptr
llvm.store %39, %41 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%42 = llvm.load %41 : !llvm.ptr -> i64
%43 = arith.constant 64 : i32
%45 = arith.extsi %43 : i32 to i64
%44 = arith.cmpi slt, %42, %45 : i64
cf.cond_br %44, ^bb7, ^bb8
^bb7:
%47 = llvm.load %41 : !llvm.ptr -> i64
%48 = llvm.getelementptr %4[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%46 = llvm.load %48 : !llvm.ptr -> i64
%49 = arith.remsi %46, %1 : i64
%50 = arith.constant 0 : i32
%52 = arith.extsi %50 : i32 to i64
%51 = arith.cmpi ne, %49, %52 : i64
cf.cond_br %51, ^bb9, ^bb10
^bb9:
%53 = arith.constant 0 : i32
%54 = arith.extsi %53 : i32 to i64
%55 = llvm.mlir.constant(1 : i64) : i64
%56 = llvm.alloca %55 x i64 : (i64) -> !llvm.ptr
llvm.store %54, %56 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%57 = llvm.load %56 : !llvm.ptr -> i64
%58 = arith.constant 64 : i32
%60 = arith.extsi %58 : i32 to i64
%59 = arith.cmpi slt, %57, %60 : i64
cf.cond_br %59, ^bb13, ^bb14
^bb13:
%62 = llvm.load %56 : !llvm.ptr -> i64
%63 = llvm.getelementptr %4[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%61 = llvm.load %63 : !llvm.ptr -> i64
%64 = arith.remsi %61, %1 : i64
%65 = arith.constant 0 : i32
%67 = arith.extsi %65 : i32 to i64
%66 = arith.cmpi ne, %64, %67 : i64
cf.cond_br %66, ^bb15, ^bb16
^bb15:
%68 = llvm.load %41 : !llvm.ptr -> i64
%69 = llvm.load %56 : !llvm.ptr -> i64
%70 = arith.xori %68, %69 : i64
%72 = llvm.getelementptr %4[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%71 = llvm.load %72 : !llvm.ptr -> i64
%73 = arith.remsi %71, %1 : i64
%74 = llvm.load %37 : !llvm.ptr -> i64
%75 = arith.muli %49, %64 : i64
%76 = arith.remsi %75, %1 : i64
%77 = arith.muli %76, %73 : i64
%78 = arith.addi %74, %77 : i64
%79 = arith.remsi %78, %1 : i64
llvm.store %79, %37 : i64, !llvm.ptr
cf.br ^bb17
^bb16:
cf.br ^bb17
^bb17:
%80 = llvm.load %56 : !llvm.ptr -> i64
%81 = arith.constant 1 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.addi %80, %83 : i64
llvm.store %82, %56 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
cf.br ^bb11
^bb10:
cf.br ^bb11
^bb11:
%84 = llvm.load %41 : !llvm.ptr -> i64
%85 = arith.constant 1 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.addi %84, %87 : i64
llvm.store %86, %41 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%88 = arith.remsi %3, %1 : i64
%89 = arith.muli %88, %88 : i64
%90 = arith.remsi %89, %1 : i64
%91 = arith.muli %90, %88 : i64
%92 = arith.remsi %91, %1 : i64
%93 = llvm.load %37 : !llvm.ptr -> i64
%94 = arith.subi %92, %93 : i64
%95 = arith.remsi %94, %1 : i64
%96 = llvm.mlir.constant(1 : i64) : i64
%97 = llvm.alloca %96 x i64 : (i64) -> !llvm.ptr
llvm.store %95, %97 : i64, !llvm.ptr
%98 = llvm.load %97 : !llvm.ptr -> i64
%99 = arith.constant 0 : i32
%101 = arith.extsi %99 : i32 to i64
%100 = arith.cmpi slt, %98, %101 : i64
cf.cond_br %100, ^bb18, ^bb19
^bb18:
%102 = llvm.load %97 : !llvm.ptr -> i64
%103 = arith.addi %102, %1 : i64
llvm.store %103, %97 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%104 = llvm.mlir.addressof @str_0 : !llvm.ptr
%105 = llvm.load %97 : !llvm.ptr -> i64
%106 = llvm.call @printf(%104, %105) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%4) : (!llvm.ptr) -> ()
%108 = arith.constant 0 : i32
func.return %108 : i32
}
}