Problem 709

f(n)=Entringer E(n,n) mod 1020202009.

Answer773479144
Output773479144
StatusPASS
Native helperno
Runtime1520 ms
Peak memory1568 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)?
Space complexityO(n)?
ApproachFlow solutionNot curated
VerdictUnknown

Flow source

# Project Euler 709
# f(n)=Entringer E(n,n) mod 1020202009.

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

function main() -> i32 {
    let n: i64 = 24680
    let mod: i64 = 1020202009
    let prev: ptr<i64> = calloc(n + 1, 8)
    let cur: ptr<i64> = calloc(n + 1, 8)
    if prev == null { return 1 }
    prev[0] = 1
    let mut i: i64 = 1
    while i <= n {
        cur[0] = 0
        let mut j: i64 = 1
        while j <= i {
            cur[j] = (cur[j - 1] + prev[i - j]) % mod
            j = j + 1
        }
        let mut k: i64 = 0
        while k <= i {
            prev[k] = cur[k]
            k = k + 1
        }
        i = i + 1
    }
    printf("%lld\n", prev[n])
    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) {
    int64_t n = 24680;
    int64_t mod = 1020202009;
    int64_t* prev = (int64_t*)(calloc((n + 1), 8));
    int64_t* cur = (int64_t*)(calloc((n + 1), 8));
    if (prev == NULL) {
        return 1;
    }
    prev[0] = 1;
    int64_t i = 1;
    while (i <= n) {
        cur[0] = 0;
        int64_t j = 1;
        while (j <= i) {
            cur[j] = FLOW_CHECKED_MOD(((cur[(j - 1)] + prev[(i - j)])), (mod));
            j = (j + 1);
        }
        int64_t k = 0;
        while (k <= i) {
            prev[k] = cur[k];
            k = (k + 1);
        }
        i = (i + 1);
    }
    printf("%lld\n", prev[n]);
    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 @main() -> i32 {
    %0 = arith.constant 24680 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 1020202009 : i32
    %3 = arith.extsi %2 : i32 to i64
    %5 = arith.constant 1 : i32
    %7 = arith.extsi %5 : i32 to i64
    %6 = arith.addi %1, %7 : i64
    %8 = arith.constant 8 : i32
    %9 = arith.extsi %8 : i32 to i64
    %4 = func.call @calloc(%6, %9) : (i64, i64) -> !llvm.ptr
    %11 = arith.constant 1 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.addi %1, %13 : i64
    %14 = arith.constant 8 : i32
    %15 = arith.extsi %14 : i32 to i64
    %10 = func.call @calloc(%12, %15) : (i64, i64) -> !llvm.ptr
    %16 = llvm.mlir.zero : !llvm.ptr
    %17 = llvm.icmp "eq" %4, %16 : !llvm.ptr
    cf.cond_br %17, ^bb0, ^bb1
    ^bb0:
      %18 = arith.constant 1 : i32
      func.return %18 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %19 = arith.constant 1 : i32
    %20 = arith.constant 0 : i32
    %21 = arith.extsi %19 : i32 to i64
    %22 = arith.extsi %20 : i32 to i64
    %23 = llvm.getelementptr %4[%22] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %21, %23 : i64, !llvm.ptr
    %24 = arith.constant 1 : i32
    %25 = arith.extsi %24 : i32 to i64
    %26 = llvm.mlir.constant(1 : i64) : i64
    %27 = llvm.alloca %26 x i64 : (i64) -> !llvm.ptr
    llvm.store %25, %27 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %28 = llvm.load %27 : !llvm.ptr -> i64
    %29 = arith.cmpi sle, %28, %1 : i64
    cf.cond_br %29, ^bb4, ^bb5
    ^bb4:
      %30 = arith.constant 0 : i32
      %31 = arith.constant 0 : i32
      %32 = arith.extsi %30 : i32 to i64
      %33 = arith.extsi %31 : i32 to i64
      %34 = llvm.getelementptr %10[%33] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %32, %34 : i64, !llvm.ptr
      %35 = arith.constant 1 : i32
      %36 = arith.extsi %35 : i32 to i64
      %37 = llvm.mlir.constant(1 : i64) : i64
      %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
      llvm.store %36, %38 : i64, !llvm.ptr
      cf.br ^bb6
      ^bb6:
      %39 = llvm.load %38 : !llvm.ptr -> i64
      %40 = llvm.load %27 : !llvm.ptr -> i64
      %41 = arith.cmpi sle, %39, %40 : i64
      cf.cond_br %41, ^bb7, ^bb8
      ^bb7:
        %43 = llvm.load %38 : !llvm.ptr -> i64
        %44 = arith.constant 1 : i32
        %46 = arith.extsi %44 : i32 to i64
        %45 = arith.subi %43, %46 : i64
        %47 = llvm.getelementptr %10[%45] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %42 = llvm.load %47 : !llvm.ptr -> i64
        %49 = llvm.load %27 : !llvm.ptr -> i64
        %50 = llvm.load %38 : !llvm.ptr -> i64
        %51 = arith.subi %49, %50 : i64
        %52 = llvm.getelementptr %4[%51] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %48 = llvm.load %52 : !llvm.ptr -> i64
        %53 = arith.addi %42, %48 : i64
        %54 = arith.remsi %53, %3 : i64
        %55 = llvm.load %38 : !llvm.ptr -> i64
        %56 = llvm.getelementptr %10[%55] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %54, %56 : i64, !llvm.ptr
        %57 = llvm.load %38 : !llvm.ptr -> i64
        %58 = arith.constant 1 : i32
        %60 = arith.extsi %58 : i32 to i64
        %59 = arith.addi %57, %60 : i64
        llvm.store %59, %38 : i64, !llvm.ptr
        cf.br ^bb6
      ^bb8:
      %61 = arith.constant 0 : i32
      %62 = arith.extsi %61 : i32 to i64
      %63 = llvm.mlir.constant(1 : i64) : i64
      %64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
      llvm.store %62, %64 : i64, !llvm.ptr
      cf.br ^bb9
      ^bb9:
      %65 = llvm.load %64 : !llvm.ptr -> i64
      %66 = llvm.load %27 : !llvm.ptr -> i64
      %67 = arith.cmpi sle, %65, %66 : i64
      cf.cond_br %67, ^bb10, ^bb11
      ^bb10:
        %69 = llvm.load %64 : !llvm.ptr -> i64
        %70 = llvm.getelementptr %10[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %68 = llvm.load %70 : !llvm.ptr -> i64
        %71 = llvm.load %64 : !llvm.ptr -> i64
        %72 = llvm.getelementptr %4[%71] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %68, %72 : i64, !llvm.ptr
        %73 = llvm.load %64 : !llvm.ptr -> i64
        %74 = arith.constant 1 : i32
        %76 = arith.extsi %74 : i32 to i64
        %75 = arith.addi %73, %76 : i64
        llvm.store %75, %64 : i64, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %77 = llvm.load %27 : !llvm.ptr -> i64
      %78 = arith.constant 1 : i32
      %80 = arith.extsi %78 : i32 to i64
      %79 = arith.addi %77, %80 : i64
      llvm.store %79, %27 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %81 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %83 = llvm.getelementptr %4[%1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %82 = llvm.load %83 : !llvm.ptr -> i64
    %84 = llvm.call @printf(%81, %82) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%4) : (!llvm.ptr) -> ()
    func.call @free(%10) : (!llvm.ptr) -> ()
    %87 = arith.constant 0 : i32
    func.return %87 : i32
  }
}