Problem 310
Nim Square losing positions for a,b,c ≤ 100000.
View problem on Project Euler
Performance comparison
| Metric | Our solution | Best known |
| Time complexity | O(n^2) | O(n^3) |
| Space complexity | O(n) | O(n^2) |
| Approach | Flow solution | Game theory DP |
| Verdict | Optimal |
Flow source
# Project Euler 310
# Nim Square losing positions for a,b,c ≤ 100000.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
function main() -> i32 {
let limit: i64 = 100000
let mex: ptr<i32> = calloc(limit + 1, 4)
let moves: ptr<i8> = calloc(80, 1)
if mex == null || moves == null { return 1 }
let mut size: i64 = 0
while size <= limit {
let mut mi: i64 = 0
while mi < 80 {
moves[mi] = 0
mi = mi + 1
}
let mut i: i64 = 1
while i * i <= size {
moves[mex[size - i * i]] = 1
i = i + 1
}
let mut available: i32 = 0
while moves[available] == 1 {
available = available + 1
}
mex[size] = available
size = size + 1
}
let mut max_nim: i32 = 0
size = 0
while size <= limit {
if mex[size] > max_nim { max_nim = mex[size] }
size = size + 1
}
let mut next_pow: i64 = 1
while next_pow < (max_nim as i64) {
next_pow = next_pow * 2
}
let frequency: ptr<i64> = calloc(next_pow, 8)
if frequency == null { return 1 }
let mut num_lost: i64 = 0
let mut a: i64 = limit
while a >= 0 {
let mut c: i64 = a
while c <= limit {
let idx: i64 = (mex[a] ^ mex[c]) as i64
frequency[idx] = frequency[idx] + 1
c = c + 1
}
num_lost = num_lost + frequency[mex[a]]
a = a - 1
}
printf("%lld\n", num_lost)
free(mex); free(moves); free(frequency)
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 limit = 100000;
int32_t* mex = (int32_t*)(calloc((limit + 1), 4));
int8_t* moves = (int8_t*)(calloc(80, 1));
if ((mex == NULL || moves == NULL)) {
return 1;
}
int64_t size = 0;
while (size <= limit) {
int64_t mi = 0;
while (mi < 80) {
moves[mi] = 0;
mi = (mi + 1);
}
int64_t i = 1;
while ((i * i) <= size) {
moves[mex[(size - (i * i))]] = 1;
i = (i + 1);
}
int32_t available = 0;
while (moves[available] == 1) {
available = (available + 1);
}
mex[size] = available;
size = (size + 1);
}
int32_t max_nim = 0;
size = 0;
while (size <= limit) {
if (mex[size] > max_nim) {
max_nim = mex[size];
}
size = (size + 1);
}
int64_t next_pow = 1;
while (next_pow < ((int64_t)(max_nim))) {
next_pow = (next_pow * 2);
}
int64_t* frequency = (int64_t*)(calloc(next_pow, 8));
if (frequency == NULL) {
return 1;
}
int64_t num_lost = 0;
int64_t a = limit;
while (a >= 0) {
int64_t c = a;
while (c <= limit) {
int64_t idx = ((int64_t)((mex[a] ^ mex[c])));
frequency[idx] = (frequency[idx] + 1);
c = (c + 1);
}
num_lost = (num_lost + frequency[mex[a]]);
a = (a - 1);
}
printf("%lld\n", num_lost);
free(mex);
free(moves);
free(frequency);
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 100000 : i32
%1 = arith.extsi %0 : i32 to i64
%3 = arith.constant 1 : i32
%5 = arith.extsi %3 : i32 to i64
%4 = arith.addi %1, %5 : i64
%6 = arith.constant 4 : i32
%7 = arith.extsi %6 : i32 to i64
%2 = func.call @calloc(%4, %7) : (i64, i64) -> !llvm.ptr
%9 = arith.constant 80 : i32
%10 = arith.constant 1 : i32
%11 = arith.extsi %9 : i32 to i64
%12 = arith.extsi %10 : i32 to i64
%8 = func.call @calloc(%11, %12) : (i64, i64) -> !llvm.ptr
%13 = llvm.mlir.zero : !llvm.ptr
%14 = llvm.icmp "eq" %2, %13 : !llvm.ptr
%15 = scf.if %14 -> (i1) {
%16 = arith.constant true
scf.yield %16 : i1
} else {
%17 = llvm.mlir.zero : !llvm.ptr
%18 = llvm.icmp "eq" %8, %17 : !llvm.ptr
scf.yield %18 : i1
}
cf.cond_br %15, ^bb0, ^bb1
^bb0:
%19 = arith.constant 1 : i32
func.return %19 : i32
^bb1:
cf.br ^bb2
^bb2:
%20 = arith.constant 0 : i32
%21 = arith.extsi %20 : i32 to i64
%22 = llvm.mlir.constant(1 : i64) : i64
%23 = llvm.alloca %22 x i64 : (i64) -> !llvm.ptr
llvm.store %21, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%24 = llvm.load %23 : !llvm.ptr -> i64
%25 = arith.cmpi sle, %24, %1 : i64
cf.cond_br %25, ^bb4, ^bb5
^bb4:
%26 = arith.constant 0 : i32
%27 = arith.extsi %26 : i32 to i64
%28 = llvm.mlir.constant(1 : i64) : i64
%29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
llvm.store %27, %29 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%30 = llvm.load %29 : !llvm.ptr -> i64
%31 = arith.constant 80 : i32
%33 = arith.extsi %31 : i32 to i64
%32 = arith.cmpi slt, %30, %33 : i64
cf.cond_br %32, ^bb7, ^bb8
^bb7:
%34 = arith.constant 0 : i32
%35 = llvm.load %29 : !llvm.ptr -> i64
%36 = arith.trunci %34 : i32 to i8
%37 = llvm.getelementptr %8[%35] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %36, %37 : i8, !llvm.ptr
%38 = llvm.load %29 : !llvm.ptr -> i64
%39 = arith.constant 1 : i32
%41 = arith.extsi %39 : i32 to i64
%40 = arith.addi %38, %41 : i64
llvm.store %40, %29 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%42 = arith.constant 1 : i32
%43 = arith.extsi %42 : i32 to i64
%44 = llvm.mlir.constant(1 : i64) : i64
%45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
llvm.store %43, %45 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%46 = llvm.load %45 : !llvm.ptr -> i64
%47 = llvm.load %45 : !llvm.ptr -> i64
%48 = arith.muli %46, %47 : i64
%49 = llvm.load %23 : !llvm.ptr -> i64
%50 = arith.cmpi sle, %48, %49 : i64
cf.cond_br %50, ^bb10, ^bb11
^bb10:
%51 = arith.constant 1 : i32
%53 = llvm.load %23 : !llvm.ptr -> i64
%54 = llvm.load %45 : !llvm.ptr -> i64
%55 = llvm.load %45 : !llvm.ptr -> i64
%56 = arith.muli %54, %55 : i64
%57 = arith.subi %53, %56 : i64
%58 = llvm.getelementptr %2[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%52 = llvm.load %58 : !llvm.ptr -> i32
%59 = arith.trunci %51 : i32 to i8
%60 = arith.extsi %52 : i32 to i64
%61 = llvm.getelementptr %8[%60] : (!llvm.ptr, i64) -> !llvm.ptr, i8
llvm.store %59, %61 : i8, !llvm.ptr
%62 = llvm.load %45 : !llvm.ptr -> i64
%63 = arith.constant 1 : i32
%65 = arith.extsi %63 : i32 to i64
%64 = arith.addi %62, %65 : i64
llvm.store %64, %45 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%66 = arith.constant 0 : i32
%67 = llvm.mlir.constant(1 : i64) : i64
%68 = llvm.alloca %67 x i32 : (i64) -> !llvm.ptr
llvm.store %66, %68 : i32, !llvm.ptr
cf.br ^bb12
^bb12:
%70 = llvm.load %68 : !llvm.ptr -> i32
%71 = arith.extsi %70 : i32 to i64
%72 = llvm.getelementptr %8[%71] : (!llvm.ptr, i64) -> !llvm.ptr, i8
%69 = llvm.load %72 : !llvm.ptr -> i8
%73 = arith.constant 1 : i32
%75 = arith.extsi %69 : i8 to i32
%74 = arith.cmpi eq, %75, %73 : i32
cf.cond_br %74, ^bb13, ^bb14
^bb13:
%76 = llvm.load %68 : !llvm.ptr -> i32
%77 = arith.constant 1 : i32
%78 = arith.addi %76, %77 : i32
llvm.store %78, %68 : i32, !llvm.ptr
cf.br ^bb12
^bb14:
%79 = llvm.load %68 : !llvm.ptr -> i32
%80 = llvm.load %23 : !llvm.ptr -> i64
%81 = llvm.getelementptr %2[%80] : (!llvm.ptr, i64) -> !llvm.ptr, i32
llvm.store %79, %81 : i32, !llvm.ptr
%82 = llvm.load %23 : !llvm.ptr -> i64
%83 = arith.constant 1 : i32
%85 = arith.extsi %83 : i32 to i64
%84 = arith.addi %82, %85 : i64
llvm.store %84, %23 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%86 = arith.constant 0 : i32
%87 = llvm.mlir.constant(1 : i64) : i64
%88 = llvm.alloca %87 x i32 : (i64) -> !llvm.ptr
llvm.store %86, %88 : i32, !llvm.ptr
%89 = arith.constant 0 : i32
%90 = arith.extsi %89 : i32 to i64
llvm.store %90, %23 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%91 = llvm.load %23 : !llvm.ptr -> i64
%92 = arith.cmpi sle, %91, %1 : i64
cf.cond_br %92, ^bb16, ^bb17
^bb16:
%94 = llvm.load %23 : !llvm.ptr -> i64
%95 = llvm.getelementptr %2[%94] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%93 = llvm.load %95 : !llvm.ptr -> i32
%96 = llvm.load %88 : !llvm.ptr -> i32
%97 = arith.cmpi sgt, %93, %96 : i32
cf.cond_br %97, ^bb18, ^bb19
^bb18:
%99 = llvm.load %23 : !llvm.ptr -> i64
%100 = llvm.getelementptr %2[%99] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%98 = llvm.load %100 : !llvm.ptr -> i32
llvm.store %98, %88 : i32, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%101 = llvm.load %23 : !llvm.ptr -> i64
%102 = arith.constant 1 : i32
%104 = arith.extsi %102 : i32 to i64
%103 = arith.addi %101, %104 : i64
llvm.store %103, %23 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%105 = arith.constant 1 : i32
%106 = arith.extsi %105 : i32 to i64
%107 = llvm.mlir.constant(1 : i64) : i64
%108 = llvm.alloca %107 x i64 : (i64) -> !llvm.ptr
llvm.store %106, %108 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%109 = llvm.load %108 : !llvm.ptr -> i64
%110 = llvm.load %88 : !llvm.ptr -> i32
%111 = arith.extsi %110 : i32 to i64
%112 = arith.cmpi slt, %109, %111 : i64
cf.cond_br %112, ^bb22, ^bb23
^bb22:
%113 = llvm.load %108 : !llvm.ptr -> i64
%114 = arith.constant 2 : i32
%116 = arith.extsi %114 : i32 to i64
%115 = arith.muli %113, %116 : i64
llvm.store %115, %108 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%118 = llvm.load %108 : !llvm.ptr -> i64
%119 = arith.constant 8 : i32
%120 = arith.extsi %119 : i32 to i64
%117 = func.call @calloc(%118, %120) : (i64, i64) -> !llvm.ptr
%121 = llvm.mlir.zero : !llvm.ptr
%122 = llvm.icmp "eq" %117, %121 : !llvm.ptr
cf.cond_br %122, ^bb24, ^bb25
^bb24:
%123 = arith.constant 1 : i32
func.return %123 : i32
^bb25:
cf.br ^bb26
^bb26:
%124 = arith.constant 0 : i32
%125 = arith.extsi %124 : i32 to i64
%126 = llvm.mlir.constant(1 : i64) : i64
%127 = llvm.alloca %126 x i64 : (i64) -> !llvm.ptr
llvm.store %125, %127 : i64, !llvm.ptr
%128 = llvm.mlir.constant(1 : i64) : i64
%129 = llvm.alloca %128 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %129 : i64, !llvm.ptr
cf.br ^bb27
^bb27:
%130 = llvm.load %129 : !llvm.ptr -> i64
%131 = arith.constant 0 : i32
%133 = arith.extsi %131 : i32 to i64
%132 = arith.cmpi sge, %130, %133 : i64
cf.cond_br %132, ^bb28, ^bb29
^bb28:
%134 = llvm.load %129 : !llvm.ptr -> i64
%135 = llvm.mlir.constant(1 : i64) : i64
%136 = llvm.alloca %135 x i64 : (i64) -> !llvm.ptr
llvm.store %134, %136 : i64, !llvm.ptr
cf.br ^bb30
^bb30:
%137 = llvm.load %136 : !llvm.ptr -> i64
%138 = arith.cmpi sle, %137, %1 : i64
cf.cond_br %138, ^bb31, ^bb32
^bb31:
%140 = llvm.load %129 : !llvm.ptr -> i64
%141 = llvm.getelementptr %2[%140] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%139 = llvm.load %141 : !llvm.ptr -> i32
%143 = llvm.load %136 : !llvm.ptr -> i64
%144 = llvm.getelementptr %2[%143] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%142 = llvm.load %144 : !llvm.ptr -> i32
%145 = arith.xori %139, %142 : i32
%146 = arith.extsi %145 : i32 to i64
%148 = llvm.getelementptr %117[%146] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%147 = llvm.load %148 : !llvm.ptr -> i64
%149 = arith.constant 1 : i32
%151 = arith.extsi %149 : i32 to i64
%150 = arith.addi %147, %151 : i64
%152 = llvm.getelementptr %117[%146] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %150, %152 : i64, !llvm.ptr
%153 = llvm.load %136 : !llvm.ptr -> i64
%154 = arith.constant 1 : i32
%156 = arith.extsi %154 : i32 to i64
%155 = arith.addi %153, %156 : i64
llvm.store %155, %136 : i64, !llvm.ptr
cf.br ^bb30
^bb32:
%157 = llvm.load %127 : !llvm.ptr -> i64
%160 = llvm.load %129 : !llvm.ptr -> i64
%161 = llvm.getelementptr %2[%160] : (!llvm.ptr, i64) -> !llvm.ptr, i32
%159 = llvm.load %161 : !llvm.ptr -> i32
%162 = arith.extsi %159 : i32 to i64
%163 = llvm.getelementptr %117[%162] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%158 = llvm.load %163 : !llvm.ptr -> i64
%164 = arith.addi %157, %158 : i64
llvm.store %164, %127 : i64, !llvm.ptr
%165 = llvm.load %129 : !llvm.ptr -> i64
%166 = arith.constant 1 : i32
%168 = arith.extsi %166 : i32 to i64
%167 = arith.subi %165, %168 : i64
llvm.store %167, %129 : i64, !llvm.ptr
cf.br ^bb27
^bb29:
%169 = llvm.mlir.addressof @str_0 : !llvm.ptr
%170 = llvm.load %127 : !llvm.ptr -> i64
%171 = llvm.call @printf(%169, %170) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
func.call @free(%2) : (!llvm.ptr) -> ()
func.call @free(%8) : (!llvm.ptr) -> ()
func.call @free(%117) : (!llvm.ptr) -> ()
%175 = arith.constant 0 : i32
func.return %175 : i32
}
}