Problem 597

Torpids p(13,1800): probability final order is even; recursive expected sign.

Answer0.5001817828
Output0.5001817828
StatusPASS
Native helperno
Runtime0 ms
Peak memory1200 KB
Time complexityO(1) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 597
# Torpids
# p(13,1800): probability final order is even; recursive expected sign.

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

let mut G_memo: ptr<f64> = null
let mut G_seen: ptr<i8> = null
let mut G_tmax: i64 = 0
let mut G_n: i64 = 0

function expected_sign(l: i64, r: i64, t: i64) -> f64 {
    if l >= r { return 1.0 }
    let key: i64 = ((l * (G_n + 2) + r) * (G_tmax + 1) + t)
    if G_seen[key] != 0 {
        return G_memo[key]
    }
    let count: i64 = r - l + 1
    let sum_j: i64 = (l + r) * count / 2
    let S: f64 = (count as f64) * (t as f64) - (sum_j as f64)
    let mut total: f64 = 0.0
    let mut m: i64 = l
    while m <= r {
        let pm: f64 = ((t - m) as f64) / S
        let mut sign_flip: f64 = 1.0
        if ((m - l) & 1) != 0 { sign_flip = -1.0 }
        let left: f64 = expected_sign(l, m - 1, m)
        let right: f64 = expected_sign(m + 1, r, t)
        total = total + pm * sign_flip * left * right
        m = m + 1
    }
    G_seen[key] = 1
    G_memo[key] = total
    return total
}

function main() -> i32 {
    let n: i64 = 13
    let t0: i64 = 46
    G_n = n
    G_tmax = t0
    let sz: i64 = (n + 2) * (n + 2) * (t0 + 1)
    G_memo = calloc(sz, 8)
    G_seen = calloc(sz, 1)
    if G_memo == null || G_seen == null { return 1 }
    let e: f64 = expected_sign(1, n, t0)
    let p: f64 = (1.0 + e) / 2.0
    let qq: i64 = floor(p * 10000000000.0 + 0.5) as i64
    let ip: i64 = qq / 10000000000
    let fp: i64 = qq % 10000000000
    printf("%lld.%010lld\n", ip, fp)
    free(G_seen)
    free(G_memo)
    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; }

double expected_sign_i64_i64_i64(int64_t l, int64_t r, int64_t t);
int32_t main(void);

/* Module statics */
static double* G_memo = NULL;
static int8_t* G_seen = NULL;
static int64_t G_tmax = 0;
static int64_t G_n = 0;




double expected_sign_i64_i64_i64(int64_t l, int64_t r, int64_t t) {
    if (l >= r) {
        return 1.0;
    }
    int64_t key = ((((l * (G_n + 2)) + r) * (G_tmax + 1)) + t);
    if (G_seen[key] != 0) {
        return G_memo[key];
    }
    int64_t count = ((r - l) + 1);
    int64_t sum_j = FLOW_CHECKED_DIV((((l + r) * count)), (2));
    double S = ((((double)(count)) * ((double)(t))) - ((double)(sum_j)));
    double total = 0.0;
    int64_t m = l;
    while (m <= r) {
        double pm = (((double)((t - m))) / S);
        double sign_flip = 1.0;
        if (((m - l) & 1) != 0) {
            sign_flip = (-1.0);
        }
        double left = expected_sign_i64_i64_i64(l, (m - 1), m);
        double right = expected_sign_i64_i64_i64((m + 1), r, t);
        total = (total + (((pm * sign_flip) * left) * right));
        m = (m + 1);
    }
    G_seen[key] = 1;
    G_memo[key] = total;
    return total;
}

int32_t main(void) {
    int64_t n = 13;
    int64_t t0 = 46;
    G_n = n;
    G_tmax = t0;
    int64_t sz = (((n + 2) * (n + 2)) * (t0 + 1));
    G_memo = calloc(sz, 8);
    G_seen = calloc(sz, 1);
    if ((G_memo == NULL || G_seen == NULL)) {
        return 1;
    }
    double e = expected_sign_i64_i64_i64(1, n, t0);
    double p = ((1.0 + e) / 2.0);
    int64_t qq = ((int64_t)(floor(((p * 10000000000.0) + 0.5))));
    int64_t ip = FLOW_CHECKED_DIV((qq), (10000000000));
    int64_t fp = FLOW_CHECKED_MOD((qq), (10000000000));
    printf("%lld.%010lld\n", ip, fp);
    free(G_seen);
    free(G_memo);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld.%010lld\n\00") {addr_space = 0 : i32} : !llvm.array<14 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func private @floor(f64) -> f64
  // Module static: G_memo
  llvm.mlir.global internal @G_memo() {addr_space = 0 : i32} : !llvm.ptr {
    %0 = llvm.mlir.zero : !llvm.ptr
    llvm.return %0 : !llvm.ptr
  }
  // Module static: G_seen
  llvm.mlir.global internal @G_seen() {addr_space = 0 : i32} : !llvm.ptr {
    %1 = llvm.mlir.zero : !llvm.ptr
    llvm.return %1 : !llvm.ptr
  }
  // Module static: G_tmax
  llvm.mlir.global internal @G_tmax(0 : i64) : i64
  // Module static: G_n
  llvm.mlir.global internal @G_n(0 : i64) : i64
  func.func @expected_sign(%arg0: i64, %arg1: i64, %arg2: i64) -> f64 {
    %2 = arith.cmpi sge, %arg0, %arg1 : i64
    cf.cond_br %2, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 1.0 : f32
      %4 = arith.extf %3 : f32 to f64
      func.return %4 : f64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = llvm.mlir.addressof @G_n : !llvm.ptr
    %6 = llvm.load %5 : !llvm.ptr -> i64
    %7 = arith.constant 2 : i32
    %9 = arith.extsi %7 : i32 to i64
    %8 = arith.addi %6, %9 : i64
    %10 = arith.muli %arg0, %8 : i64
    %11 = arith.addi %10, %arg1 : i64
    %12 = llvm.mlir.addressof @G_tmax : !llvm.ptr
    %13 = llvm.load %12 : !llvm.ptr -> i64
    %14 = arith.constant 1 : i32
    %16 = arith.extsi %14 : i32 to i64
    %15 = arith.addi %13, %16 : i64
    %17 = arith.muli %11, %15 : i64
    %18 = arith.addi %17, %arg2 : i64
    %20 = llvm.mlir.addressof @G_seen : !llvm.ptr
    %21 = llvm.load %20 : !llvm.ptr -> !llvm.ptr
    %22 = llvm.getelementptr %21[%18] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    %19 = llvm.load %22 : !llvm.ptr -> i8
    %23 = arith.constant 0 : i32
    %25 = arith.extsi %19 : i8 to i32
    %24 = arith.cmpi ne, %25, %23 : i32
    cf.cond_br %24, ^bb3, ^bb4
    ^bb3:
      %27 = llvm.mlir.addressof @G_memo : !llvm.ptr
      %28 = llvm.load %27 : !llvm.ptr -> !llvm.ptr
      %29 = llvm.getelementptr %28[%18] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %26 = llvm.load %29 : !llvm.ptr -> f64
      func.return %26 : f64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %30 = arith.subi %arg1, %arg0 : i64
    %31 = arith.constant 1 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.addi %30, %33 : i64
    %34 = arith.addi %arg0, %arg1 : i64
    %35 = arith.muli %34, %32 : i64
    %36 = arith.constant 2 : i32
    %38 = arith.extsi %36 : i32 to i64
    %37 = arith.divsi %35, %38 : i64
    %39 = arith.sitofp %32 : i64 to f64
    %40 = arith.sitofp %arg2 : i64 to f64
    %41 = arith.mulf %39, %40 : f64
    %42 = arith.sitofp %37 : i64 to f64
    %43 = arith.subf %41, %42 : f64
    %44 = arith.constant 0.0 : f32
    %45 = arith.extf %44 : f32 to f64
    %46 = llvm.mlir.constant(1 : i64) : i64
    %47 = llvm.alloca %46 x f64 : (i64) -> !llvm.ptr
    llvm.store %45, %47 : f64, !llvm.ptr
    %48 = llvm.mlir.constant(1 : i64) : i64
    %49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %49 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %50 = llvm.load %49 : !llvm.ptr -> i64
    %51 = arith.cmpi sle, %50, %arg1 : i64
    cf.cond_br %51, ^bb7, ^bb8
    ^bb7:
      %52 = llvm.load %49 : !llvm.ptr -> i64
      %53 = arith.subi %arg2, %52 : i64
      %54 = arith.sitofp %53 : i64 to f64
      %55 = arith.divf %54, %43 : f64
      %56 = arith.constant 1.0 : f32
      %57 = arith.extf %56 : f32 to f64
      %58 = llvm.mlir.constant(1 : i64) : i64
      %59 = llvm.alloca %58 x f64 : (i64) -> !llvm.ptr
      llvm.store %57, %59 : f64, !llvm.ptr
      %60 = llvm.load %49 : !llvm.ptr -> i64
      %61 = arith.subi %60, %arg0 : i64
      %62 = arith.constant 1 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.andi %61, %64 : i64
      %65 = arith.constant 0 : i32
      %67 = arith.extsi %65 : i32 to i64
      %66 = arith.cmpi ne, %63, %67 : i64
      cf.cond_br %66, ^bb9, ^bb10
      ^bb9:
        %68 = arith.constant 1.0 : f32
        %69 = arith.negf %68 : f32
        %70 = arith.extf %69 : f32 to f64
        llvm.store %70, %59 : f64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %72 = llvm.load %49 : !llvm.ptr -> i64
      %73 = arith.constant 1 : i32
      %75 = arith.extsi %73 : i32 to i64
      %74 = arith.subi %72, %75 : i64
      %76 = llvm.load %49 : !llvm.ptr -> i64
      %71 = func.call @expected_sign(%arg0, %74, %76) : (i64, i64, i64) -> f64
      %78 = llvm.load %49 : !llvm.ptr -> i64
      %79 = arith.constant 1 : i32
      %81 = arith.extsi %79 : i32 to i64
      %80 = arith.addi %78, %81 : i64
      %77 = func.call @expected_sign(%80, %arg1, %arg2) : (i64, i64, i64) -> f64
      %82 = llvm.load %47 : !llvm.ptr -> f64
      %83 = llvm.load %59 : !llvm.ptr -> f64
      %84 = arith.mulf %55, %83 : f64
      %85 = arith.mulf %84, %71 : f64
      %86 = arith.mulf %85, %77 : f64
      %87 = arith.addf %82, %86 : f64
      llvm.store %87, %47 : f64, !llvm.ptr
      %88 = llvm.load %49 : !llvm.ptr -> i64
      %89 = arith.constant 1 : i32
      %91 = arith.extsi %89 : i32 to i64
      %90 = arith.addi %88, %91 : i64
      llvm.store %90, %49 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %92 = arith.constant 1 : i32
    %93 = llvm.mlir.addressof @G_seen : !llvm.ptr
    %94 = llvm.load %93 : !llvm.ptr -> !llvm.ptr
    %95 = arith.trunci %92 : i32 to i8
    %96 = llvm.getelementptr %94[%18] : (!llvm.ptr, i64) -> !llvm.ptr, i8
    llvm.store %95, %96 : i8, !llvm.ptr
    %97 = llvm.load %47 : !llvm.ptr -> f64
    %98 = llvm.mlir.addressof @G_memo : !llvm.ptr
    %99 = llvm.load %98 : !llvm.ptr -> !llvm.ptr
    %100 = llvm.getelementptr %99[%18] : (!llvm.ptr, i64) -> !llvm.ptr, f64
    llvm.store %97, %100 : f64, !llvm.ptr
    %101 = llvm.load %47 : !llvm.ptr -> f64
    func.return %101 : f64
  }
  func.func @main() -> i32 {
    %102 = arith.constant 13 : i32
    %103 = arith.extsi %102 : i32 to i64
    %104 = arith.constant 46 : i32
    %105 = arith.extsi %104 : i32 to i64
    %106 = llvm.mlir.addressof @G_n : !llvm.ptr
    llvm.store %103, %106 : i64, !llvm.ptr
    %107 = llvm.mlir.addressof @G_tmax : !llvm.ptr
    llvm.store %105, %107 : i64, !llvm.ptr
    %108 = arith.constant 2 : i32
    %110 = arith.extsi %108 : i32 to i64
    %109 = arith.addi %103, %110 : i64
    %111 = arith.constant 2 : i32
    %113 = arith.extsi %111 : i32 to i64
    %112 = arith.addi %103, %113 : i64
    %114 = arith.muli %109, %112 : i64
    %115 = arith.constant 1 : i32
    %117 = arith.extsi %115 : i32 to i64
    %116 = arith.addi %105, %117 : i64
    %118 = arith.muli %114, %116 : i64
    %120 = arith.constant 8 : i32
    %121 = arith.extsi %120 : i32 to i64
    %119 = func.call @calloc(%118, %121) : (i64, i64) -> !llvm.ptr
    %122 = llvm.mlir.addressof @G_memo : !llvm.ptr
    llvm.store %119, %122 : !llvm.ptr, !llvm.ptr
    %124 = arith.constant 1 : i32
    %125 = arith.extsi %124 : i32 to i64
    %123 = func.call @calloc(%118, %125) : (i64, i64) -> !llvm.ptr
    %126 = llvm.mlir.addressof @G_seen : !llvm.ptr
    llvm.store %123, %126 : !llvm.ptr, !llvm.ptr
    %127 = llvm.mlir.addressof @G_memo : !llvm.ptr
    %128 = llvm.load %127 : !llvm.ptr -> !llvm.ptr
    %129 = llvm.mlir.zero : !llvm.ptr
    %130 = llvm.icmp "eq" %128, %129 : !llvm.ptr
    %131 = scf.if %130 -> (i1) {
      %132 = arith.constant true
      scf.yield %132 : i1
    } else {
      %133 = llvm.mlir.addressof @G_seen : !llvm.ptr
      %134 = llvm.load %133 : !llvm.ptr -> !llvm.ptr
      %135 = llvm.mlir.zero : !llvm.ptr
      %136 = llvm.icmp "eq" %134, %135 : !llvm.ptr
      scf.yield %136 : i1
    }
    cf.cond_br %131, ^bb12, ^bb13
    ^bb12:
      %137 = arith.constant 1 : i32
      func.return %137 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %139 = arith.constant 1 : i32
    %140 = arith.extsi %139 : i32 to i64
    %138 = func.call @expected_sign(%140, %103, %105) : (i64, i64, i64) -> f64
    %141 = arith.constant 1.0 : f32
    %143 = arith.extf %141 : f32 to f64
    %142 = arith.addf %143, %138 : f64
    %144 = arith.constant 2.0 : f32
    %146 = arith.extf %144 : f32 to f64
    %145 = arith.divf %142, %146 : f64
    %148 = arith.constant 10000000000.0 : f32
    %150 = arith.extf %148 : f32 to f64
    %149 = arith.mulf %145, %150 : f64
    %151 = arith.constant 0.5 : f32
    %153 = arith.extf %151 : f32 to f64
    %152 = arith.addf %149, %153 : f64
    %147 = func.call @floor(%152) : (f64) -> f64
    %154 = arith.fptosi %147 : f64 to i64
    %155 = arith.constant 5705032704 : i32
    %157 = arith.extsi %155 : i32 to i64
    %156 = arith.divsi %154, %157 : i64
    %158 = arith.constant 5705032704 : i32
    %160 = arith.extsi %158 : i32 to i64
    %159 = arith.remsi %154, %160 : i64
    %161 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %162 = llvm.call @printf(%161, %156, %159) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64, i64) -> i32
    %164 = llvm.mlir.addressof @G_seen : !llvm.ptr
    %165 = llvm.load %164 : !llvm.ptr -> !llvm.ptr
    func.call @free(%165) : (!llvm.ptr) -> ()
    %167 = llvm.mlir.addressof @G_memo : !llvm.ptr
    %168 = llvm.load %167 : !llvm.ptr -> !llvm.ptr
    func.call @free(%168) : (!llvm.ptr) -> ()
    %169 = arith.constant 0 : i32
    func.return %169 : i32
  }
}