Problem 653

Frictionless tube: d(10^9, 10^6+1, 500001).

Answer1130658687
Output1130658687
StatusPASS
Native helperno
Runtime140 ms
Peak memory8928 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 653
# Frictionless tube: d(10^9, 10^6+1, 500001).

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

function heapify(a: ptr<i64>, n: i64, i0: i64) -> void {
    let mut i: i64 = i0
    while true {
        let mut largest: i64 = i
        let l: i64 = 2 * i + 1
        let r: i64 = 2 * i + 2
        if l < n && a[l] > a[largest] { largest = l }
        if r < n && a[r] > a[largest] { largest = r }
        if largest == i { break }
        let t: i64 = a[i]
        a[i] = a[largest]
        a[largest] = t
        i = largest
    }
}

function heapsort(a: ptr<i64>, n: i64) -> void {
    let mut i: i64 = n / 2 - 1
    while i >= 0 {
        heapify(a, n, i)
        i = i - 1
    }
    i = n - 1
    while i > 0 {
        let t: i64 = a[0]
        a[0] = a[i]
        a[i] = t
        heapify(a, i, 0)
        i = i - 1
    }
}

function d(L: i64, N: i64, j: i64) -> i64 {
    let MOD: i64 = 32745673
    let mut r: i64 = 6563116
    let a: ptr<i64> = calloc(N, 8)
    if a == null { return -1 }
    let mut y: i64 = 0
    let mut i: i64 = 0
    while i < N {
        let g: i64 = (r % 1000) + 1
        y = y + g
        if r <= 10000000 {
            a[i] = 0 - y
        } else {
            a[i] = y
        }
        r = (r * r) % MOD
        i = i + 1
    }
    heapsort(a, N)
    let m: i64 = N - j + 1
    let am: i64 = a[m - 1]
    free(a)
    return (L - 20 * j + 10) + am
}

function main() -> i32 {
    printf("%lld\n", d(1000000000, 1000001, 500001))
    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; }

void heapify_ptr_i64_i64_i64(int64_t* a, int64_t n, int64_t i0);
void heapsort_ptr_i64_i64(int64_t* a, int64_t n);
int64_t d_i64_i64_i64(int64_t L, int64_t N, int64_t j);
int32_t main(void);



void heapify_ptr_i64_i64_i64(int64_t* a, int64_t n, int64_t i0) {
    int64_t i = i0;
    while (1) {
        int64_t largest = i;
        int64_t l = ((2 * i) + 1);
        int64_t r = ((2 * i) + 2);
        if ((l < n && a[l] > a[largest])) {
            largest = l;
        }
        if ((r < n && a[r] > a[largest])) {
            largest = r;
        }
        if (largest == i) {
            break;
        }
        int64_t t = a[i];
        a[i] = a[largest];
        a[largest] = t;
        i = largest;
    }
}

void heapsort_ptr_i64_i64(int64_t* a, int64_t n) {
    int64_t i = (FLOW_CHECKED_DIV((n), (2)) - 1);
    while (i >= 0) {
        heapify_ptr_i64_i64_i64(a, n, i);
        i = (i - 1);
    }
    i = (n - 1);
    while (i > 0) {
        int64_t t = a[0];
        a[0] = a[i];
        a[i] = t;
        heapify_ptr_i64_i64_i64(a, i, 0);
        i = (i - 1);
    }
}

int64_t d_i64_i64_i64(int64_t L, int64_t N, int64_t j) {
    int64_t MOD = 32745673;
    int64_t r = 6563116;
    int64_t* a = (int64_t*)(calloc(N, 8));
    if (a == NULL) {
        return (-1);
    }
    int64_t y = 0;
    int64_t i = 0;
    while (i < N) {
        int64_t g = (FLOW_CHECKED_MOD((r), (1000)) + 1);
        y = (y + g);
        if (r <= 10000000) {
            a[i] = (0 - y);
        } else {
            a[i] = y;
        }
        r = FLOW_CHECKED_MOD(((r * r)), (MOD));
        i = (i + 1);
    }
    heapsort_ptr_i64_i64(a, N);
    int64_t m = ((N - j) + 1);
    int64_t am = a[(m - 1)];
    free(a);
    return (((L - (20 * j)) + 10) + am);
}

int32_t main(void) {
    printf("%lld\n", d_i64_i64_i64(1000000000, 1000001, 500001));
    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 @heapify(%arg0: !llvm.ptr, %arg1: i64, %arg2: i64) -> () {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg2, %1 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %2 = arith.constant 1 : i1
    cf.cond_br %2, ^bb1, ^bb2
    ^bb1:
      %3 = llvm.load %1 : !llvm.ptr -> i64
      %4 = llvm.mlir.constant(1 : i64) : i64
      %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
      llvm.store %3, %5 : i64, !llvm.ptr
      %6 = arith.constant 2 : i32
      %7 = llvm.load %1 : !llvm.ptr -> i64
      %9 = arith.extsi %6 : i32 to i64
      %8 = arith.muli %9, %7 : i64
      %10 = arith.constant 1 : i32
      %12 = arith.extsi %10 : i32 to i64
      %11 = arith.addi %8, %12 : i64
      %13 = arith.constant 2 : i32
      %14 = llvm.load %1 : !llvm.ptr -> i64
      %16 = arith.extsi %13 : i32 to i64
      %15 = arith.muli %16, %14 : i64
      %17 = arith.constant 2 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.addi %15, %19 : i64
      %20 = arith.cmpi slt, %11, %arg1 : i64
      %21 = scf.if %20 -> (i1) {
        %23 = llvm.getelementptr %arg0[%11] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %22 = llvm.load %23 : !llvm.ptr -> i64
        %25 = llvm.load %5 : !llvm.ptr -> i64
        %26 = llvm.getelementptr %arg0[%25] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %24 = llvm.load %26 : !llvm.ptr -> i64
        %27 = arith.cmpi sgt, %22, %24 : i64
        scf.yield %27 : i1
      } else {
        %28 = arith.constant false
        scf.yield %28 : i1
      }
      cf.cond_br %21, ^bb3, ^bb4
      ^bb3:
        llvm.store %11, %5 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %29 = arith.cmpi slt, %18, %arg1 : i64
      %30 = scf.if %29 -> (i1) {
        %32 = llvm.getelementptr %arg0[%18] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %31 = llvm.load %32 : !llvm.ptr -> i64
        %34 = llvm.load %5 : !llvm.ptr -> i64
        %35 = llvm.getelementptr %arg0[%34] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %33 = llvm.load %35 : !llvm.ptr -> i64
        %36 = arith.cmpi sgt, %31, %33 : i64
        scf.yield %36 : i1
      } else {
        %37 = arith.constant false
        scf.yield %37 : i1
      }
      cf.cond_br %30, ^bb6, ^bb7
      ^bb6:
        llvm.store %18, %5 : i64, !llvm.ptr
        cf.br ^bb8
      ^bb7:
        cf.br ^bb8
      ^bb8:
      %38 = llvm.load %5 : !llvm.ptr -> i64
      %39 = llvm.load %1 : !llvm.ptr -> i64
      %40 = arith.cmpi eq, %38, %39 : i64
      cf.cond_br %40, ^bb9, ^bb10
      ^bb9:
        cf.br ^bb2
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %42 = llvm.load %1 : !llvm.ptr -> i64
      %43 = llvm.getelementptr %arg0[%42] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %41 = llvm.load %43 : !llvm.ptr -> i64
      %45 = llvm.load %5 : !llvm.ptr -> i64
      %46 = llvm.getelementptr %arg0[%45] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %44 = llvm.load %46 : !llvm.ptr -> i64
      %47 = llvm.load %1 : !llvm.ptr -> i64
      %48 = llvm.getelementptr %arg0[%47] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %44, %48 : i64, !llvm.ptr
      %49 = llvm.load %5 : !llvm.ptr -> i64
      %50 = llvm.getelementptr %arg0[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %41, %50 : i64, !llvm.ptr
      %51 = llvm.load %5 : !llvm.ptr -> i64
      llvm.store %51, %1 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    func.return
  }
  func.func @heapsort(%arg0: !llvm.ptr, %arg1: i64) -> () {
    %52 = arith.constant 2 : i32
    %54 = arith.extsi %52 : i32 to i64
    %53 = arith.divsi %arg1, %54 : i64
    %55 = arith.constant 1 : i32
    %57 = arith.extsi %55 : i32 to i64
    %56 = arith.subi %53, %57 : i64
    %58 = llvm.mlir.constant(1 : i64) : i64
    %59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
    llvm.store %56, %59 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %60 = llvm.load %59 : !llvm.ptr -> i64
    %61 = arith.constant 0 : i32
    %63 = arith.extsi %61 : i32 to i64
    %62 = arith.cmpi sge, %60, %63 : i64
    cf.cond_br %62, ^bb13, ^bb14
    ^bb13:
      %65 = llvm.load %59 : !llvm.ptr -> i64
      func.call @heapify(%arg0, %arg1, %65) : (!llvm.ptr, i64, i64) -> ()
      %66 = llvm.load %59 : !llvm.ptr -> i64
      %67 = arith.constant 1 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.subi %66, %69 : i64
      llvm.store %68, %59 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %70 = arith.constant 1 : i32
    %72 = arith.extsi %70 : i32 to i64
    %71 = arith.subi %arg1, %72 : i64
    llvm.store %71, %59 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %73 = llvm.load %59 : !llvm.ptr -> i64
    %74 = arith.constant 0 : i32
    %76 = arith.extsi %74 : i32 to i64
    %75 = arith.cmpi sgt, %73, %76 : i64
    cf.cond_br %75, ^bb16, ^bb17
    ^bb16:
      %78 = arith.constant 0 : i32
      %79 = arith.extsi %78 : i32 to i64
      %80 = llvm.getelementptr %arg0[%79] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %77 = llvm.load %80 : !llvm.ptr -> i64
      %82 = llvm.load %59 : !llvm.ptr -> i64
      %83 = llvm.getelementptr %arg0[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %81 = llvm.load %83 : !llvm.ptr -> i64
      %84 = arith.constant 0 : i32
      %85 = arith.extsi %84 : i32 to i64
      %86 = llvm.getelementptr %arg0[%85] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %81, %86 : i64, !llvm.ptr
      %87 = llvm.load %59 : !llvm.ptr -> i64
      %88 = llvm.getelementptr %arg0[%87] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %77, %88 : i64, !llvm.ptr
      %90 = llvm.load %59 : !llvm.ptr -> i64
      %91 = arith.constant 0 : i32
      %92 = arith.extsi %91 : i32 to i64
      func.call @heapify(%arg0, %90, %92) : (!llvm.ptr, i64, i64) -> ()
      %93 = llvm.load %59 : !llvm.ptr -> i64
      %94 = arith.constant 1 : i32
      %96 = arith.extsi %94 : i32 to i64
      %95 = arith.subi %93, %96 : i64
      llvm.store %95, %59 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    func.return
  }
  func.func @d(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %97 = arith.constant 32745673 : i32
    %98 = arith.extsi %97 : i32 to i64
    %99 = arith.constant 6563116 : i32
    %100 = arith.extsi %99 : i32 to i64
    %101 = llvm.mlir.constant(1 : i64) : i64
    %102 = llvm.alloca %101 x i64 : (i64) -> !llvm.ptr
    llvm.store %100, %102 : i64, !llvm.ptr
    %104 = arith.constant 8 : i32
    %105 = arith.extsi %104 : i32 to i64
    %103 = func.call @calloc(%arg1, %105) : (i64, i64) -> !llvm.ptr
    %106 = llvm.mlir.zero : !llvm.ptr
    %107 = llvm.icmp "eq" %103, %106 : !llvm.ptr
    cf.cond_br %107, ^bb18, ^bb19
    ^bb18:
      %108 = arith.constant 1 : i32
      %110 = arith.constant 0 : i32
      %109 = arith.subi %110, %108 : i32
      %111 = arith.extsi %109 : i32 to i64
      func.return %111 : i64
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %112 = arith.constant 0 : i32
    %113 = arith.extsi %112 : i32 to i64
    %114 = llvm.mlir.constant(1 : i64) : i64
    %115 = llvm.alloca %114 x i64 : (i64) -> !llvm.ptr
    llvm.store %113, %115 : i64, !llvm.ptr
    %116 = arith.constant 0 : i32
    %117 = arith.extsi %116 : i32 to i64
    %118 = llvm.mlir.constant(1 : i64) : i64
    %119 = llvm.alloca %118 x i64 : (i64) -> !llvm.ptr
    llvm.store %117, %119 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %120 = llvm.load %119 : !llvm.ptr -> i64
    %121 = arith.cmpi slt, %120, %arg1 : i64
    cf.cond_br %121, ^bb22, ^bb23
    ^bb22:
      %122 = llvm.load %102 : !llvm.ptr -> i64
      %123 = arith.constant 1000 : i32
      %125 = arith.extsi %123 : i32 to i64
      %124 = arith.remsi %122, %125 : i64
      %126 = arith.constant 1 : i32
      %128 = arith.extsi %126 : i32 to i64
      %127 = arith.addi %124, %128 : i64
      %129 = llvm.load %115 : !llvm.ptr -> i64
      %130 = arith.addi %129, %127 : i64
      llvm.store %130, %115 : i64, !llvm.ptr
      %131 = llvm.load %102 : !llvm.ptr -> i64
      %132 = arith.constant 10000000 : i32
      %134 = arith.extsi %132 : i32 to i64
      %133 = arith.cmpi sle, %131, %134 : i64
      cf.cond_br %133, ^bb24, ^bb25
      ^bb24:
        %135 = arith.constant 0 : i32
        %136 = llvm.load %115 : !llvm.ptr -> i64
        %138 = arith.extsi %135 : i32 to i64
        %137 = arith.subi %138, %136 : i64
        %139 = llvm.load %119 : !llvm.ptr -> i64
        %140 = llvm.getelementptr %103[%139] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %137, %140 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        %141 = llvm.load %115 : !llvm.ptr -> i64
        %142 = llvm.load %119 : !llvm.ptr -> i64
        %143 = llvm.getelementptr %103[%142] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %141, %143 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb26:
      %144 = llvm.load %102 : !llvm.ptr -> i64
      %145 = llvm.load %102 : !llvm.ptr -> i64
      %146 = arith.muli %144, %145 : i64
      %147 = arith.remsi %146, %98 : i64
      llvm.store %147, %102 : i64, !llvm.ptr
      %148 = llvm.load %119 : !llvm.ptr -> i64
      %149 = arith.constant 1 : i32
      %151 = arith.extsi %149 : i32 to i64
      %150 = arith.addi %148, %151 : i64
      llvm.store %150, %119 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    func.call @heapsort(%103, %arg1) : (!llvm.ptr, i64) -> ()
    %153 = arith.subi %arg1, %arg2 : i64
    %154 = arith.constant 1 : i32
    %156 = arith.extsi %154 : i32 to i64
    %155 = arith.addi %153, %156 : i64
    %158 = arith.constant 1 : i32
    %160 = arith.extsi %158 : i32 to i64
    %159 = arith.subi %155, %160 : i64
    %161 = llvm.getelementptr %103[%159] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %157 = llvm.load %161 : !llvm.ptr -> i64
    func.call @free(%103) : (!llvm.ptr) -> ()
    %163 = arith.constant 20 : i32
    %165 = arith.extsi %163 : i32 to i64
    %164 = arith.muli %165, %arg2 : i64
    %166 = arith.subi %arg0, %164 : i64
    %167 = arith.constant 10 : i32
    %169 = arith.extsi %167 : i32 to i64
    %168 = arith.addi %166, %169 : i64
    %170 = arith.addi %168, %157 : i64
    func.return %170 : i64
  }
  func.func @main() -> i32 {
    %171 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %173 = arith.constant 1000000000 : i32
    %174 = arith.constant 1000001 : i32
    %175 = arith.constant 500001 : i32
    %176 = arith.extsi %173 : i32 to i64
    %177 = arith.extsi %174 : i32 to i64
    %178 = arith.extsi %175 : i32 to i64
    %172 = func.call @d(%176, %177, %178) : (i64, i64, i64) -> i64
    %179 = llvm.call @printf(%171, %172) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %180 = arith.constant 0 : i32
    func.return %180 : i32
  }
}