Problem 726

Falling Bottles: S(10^4) mod 1000000033. Layered product with Mersenne prefix and odd-inverses.

Answer578040951
Output578040951
StatusPASS
Native helperno
Runtime290 ms
Peak memory1216 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 726
# Falling Bottles: S(10^4) mod 1000000033.
# Layered product with Mersenne prefix and odd-inverses.

const MOD: i64 = 1000000033

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

function modpow(b: i64, e: i64) -> i64 {
    let mut r: i64 = 1
    let mut bb: i64 = b % MOD
    let mut ee: i64 = e
    while ee > 0 {
        if (ee & 1) == 1 {
            r = r * bb % MOD
        }
        bb = bb * bb % MOD
        ee = ee >> 1
    }
    return r
}

function main() -> i32 {
    let n: i64 = 10000
    let inv_odd: ptr<i64> = malloc((n + 1) * 8)
    let mut k: i64 = 1
    while k <= n {
        inv_odd[k] = modpow(2 * k - 1, MOD - 2)
        k = k + 1
    }

    let mut cur_f: i64 = 1
    let mut total: i64 = 1
    let mut curN: i64 = 1
    let mut pow2: i64 = 2
    let mut mers_prefix: i64 = 1
    let mut odd_inv_prefix: i64 = 1

    let mut layer: i64 = 2
    while layer <= n {
        let start: i64 = curN + 1
        let end: i64 = curN + layer
        let mut x: i64 = start
        while x <= end {
            cur_f = cur_f * x % MOD
            x = x + 1
        }
        curN = end
        pow2 = pow2 * 2 % MOD
        mers_prefix = mers_prefix * ((pow2 - 1 + MOD) % MOD) % MOD
        odd_inv_prefix = odd_inv_prefix * inv_odd[layer] % MOD
        cur_f = cur_f * mers_prefix % MOD * odd_inv_prefix % MOD
        total = (total + cur_f) % MOD
        layer = layer + 1
    }

    free(inv_odd)
    printf("%lld\n", total)
    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 modpow_i64_i64(int64_t b, int64_t e);
int32_t main(void);

static const int64_t MOD = 1000000033;



int64_t modpow_i64_i64(int64_t b, int64_t e) {
    int64_t r = 1;
    int64_t bb = FLOW_CHECKED_MOD((b), (MOD));
    int64_t ee = e;
    while (ee > 0) {
        if ((ee & 1) == 1) {
            r = FLOW_CHECKED_MOD(((r * bb)), (MOD));
        }
        bb = FLOW_CHECKED_MOD(((bb * bb)), (MOD));
        ee = FLOW_CHECKED_SHR((ee), (1));
    }
    return r;
}

int32_t main(void) {
    int64_t n = 10000;
    int64_t* inv_odd = (int64_t*)(malloc(((n + 1) * 8)));
    int64_t k = 1;
    while (k <= n) {
        inv_odd[k] = modpow_i64_i64(((2 * k) - 1), (MOD - 2));
        k = (k + 1);
    }
    int64_t cur_f = 1;
    int64_t total = 1;
    int64_t curN = 1;
    int64_t pow2 = 2;
    int64_t mers_prefix = 1;
    int64_t odd_inv_prefix = 1;
    int64_t layer = 2;
    while (layer <= n) {
        int64_t start = (curN + 1);
        int64_t end = (curN + layer);
        int64_t x = start;
        while (x <= end) {
            cur_f = FLOW_CHECKED_MOD(((cur_f * x)), (MOD));
            x = (x + 1);
        }
        curN = end;
        pow2 = FLOW_CHECKED_MOD(((pow2 * 2)), (MOD));
        mers_prefix = FLOW_CHECKED_MOD(((mers_prefix * FLOW_CHECKED_MOD((((pow2 - 1) + MOD)), (MOD)))), (MOD));
        odd_inv_prefix = FLOW_CHECKED_MOD(((odd_inv_prefix * inv_odd[layer])), (MOD));
        cur_f = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((cur_f * mers_prefix)), (MOD)) * odd_inv_prefix)), (MOD));
        total = FLOW_CHECKED_MOD(((total + cur_f)), (MOD));
        layer = (layer + 1);
    }
    free(inv_odd);
    printf("%lld\n", total);
    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>
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000033 : i64) : i64
  func.func private @malloc(i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @modpow(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.constant 1 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = llvm.mlir.constant(1 : i64) : i64
    %3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
    llvm.store %1, %3 : i64, !llvm.ptr
    %4 = llvm.mlir.addressof @MOD : !llvm.ptr
    %5 = llvm.load %4 : !llvm.ptr -> i64
    %6 = arith.remsi %arg0, %5 : i64
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i64, !llvm.ptr
    %9 = llvm.mlir.constant(1 : i64) : i64
    %10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %10 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %11 = llvm.load %10 : !llvm.ptr -> i64
    %12 = arith.constant 0 : i32
    %14 = arith.extsi %12 : i32 to i64
    %13 = arith.cmpi sgt, %11, %14 : i64
    cf.cond_br %13, ^bb1, ^bb2
    ^bb1:
      %15 = llvm.load %10 : !llvm.ptr -> i64
      %16 = arith.constant 1 : i32
      %18 = arith.extsi %16 : i32 to i64
      %17 = arith.andi %15, %18 : i64
      %19 = arith.constant 1 : i32
      %21 = arith.extsi %19 : i32 to i64
      %20 = arith.cmpi eq, %17, %21 : i64
      cf.cond_br %20, ^bb3, ^bb4
      ^bb3:
        %22 = llvm.load %3 : !llvm.ptr -> i64
        %23 = llvm.load %8 : !llvm.ptr -> i64
        %24 = arith.muli %22, %23 : i64
        %25 = llvm.mlir.addressof @MOD : !llvm.ptr
        %26 = llvm.load %25 : !llvm.ptr -> i64
        %27 = arith.remsi %24, %26 : i64
        llvm.store %27, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %28 = llvm.load %8 : !llvm.ptr -> i64
      %29 = llvm.load %8 : !llvm.ptr -> i64
      %30 = arith.muli %28, %29 : i64
      %31 = llvm.mlir.addressof @MOD : !llvm.ptr
      %32 = llvm.load %31 : !llvm.ptr -> i64
      %33 = arith.remsi %30, %32 : i64
      llvm.store %33, %8 : i64, !llvm.ptr
      %34 = llvm.load %10 : !llvm.ptr -> i64
      %35 = arith.constant 1 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.shrsi %34, %37 : i64
      llvm.store %36, %10 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %38 = llvm.load %3 : !llvm.ptr -> i64
    func.return %38 : i64
  }
  func.func @main() -> i32 {
    %39 = arith.constant 10000 : i32
    %40 = arith.extsi %39 : i32 to i64
    %42 = arith.constant 1 : i32
    %44 = arith.extsi %42 : i32 to i64
    %43 = arith.addi %40, %44 : i64
    %45 = arith.constant 8 : i32
    %47 = arith.extsi %45 : i32 to i64
    %46 = arith.muli %43, %47 : i64
    %41 = func.call @malloc(%46) : (i64) -> !llvm.ptr
    %48 = arith.constant 1 : i32
    %49 = arith.extsi %48 : i32 to i64
    %50 = llvm.mlir.constant(1 : i64) : i64
    %51 = llvm.alloca %50 x i64 : (i64) -> !llvm.ptr
    llvm.store %49, %51 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %52 = llvm.load %51 : !llvm.ptr -> i64
    %53 = arith.cmpi sle, %52, %40 : i64
    cf.cond_br %53, ^bb7, ^bb8
    ^bb7:
      %55 = arith.constant 2 : i32
      %56 = llvm.load %51 : !llvm.ptr -> i64
      %58 = arith.extsi %55 : i32 to i64
      %57 = arith.muli %58, %56 : i64
      %59 = arith.constant 1 : i32
      %61 = arith.extsi %59 : i32 to i64
      %60 = arith.subi %57, %61 : i64
      %62 = llvm.mlir.addressof @MOD : !llvm.ptr
      %63 = llvm.load %62 : !llvm.ptr -> i64
      %64 = arith.constant 2 : i32
      %66 = arith.extsi %64 : i32 to i64
      %65 = arith.subi %63, %66 : i64
      %54 = func.call @modpow(%60, %65) : (i64, i64) -> i64
      %67 = llvm.load %51 : !llvm.ptr -> i64
      %68 = llvm.getelementptr %41[%67] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %54, %68 : i64, !llvm.ptr
      %69 = llvm.load %51 : !llvm.ptr -> i64
      %70 = arith.constant 1 : i32
      %72 = arith.extsi %70 : i32 to i64
      %71 = arith.addi %69, %72 : i64
      llvm.store %71, %51 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %73 = arith.constant 1 : i32
    %74 = arith.extsi %73 : i32 to i64
    %75 = llvm.mlir.constant(1 : i64) : i64
    %76 = llvm.alloca %75 x i64 : (i64) -> !llvm.ptr
    llvm.store %74, %76 : i64, !llvm.ptr
    %77 = arith.constant 1 : i32
    %78 = arith.extsi %77 : i32 to i64
    %79 = llvm.mlir.constant(1 : i64) : i64
    %80 = llvm.alloca %79 x i64 : (i64) -> !llvm.ptr
    llvm.store %78, %80 : i64, !llvm.ptr
    %81 = arith.constant 1 : i32
    %82 = arith.extsi %81 : i32 to i64
    %83 = llvm.mlir.constant(1 : i64) : i64
    %84 = llvm.alloca %83 x i64 : (i64) -> !llvm.ptr
    llvm.store %82, %84 : i64, !llvm.ptr
    %85 = arith.constant 2 : i32
    %86 = arith.extsi %85 : i32 to i64
    %87 = llvm.mlir.constant(1 : i64) : i64
    %88 = llvm.alloca %87 x i64 : (i64) -> !llvm.ptr
    llvm.store %86, %88 : i64, !llvm.ptr
    %89 = arith.constant 1 : i32
    %90 = arith.extsi %89 : i32 to i64
    %91 = llvm.mlir.constant(1 : i64) : i64
    %92 = llvm.alloca %91 x i64 : (i64) -> !llvm.ptr
    llvm.store %90, %92 : i64, !llvm.ptr
    %93 = arith.constant 1 : i32
    %94 = arith.extsi %93 : i32 to i64
    %95 = llvm.mlir.constant(1 : i64) : i64
    %96 = llvm.alloca %95 x i64 : (i64) -> !llvm.ptr
    llvm.store %94, %96 : i64, !llvm.ptr
    %97 = arith.constant 2 : i32
    %98 = arith.extsi %97 : i32 to i64
    %99 = llvm.mlir.constant(1 : i64) : i64
    %100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
    llvm.store %98, %100 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %101 = llvm.load %100 : !llvm.ptr -> i64
    %102 = arith.cmpi sle, %101, %40 : i64
    cf.cond_br %102, ^bb10, ^bb11
    ^bb10:
      %103 = llvm.load %84 : !llvm.ptr -> i64
      %104 = arith.constant 1 : i32
      %106 = arith.extsi %104 : i32 to i64
      %105 = arith.addi %103, %106 : i64
      %107 = llvm.load %84 : !llvm.ptr -> i64
      %108 = llvm.load %100 : !llvm.ptr -> i64
      %109 = arith.addi %107, %108 : i64
      %110 = llvm.mlir.constant(1 : i64) : i64
      %111 = llvm.alloca %110 x i64 : (i64) -> !llvm.ptr
      llvm.store %105, %111 : i64, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %112 = llvm.load %111 : !llvm.ptr -> i64
      %113 = arith.cmpi sle, %112, %109 : i64
      cf.cond_br %113, ^bb13, ^bb14
      ^bb13:
        %114 = llvm.load %76 : !llvm.ptr -> i64
        %115 = llvm.load %111 : !llvm.ptr -> i64
        %116 = arith.muli %114, %115 : i64
        %117 = llvm.mlir.addressof @MOD : !llvm.ptr
        %118 = llvm.load %117 : !llvm.ptr -> i64
        %119 = arith.remsi %116, %118 : i64
        llvm.store %119, %76 : i64, !llvm.ptr
        %120 = llvm.load %111 : !llvm.ptr -> i64
        %121 = arith.constant 1 : i32
        %123 = arith.extsi %121 : i32 to i64
        %122 = arith.addi %120, %123 : i64
        llvm.store %122, %111 : i64, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      llvm.store %109, %84 : i64, !llvm.ptr
      %124 = llvm.load %88 : !llvm.ptr -> i64
      %125 = arith.constant 2 : i32
      %127 = arith.extsi %125 : i32 to i64
      %126 = arith.muli %124, %127 : i64
      %128 = llvm.mlir.addressof @MOD : !llvm.ptr
      %129 = llvm.load %128 : !llvm.ptr -> i64
      %130 = arith.remsi %126, %129 : i64
      llvm.store %130, %88 : i64, !llvm.ptr
      %131 = llvm.load %92 : !llvm.ptr -> i64
      %132 = llvm.load %88 : !llvm.ptr -> i64
      %133 = arith.constant 1 : i32
      %135 = arith.extsi %133 : i32 to i64
      %134 = arith.subi %132, %135 : i64
      %136 = llvm.mlir.addressof @MOD : !llvm.ptr
      %137 = llvm.load %136 : !llvm.ptr -> i64
      %138 = arith.addi %134, %137 : i64
      %139 = llvm.mlir.addressof @MOD : !llvm.ptr
      %140 = llvm.load %139 : !llvm.ptr -> i64
      %141 = arith.remsi %138, %140 : i64
      %142 = arith.muli %131, %141 : i64
      %143 = llvm.mlir.addressof @MOD : !llvm.ptr
      %144 = llvm.load %143 : !llvm.ptr -> i64
      %145 = arith.remsi %142, %144 : i64
      llvm.store %145, %92 : i64, !llvm.ptr
      %146 = llvm.load %96 : !llvm.ptr -> i64
      %148 = llvm.load %100 : !llvm.ptr -> i64
      %149 = llvm.getelementptr %41[%148] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %147 = llvm.load %149 : !llvm.ptr -> i64
      %150 = arith.muli %146, %147 : i64
      %151 = llvm.mlir.addressof @MOD : !llvm.ptr
      %152 = llvm.load %151 : !llvm.ptr -> i64
      %153 = arith.remsi %150, %152 : i64
      llvm.store %153, %96 : i64, !llvm.ptr
      %154 = llvm.load %76 : !llvm.ptr -> i64
      %155 = llvm.load %92 : !llvm.ptr -> i64
      %156 = arith.muli %154, %155 : i64
      %157 = llvm.mlir.addressof @MOD : !llvm.ptr
      %158 = llvm.load %157 : !llvm.ptr -> i64
      %159 = arith.remsi %156, %158 : i64
      %160 = llvm.load %96 : !llvm.ptr -> i64
      %161 = arith.muli %159, %160 : i64
      %162 = llvm.mlir.addressof @MOD : !llvm.ptr
      %163 = llvm.load %162 : !llvm.ptr -> i64
      %164 = arith.remsi %161, %163 : i64
      llvm.store %164, %76 : i64, !llvm.ptr
      %165 = llvm.load %80 : !llvm.ptr -> i64
      %166 = llvm.load %76 : !llvm.ptr -> i64
      %167 = arith.addi %165, %166 : i64
      %168 = llvm.mlir.addressof @MOD : !llvm.ptr
      %169 = llvm.load %168 : !llvm.ptr -> i64
      %170 = arith.remsi %167, %169 : i64
      llvm.store %170, %80 : i64, !llvm.ptr
      %171 = llvm.load %100 : !llvm.ptr -> i64
      %172 = arith.constant 1 : i32
      %174 = arith.extsi %172 : i32 to i64
      %173 = arith.addi %171, %174 : i64
      llvm.store %173, %100 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    func.call @free(%41) : (!llvm.ptr) -> ()
    %176 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %177 = llvm.load %80 : !llvm.ptr -> i64
    %178 = llvm.call @printf(%176, %177) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %179 = arith.constant 0 : i32
    func.return %179 : i32
  }
}