Problem 181

Ways to group 60 black and 40 white objects.

Answer83735848679360680
Output83735848679360680
StatusPASS
Native helperno
Runtime10 ms
Peak memory1136 KB
Time complexityO(n^5) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^5)O(n * m)
Space complexityO(n)O(n)
ApproachFlow solutionDynamic programming or generating function
VerdictUnknown

Flow source

# Project Euler 181
# Ways to group 60 black and 40 white objects.

extern {
    function calloc(n: i64, size: i64) -> ptr<void>
    function free(p: ptr<void>) -> void
    function memcpy(dst: ptr<void>, src: ptr<void>, n: i64) -> ptr<void>
}

function main() -> i32 {
    let MB: i32 = 60
    let MW: i32 = 40
    let rows: i64 = (MB + 1) as i64
    let cols: i64 = (MW + 1) as i64
    let prev: ptr<i64> = calloc(rows * cols, 8)
    let cur: ptr<i64> = calloc(rows * cols, 8)
    if prev == null || cur == null { return 1 }
    prev[0] = 1

    let mut ub: i32 = 0
    while ub <= MB {
        let mut uw: i32 = 0
        while uw <= MW {
            if !(ub == 0 && uw == 0) {
                let mut i: i32 = 0
                while i <= MB {
                    let mut j: i32 = 0
                    while j <= MW {
                        let mut total: i64 = 0
                        let mut k: i32 = 0
                        while i >= k * ub && j >= k * uw {
                            total = total + prev[((i - k * ub) as i64) * cols + ((j - k * uw) as i64)]
                            k = k + 1
                        }
                        cur[(i as i64) * cols + (j as i64)] = total
                        j = j + 1
                    }
                    i = i + 1
                }
                memcpy(prev as ptr<void>, cur as ptr<void>, rows * cols * 8)
            }
            uw = uw + 1
        }
        ub = ub + 1
    }
    printf("%lld\n", cur[(MB as i64) * cols + (MW as i64)])
    free(prev)
    free(cur)
    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) {
    int32_t MB = 60;
    int32_t MW = 40;
    int64_t rows = ((int64_t)((MB + 1)));
    int64_t cols = ((int64_t)((MW + 1)));
    int64_t* prev = (int64_t*)(calloc((rows * cols), 8));
    int64_t* cur = (int64_t*)(calloc((rows * cols), 8));
    if ((prev == NULL || cur == NULL)) {
        return 1;
    }
    prev[0] = 1;
    int32_t ub = 0;
    while (ub <= MB) {
        int32_t uw = 0;
        while (uw <= MW) {
            if ((!((ub == 0 && uw == 0)))) {
                int32_t i = 0;
                while (i <= MB) {
                    int32_t j = 0;
                    while (j <= MW) {
                        int64_t total = 0;
                        int32_t k = 0;
                        while ((i >= (k * ub) && j >= (k * uw))) {
                            total = (total + prev[((((int64_t)((i - (k * ub)))) * cols) + ((int64_t)((j - (k * uw)))))]);
                            k = (k + 1);
                        }
                        cur[((((int64_t)(i)) * cols) + ((int64_t)(j)))] = total;
                        j = (j + 1);
                    }
                    i = (i + 1);
                }
                memcpy(((void*)(prev)), ((void*)(cur)), ((rows * cols) * 8));
            }
            uw = (uw + 1);
        }
        ub = (ub + 1);
    }
    printf("%lld\n", cur[((((int64_t)(MB)) * cols) + ((int64_t)(MW)))]);
    free(prev);
    free(cur);
    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 private @memcpy(!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
  func.func @main() -> i32 {
    %0 = arith.constant 60 : i32
    %1 = arith.constant 40 : i32
    %2 = arith.constant 1 : i32
    %3 = arith.addi %0, %2 : i32
    %4 = arith.extsi %3 : i32 to i64
    %5 = arith.constant 1 : i32
    %6 = arith.addi %1, %5 : i32
    %7 = arith.extsi %6 : i32 to i64
    %9 = arith.muli %4, %7 : i64
    %10 = arith.constant 8 : i32
    %11 = arith.extsi %10 : i32 to i64
    %8 = func.call @calloc(%9, %11) : (i64, i64) -> !llvm.ptr
    %13 = arith.muli %4, %7 : i64
    %14 = arith.constant 8 : i32
    %15 = arith.extsi %14 : i32 to i64
    %12 = func.call @calloc(%13, %15) : (i64, i64) -> !llvm.ptr
    %16 = llvm.mlir.zero : !llvm.ptr
    %17 = llvm.icmp "eq" %8, %16 : !llvm.ptr
    %18 = scf.if %17 -> (i1) {
      %19 = arith.constant true
      scf.yield %19 : i1
    } else {
      %20 = llvm.mlir.zero : !llvm.ptr
      %21 = llvm.icmp "eq" %12, %20 : !llvm.ptr
      scf.yield %21 : i1
    }
    cf.cond_br %18, ^bb0, ^bb1
    ^bb0:
      %22 = arith.constant 1 : i32
      func.return %22 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %23 = arith.constant 1 : i32
    %24 = arith.constant 0 : i32
    %25 = arith.extsi %23 : i32 to i64
    %26 = arith.extsi %24 : i32 to i64
    %27 = llvm.getelementptr %8[%26] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %25, %27 : i64, !llvm.ptr
    %28 = arith.constant 0 : i32
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.alloca %29 x i32 : (i64) -> !llvm.ptr
    llvm.store %28, %30 : i32, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %31 = llvm.load %30 : !llvm.ptr -> i32
    %32 = arith.cmpi sle, %31, %0 : i32
    cf.cond_br %32, ^bb4, ^bb5
    ^bb4:
      %33 = arith.constant 0 : i32
      %34 = llvm.mlir.constant(1 : i64) : i64
      %35 = llvm.alloca %34 x i32 : (i64) -> !llvm.ptr
      llvm.store %33, %35 : i32, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %36 = llvm.load %35 : !llvm.ptr -> i32
      %37 = arith.cmpi sle, %36, %1 : i32
      cf.cond_br %37, ^bb7, ^bb8
      ^bb7:
        %38 = llvm.load %30 : !llvm.ptr -> i32
        %39 = arith.constant 0 : i32
        %40 = arith.cmpi eq, %38, %39 : i32
        %41 = scf.if %40 -> (i1) {
          %42 = llvm.load %35 : !llvm.ptr -> i32
          %43 = arith.constant 0 : i32
          %44 = arith.cmpi eq, %42, %43 : i32
          scf.yield %44 : i1
        } else {
          %45 = arith.constant false
          scf.yield %45 : i1
        }
        %47 = arith.constant 1 : i1
        %46 = arith.xori %41, %47 : i1
        cf.cond_br %46, ^bb9, ^bb10
        ^bb9:
          %49 = arith.constant 0 : i32
          %50 = llvm.mlir.constant(1 : i64) : i64
          %51 = llvm.alloca %50 x i32 : (i64) -> !llvm.ptr
          llvm.store %49, %51 : i32, !llvm.ptr
          cf.br ^bb12
          ^bb12:
          %52 = llvm.load %51 : !llvm.ptr -> i32
          %53 = arith.cmpi sle, %52, %0 : i32
          cf.cond_br %53, ^bb13, ^bb14
          ^bb13:
            %54 = arith.constant 0 : i32
            %55 = llvm.mlir.constant(1 : i64) : i64
            %56 = llvm.alloca %55 x i32 : (i64) -> !llvm.ptr
            llvm.store %54, %56 : i32, !llvm.ptr
            cf.br ^bb15
            ^bb15:
            %57 = llvm.load %56 : !llvm.ptr -> i32
            %58 = arith.cmpi sle, %57, %1 : i32
            cf.cond_br %58, ^bb16, ^bb17
            ^bb16:
              %59 = arith.constant 0 : i32
              %60 = arith.extsi %59 : i32 to i64
              %61 = llvm.mlir.constant(1 : i64) : i64
              %62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
              llvm.store %60, %62 : i64, !llvm.ptr
              %63 = arith.constant 0 : i32
              %64 = llvm.mlir.constant(1 : i64) : i64
              %65 = llvm.alloca %64 x i32 : (i64) -> !llvm.ptr
              llvm.store %63, %65 : i32, !llvm.ptr
              cf.br ^bb18
              ^bb18:
              %66 = llvm.load %51 : !llvm.ptr -> i32
              %67 = llvm.load %65 : !llvm.ptr -> i32
              %68 = llvm.load %30 : !llvm.ptr -> i32
              %69 = arith.muli %67, %68 : i32
              %70 = arith.cmpi sge, %66, %69 : i32
              %71 = scf.if %70 -> (i1) {
                %72 = llvm.load %56 : !llvm.ptr -> i32
                %73 = llvm.load %65 : !llvm.ptr -> i32
                %74 = llvm.load %35 : !llvm.ptr -> i32
                %75 = arith.muli %73, %74 : i32
                %76 = arith.cmpi sge, %72, %75 : i32
                scf.yield %76 : i1
              } else {
                %77 = arith.constant false
                scf.yield %77 : i1
              }
              cf.cond_br %71, ^bb19, ^bb20
              ^bb19:
                %78 = llvm.load %62 : !llvm.ptr -> i64
                %80 = llvm.load %51 : !llvm.ptr -> i32
                %81 = llvm.load %65 : !llvm.ptr -> i32
                %82 = llvm.load %30 : !llvm.ptr -> i32
                %83 = arith.muli %81, %82 : i32
                %84 = arith.subi %80, %83 : i32
                %85 = arith.extsi %84 : i32 to i64
                %86 = arith.muli %85, %7 : i64
                %87 = llvm.load %56 : !llvm.ptr -> i32
                %88 = llvm.load %65 : !llvm.ptr -> i32
                %89 = llvm.load %35 : !llvm.ptr -> i32
                %90 = arith.muli %88, %89 : i32
                %91 = arith.subi %87, %90 : i32
                %92 = arith.extsi %91 : i32 to i64
                %93 = arith.addi %86, %92 : i64
                %94 = llvm.getelementptr %8[%93] : (!llvm.ptr, i64) -> !llvm.ptr, i64
                %79 = llvm.load %94 : !llvm.ptr -> i64
                %95 = arith.addi %78, %79 : i64
                llvm.store %95, %62 : i64, !llvm.ptr
                %96 = llvm.load %65 : !llvm.ptr -> i32
                %97 = arith.constant 1 : i32
                %98 = arith.addi %96, %97 : i32
                llvm.store %98, %65 : i32, !llvm.ptr
                cf.br ^bb18
              ^bb20:
              %99 = llvm.load %62 : !llvm.ptr -> i64
              %100 = llvm.load %51 : !llvm.ptr -> i32
              %101 = arith.extsi %100 : i32 to i64
              %102 = arith.muli %101, %7 : i64
              %103 = llvm.load %56 : !llvm.ptr -> i32
              %104 = arith.extsi %103 : i32 to i64
              %105 = arith.addi %102, %104 : i64
              %106 = llvm.getelementptr %12[%105] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              llvm.store %99, %106 : i64, !llvm.ptr
              %107 = llvm.load %56 : !llvm.ptr -> i32
              %108 = arith.constant 1 : i32
              %109 = arith.addi %107, %108 : i32
              llvm.store %109, %56 : i32, !llvm.ptr
              cf.br ^bb15
            ^bb17:
            %110 = llvm.load %51 : !llvm.ptr -> i32
            %111 = arith.constant 1 : i32
            %112 = arith.addi %110, %111 : i32
            llvm.store %112, %51 : i32, !llvm.ptr
            cf.br ^bb12
          ^bb14:
          %114 = arith.muli %4, %7 : i64
          %115 = arith.constant 8 : i32
          %117 = arith.extsi %115 : i32 to i64
          %116 = arith.muli %114, %117 : i64
          %113 = func.call @memcpy(%8, %12, %116) : (!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
          cf.br ^bb11
        ^bb10:
          cf.br ^bb11
        ^bb11:
        %118 = llvm.load %35 : !llvm.ptr -> i32
        %119 = arith.constant 1 : i32
        %120 = arith.addi %118, %119 : i32
        llvm.store %120, %35 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %121 = llvm.load %30 : !llvm.ptr -> i32
      %122 = arith.constant 1 : i32
      %123 = arith.addi %121, %122 : i32
      llvm.store %123, %30 : i32, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %124 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %126 = arith.extsi %0 : i32 to i64
    %127 = arith.muli %126, %7 : i64
    %128 = arith.extsi %1 : i32 to i64
    %129 = arith.addi %127, %128 : i64
    %130 = llvm.getelementptr %12[%129] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %125 = llvm.load %130 : !llvm.ptr -> i64
    %131 = llvm.call @printf(%124, %125) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%8) : (!llvm.ptr) -> ()
    func.call @free(%12) : (!llvm.ptr) -> ()
    %134 = arith.constant 0 : i32
    func.return %134 : i32
  }
}