Problem 866

Tidying Up B — E(n)=(2n-1)*sum E(k)E(n-1-k) mod 987654319.

Answer492401720
Output492401720
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 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 866
# Tidying Up B — E(n)=(2n-1)*sum E(k)E(n-1-k) mod 987654319.

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

const MOD: i64 = 987654319

function expected_product(n: i64) -> i64 {
    let E: ptr<i64> = calloc(n + 1, 8)
    E[0] = 1
    let mut length: i64 = 1
    while length <= n {
        let mut s: i64 = 0
        let mut k: i64 = 0
        while k < length {
            s = (s + ((E[k] as i128) * (E[length - 1 - k] as i128) % (MOD as i128)) as i64) % MOD
            k = k + 1
        }
        E[length] = (((2 * length - 1) as i128) * (s as i128) % (MOD as i128)) as i64
        length = length + 1
    }
    let ans: i64 = E[n]
    free(E)
    return ans
}

function main() -> i32 {
    printf("%lld\n", expected_product(100))
    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; }

int64_t expected_product_i64(int64_t n);
int32_t main(void);

static const int64_t MOD = 987654319;



int64_t expected_product_i64(int64_t n) {
    int64_t* E = (int64_t*)(calloc((n + 1), 8));
    E[0] = 1;
    int64_t length = 1;
    while (length <= n) {
        int64_t s = 0;
        int64_t k = 0;
        while (k < length) {
            s = FLOW_CHECKED_MOD(((s + ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(E[k])) * ((__int128)(E[((length - 1) - k)])))), (((__int128)(MOD)))))))), (MOD));
            k = (k + 1);
        }
        E[length] = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(((2 * length) - 1))) * ((__int128)(s)))), (((__int128)(MOD))))));
        length = (length + 1);
    }
    int64_t ans = E[n];
    free(E);
    return ans;
}

int32_t main(void) {
    printf("%lld\n", expected_product_i64(100));
    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) -> ()
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(987654319 : i64) : i64
  func.func @expected_product(%arg0: i64) -> i64 {
    %1 = arith.constant 1 : i32
    %3 = arith.extsi %1 : i32 to i64
    %2 = arith.addi %arg0, %3 : i64
    %4 = arith.constant 8 : i32
    %5 = arith.extsi %4 : i32 to i64
    %0 = func.call @calloc(%2, %5) : (i64, i64) -> !llvm.ptr
    %6 = arith.constant 1 : i32
    %7 = arith.constant 0 : i32
    %8 = arith.extsi %6 : i32 to i64
    %9 = arith.extsi %7 : i32 to i64
    %10 = llvm.getelementptr %0[%9] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %8, %10 : i64, !llvm.ptr
    %11 = arith.constant 1 : i32
    %12 = arith.extsi %11 : i32 to i64
    %13 = llvm.mlir.constant(1 : i64) : i64
    %14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %14 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %15 = llvm.load %14 : !llvm.ptr -> i64
    %16 = arith.cmpi sle, %15, %arg0 : i64
    cf.cond_br %16, ^bb1, ^bb2
    ^bb1:
      %17 = arith.constant 0 : i32
      %18 = arith.extsi %17 : i32 to i64
      %19 = llvm.mlir.constant(1 : i64) : i64
      %20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
      llvm.store %18, %20 : i64, !llvm.ptr
      %21 = arith.constant 0 : i32
      %22 = arith.extsi %21 : i32 to i64
      %23 = llvm.mlir.constant(1 : i64) : i64
      %24 = llvm.alloca %23 x i64 : (i64) -> !llvm.ptr
      llvm.store %22, %24 : i64, !llvm.ptr
      cf.br ^bb3
      ^bb3:
      %25 = llvm.load %24 : !llvm.ptr -> i64
      %26 = llvm.load %14 : !llvm.ptr -> i64
      %27 = arith.cmpi slt, %25, %26 : i64
      cf.cond_br %27, ^bb4, ^bb5
      ^bb4:
        %28 = llvm.load %20 : !llvm.ptr -> i64
        %30 = llvm.load %24 : !llvm.ptr -> i64
        %31 = llvm.getelementptr %0[%30] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %29 = llvm.load %31 : !llvm.ptr -> i64
        %32 = arith.extsi %29 : i64 to i128
        %34 = llvm.load %14 : !llvm.ptr -> i64
        %35 = arith.constant 1 : i32
        %37 = arith.extsi %35 : i32 to i64
        %36 = arith.subi %34, %37 : i64
        %38 = llvm.load %24 : !llvm.ptr -> i64
        %39 = arith.subi %36, %38 : i64
        %40 = llvm.getelementptr %0[%39] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %33 = llvm.load %40 : !llvm.ptr -> i64
        %41 = arith.extsi %33 : i64 to i128
        %43 = arith.trunci %32 : i128 to i64
        %44 = arith.trunci %41 : i128 to i64
        %42 = arith.muli %43, %44 : i64
        %45 = llvm.mlir.addressof @MOD : !llvm.ptr
        %46 = llvm.load %45 : !llvm.ptr -> i64
        %47 = arith.extsi %46 : i64 to i128
        %49 = arith.trunci %47 : i128 to i64
        %48 = arith.remsi %42, %49 : i64
        %50 = arith.addi %28, %48 : i64
        %51 = llvm.mlir.addressof @MOD : !llvm.ptr
        %52 = llvm.load %51 : !llvm.ptr -> i64
        %53 = arith.remsi %50, %52 : i64
        llvm.store %53, %20 : i64, !llvm.ptr
        %54 = llvm.load %24 : !llvm.ptr -> i64
        %55 = arith.constant 1 : i32
        %57 = arith.extsi %55 : i32 to i64
        %56 = arith.addi %54, %57 : i64
        llvm.store %56, %24 : i64, !llvm.ptr
        cf.br ^bb3
      ^bb5:
      %58 = arith.constant 2 : i32
      %59 = llvm.load %14 : !llvm.ptr -> i64
      %61 = arith.extsi %58 : i32 to i64
      %60 = arith.muli %61, %59 : i64
      %62 = arith.constant 1 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.subi %60, %64 : i64
      %65 = arith.extsi %63 : i64 to i128
      %66 = llvm.load %20 : !llvm.ptr -> i64
      %67 = arith.extsi %66 : i64 to i128
      %69 = arith.trunci %65 : i128 to i64
      %70 = arith.trunci %67 : i128 to i64
      %68 = arith.muli %69, %70 : i64
      %71 = llvm.mlir.addressof @MOD : !llvm.ptr
      %72 = llvm.load %71 : !llvm.ptr -> i64
      %73 = arith.extsi %72 : i64 to i128
      %75 = arith.trunci %73 : i128 to i64
      %74 = arith.remsi %68, %75 : i64
      %76 = llvm.load %14 : !llvm.ptr -> i64
      %77 = llvm.getelementptr %0[%76] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %74, %77 : i64, !llvm.ptr
      %78 = llvm.load %14 : !llvm.ptr -> i64
      %79 = arith.constant 1 : i32
      %81 = arith.extsi %79 : i32 to i64
      %80 = arith.addi %78, %81 : i64
      llvm.store %80, %14 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %83 = llvm.getelementptr %0[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %82 = llvm.load %83 : !llvm.ptr -> i64
    func.call @free(%0) : (!llvm.ptr) -> ()
    func.return %82 : i64
  }
  func.func @main() -> i32 {
    %85 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %87 = arith.constant 100 : i32
    %88 = arith.extsi %87 : i32 to i64
    %86 = func.call @expected_product(%88) : (i64) -> i64
    %89 = llvm.call @printf(%85, %86) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %90 = arith.constant 0 : i32
    func.return %90 : i32
  }
}