Problem 819

Iterative Sampling - E(1000), expected steps until all entries equal. The process is equivalent to repeated composition of random functions. The chain is monotone non-increasing in m (image size), so the expected hitting time to m=1 is computed by a triangular dynamic program.

Answer1995.975556
Output1995.975556
StatusPASS
Native helperno
Runtime0 ms
Peak memory1168 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n * s^2)
Space complexityO(n)O(s^2)
ApproachFlow solutionMarkov chain or DP over states
VerdictUnknown

Flow source

# Project Euler 819
# Iterative Sampling - E(1000), expected steps until all entries equal.
# The process is equivalent to repeated composition of random functions.
# The chain is monotone non-increasing in m (image size), so the expected
# hitting time to m=1 is computed by a triangular dynamic program.

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

function main() -> i32 {
    let n: i32 = 1000
    if n <= 1 {
        printf("0.000000\n")
        return 0
    }

    let n64: i64 = (n + 1) as i64
    let T: ptr<f64> = calloc(n64, 8)
    let p: ptr<f64> = calloc(n64, 8)
    if T == null || p == null { return 1 }

    T[1] = 0.0
    p[0] = 1.0
    let inv_n: f64 = 1.0 / (n as f64)

    let mut m: i32 = 1
    while m <= n {
        let mut k: i32 = m
        while k >= 1 {
            let kf: f64 = (k as f64)
            let pk: f64 = p[k] * (kf * inv_n)
            let pk1: f64 = p[k - 1] * (((n - k + 1) as f64) * inv_n)
            p[k] = pk + pk1
            k = k - 1
        }
        p[0] = 0.0

        if m == 1 {
            m = m + 1
            continue
        }

        let stay: f64 = p[m]
        let mut acc: f64 = 0.0
        let mut kk: i32 = 1
        while kk < m {
            acc = acc + p[kk] * T[kk]
            kk = kk + 1
        }

        T[m] = (1.0 + acc) / (1.0 - stay)
        m = m + 1
    }

    printf("%.6f\n", T[n])
    free(T)
    free(p)
    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 n = 1000;
    if (n <= 1) {
        printf("0.000000\n");
        return 0;
    }
    int64_t n64 = ((int64_t)((n + 1)));
    double* T = (double*)(calloc(n64, 8));
    double* p = (double*)(calloc(n64, 8));
    if ((T == NULL || p == NULL)) {
        return 1;
    }
    T[1] = 0.0;
    p[0] = 1.0;
    double inv_n = (1.0 / ((double)(n)));
    int32_t m = 1;
    while (m <= n) {
        int32_t k = m;
        while (k >= 1) {
            double kf = ((double)(k));
            double pk = (p[k] * (kf * inv_n));
            double pk1 = (p[(k - 1)] * (((double)(((n - k) + 1))) * inv_n));
            p[k] = (pk + pk1);
            k = (k - 1);
        }
        p[0] = 0.0;
        if (m == 1) {
            m = (m + 1);
            continue;
        }
        double stay = p[m];
        double acc = 0.0;
        int32_t kk = 1;
        while (kk < m) {
            acc = (acc + (p[kk] * T[kk]));
            kk = (kk + 1);
        }
        T[m] = ((1.0 + acc) / (1.0 - stay));
        m = (m + 1);
    }
    printf("%.6f\n", T[n]);
    free(T);
    free(p);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("0.000000\n\00") {addr_space = 0 : i32} : !llvm.array<10 x i8>
  llvm.mlir.global internal constant @str_1("%.6f\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 1000 : i32
    %1 = arith.constant 1 : i32
    %2 = arith.cmpi sle, %0, %1 : i32
    cf.cond_br %2, ^bb0, ^bb1
    ^bb0:
      %3 = llvm.mlir.addressof @str_0 : !llvm.ptr
      %4 = llvm.call @printf(%3) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
      %5 = arith.constant 0 : i32
      func.return %5 : i32
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %6 = arith.constant 1 : i32
    %7 = arith.addi %0, %6 : i32
    %8 = arith.extsi %7 : i32 to i64
    %10 = arith.constant 8 : i32
    %11 = arith.extsi %10 : i32 to i64
    %9 = func.call @calloc(%8, %11) : (i64, i64) -> !llvm.ptr
    %13 = arith.constant 8 : i32
    %14 = arith.extsi %13 : i32 to i64
    %12 = func.call @calloc(%8, %14) : (i64, i64) -> !llvm.ptr
    %15 = llvm.mlir.zero : !llvm.ptr
    %16 = llvm.icmp "eq" %9, %15 : !llvm.ptr
    %17 = scf.if %16 -> (i1) {
      %18 = arith.constant true
      scf.yield %18 : i1
    } else {
      %19 = llvm.mlir.zero : !llvm.ptr
      %20 = llvm.icmp "eq" %12, %19 : !llvm.ptr
      scf.yield %20 : i1
    }
    cf.cond_br %17, ^bb3, ^bb4
    ^bb3:
      %21 = arith.constant 1 : i32
      func.return %21 : i32
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %22 = arith.constant 0.0 : f32
    %23 = arith.constant 1 : i32
    %24 = arith.extf %22 : f32 to f64
    %25 = arith.extsi %23 : i32 to i64
    %26 = llvm.getelementptr %9[%25] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %24, %26 : f64, !llvm.ptr
    %27 = arith.constant 1.0 : f32
    %28 = arith.constant 0 : i32
    %29 = arith.extf %27 : f32 to f64
    %30 = arith.extsi %28 : i32 to i64
    %31 = llvm.getelementptr %12[%30] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %29, %31 : f64, !llvm.ptr
    %32 = arith.constant 1.0 : f32
    %33 = arith.sitofp %0 : i32 to f64
    %35 = arith.extf %32 : f32 to f64
    %34 = arith.divf %35, %33 : f64
    %36 = arith.constant 1 : i32
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i32 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %39 = llvm.load %38 : !llvm.ptr -> i32
    %40 = arith.cmpi sle, %39, %0 : i32
    cf.cond_br %40, ^bb7, ^bb8
    ^bb7:
      %41 = llvm.load %38 : !llvm.ptr -> i32
      %42 = llvm.mlir.constant(1 : i64) : i64
      %43 = llvm.alloca %42 x i32 : (i64) -> !llvm.ptr
      llvm.store %41, %43 : i32, !llvm.ptr
      cf.br ^bb9
      ^bb9:
      %44 = llvm.load %43 : !llvm.ptr -> i32
      %45 = arith.constant 1 : i32
      %46 = arith.cmpi sge, %44, %45 : i32
      cf.cond_br %46, ^bb10, ^bb11
      ^bb10:
        %47 = llvm.load %43 : !llvm.ptr -> i32
        %48 = arith.sitofp %47 : i32 to f64
        %50 = llvm.load %43 : !llvm.ptr -> i32
        %51 = arith.extsi %50 : i32 to i64
        %52 = llvm.getelementptr %12[%51] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %49 = llvm.load %52 : !llvm.ptr -> f64
        %53 = arith.mulf %48, %34 : f64
        %54 = arith.mulf %49, %53 : f64
        %56 = llvm.load %43 : !llvm.ptr -> i32
        %57 = arith.constant 1 : i32
        %58 = arith.subi %56, %57 : i32
        %59 = arith.extsi %58 : i32 to i64
        %60 = llvm.getelementptr %12[%59] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %55 = llvm.load %60 : !llvm.ptr -> f64
        %61 = llvm.load %43 : !llvm.ptr -> i32
        %62 = arith.subi %0, %61 : i32
        %63 = arith.constant 1 : i32
        %64 = arith.addi %62, %63 : i32
        %65 = arith.sitofp %64 : i32 to f64
        %66 = arith.mulf %65, %34 : f64
        %67 = arith.mulf %55, %66 : f64
        %68 = arith.addf %54, %67 : f64
        %69 = llvm.load %43 : !llvm.ptr -> i32
        %70 = arith.extsi %69 : i32 to i64
        %71 = llvm.getelementptr %12[%70] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %68, %71 : f64, !llvm.ptr
        %72 = llvm.load %43 : !llvm.ptr -> i32
        %73 = arith.constant 1 : i32
        %74 = arith.subi %72, %73 : i32
        llvm.store %74, %43 : i32, !llvm.ptr
        cf.br ^bb9
      ^bb11:
      %75 = arith.constant 0.0 : f32
      %76 = arith.constant 0 : i32
      %77 = arith.extf %75 : f32 to f64
      %78 = arith.extsi %76 : i32 to i64
      %79 = llvm.getelementptr %12[%78] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      llvm.store %77, %79 : f64, !llvm.ptr
      %80 = llvm.load %38 : !llvm.ptr -> i32
      %81 = arith.constant 1 : i32
      %82 = arith.cmpi eq, %80, %81 : i32
      cf.cond_br %82, ^bb12, ^bb13
      ^bb12:
        %83 = llvm.load %38 : !llvm.ptr -> i32
        %84 = arith.constant 1 : i32
        %85 = arith.addi %83, %84 : i32
        llvm.store %85, %38 : i32, !llvm.ptr
        cf.br ^bb6
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %87 = llvm.load %38 : !llvm.ptr -> i32
      %88 = arith.extsi %87 : i32 to i64
      %89 = llvm.getelementptr %12[%88] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %86 = llvm.load %89 : !llvm.ptr -> f64
      %90 = arith.constant 0.0 : f32
      %91 = arith.extf %90 : f32 to f64
      %92 = llvm.mlir.constant(1 : i64) : i64
      %93 = llvm.alloca %92 x f64 : (i64) -> !llvm.ptr
      llvm.store %91, %93 : f64, !llvm.ptr
      %94 = arith.constant 1 : i32
      %95 = llvm.mlir.constant(1 : i64) : i64
      %96 = llvm.alloca %95 x i32 : (i64) -> !llvm.ptr
      llvm.store %94, %96 : i32, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %97 = llvm.load %96 : !llvm.ptr -> i32
      %98 = llvm.load %38 : !llvm.ptr -> i32
      %99 = arith.cmpi slt, %97, %98 : i32
      cf.cond_br %99, ^bb16, ^bb17
      ^bb16:
        %100 = llvm.load %93 : !llvm.ptr -> f64
        %102 = llvm.load %96 : !llvm.ptr -> i32
        %103 = arith.extsi %102 : i32 to i64
        %104 = llvm.getelementptr %12[%103] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %101 = llvm.load %104 : !llvm.ptr -> f64
        %106 = llvm.load %96 : !llvm.ptr -> i32
        %107 = arith.extsi %106 : i32 to i64
        %108 = llvm.getelementptr %9[%107] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %105 = llvm.load %108 : !llvm.ptr -> f64
        %109 = arith.mulf %101, %105 : f64
        %110 = arith.addf %100, %109 : f64
        llvm.store %110, %93 : f64, !llvm.ptr
        %111 = llvm.load %96 : !llvm.ptr -> i32
        %112 = arith.constant 1 : i32
        %113 = arith.addi %111, %112 : i32
        llvm.store %113, %96 : i32, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %114 = arith.constant 1.0 : f32
      %115 = llvm.load %93 : !llvm.ptr -> f64
      %117 = arith.extf %114 : f32 to f64
      %116 = arith.addf %117, %115 : f64
      %118 = arith.constant 1.0 : f32
      %120 = arith.extf %118 : f32 to f64
      %119 = arith.subf %120, %86 : f64
      %121 = arith.divf %116, %119 : f64
      %122 = llvm.load %38 : !llvm.ptr -> i32
      %123 = arith.extsi %122 : i32 to i64
      %124 = llvm.getelementptr %9[%123] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      llvm.store %121, %124 : f64, !llvm.ptr
      %125 = llvm.load %38 : !llvm.ptr -> i32
      %126 = arith.constant 1 : i32
      %127 = arith.addi %125, %126 : i32
      llvm.store %127, %38 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %128 = llvm.mlir.addressof @str_1 : !llvm.ptr
    %130 = arith.extsi %0 : i32 to i64
    %131 = llvm.getelementptr %9[%130] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    %129 = llvm.load %131 : !llvm.ptr -> f64
    %132 = llvm.call @printf(%128, %129) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, f64) -> i32
    func.call @free(%9) : (!llvm.ptr) -> ()
    func.call @free(%12) : (!llvm.ptr) -> ()
    %135 = arith.constant 0 : i32
    func.return %135 : i32
  }
}