Problem 266

Pseudo-Fortunate? No - sqrt of product of primes <=190, last 16 digits of floor(sqrt(P)).

Answer1096883702440585
Output1096883702440585
StatusPASS
Native helperno
Runtime1560 ms
Peak memory33888 KB
Time complexityO(n^3) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve-based search
VerdictSuboptimal

Flow source

# Project Euler 266
# Pseudo-Fortunate? No - sqrt of product of primes <=190, last 16 digits of floor(sqrt(P)).

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

function mulmod(a0: i64, b0: i64, mod: i64) -> i64 {
    let mut a: i64 = a0 % mod
    let mut b: i64 = b0 % mod
    let mut result: i64 = 0
    while b > 0 {
        if b % 2 == 1 {
            result = result + a
            if result >= mod { result = result - mod }
        }
        a = a + a
        if a >= mod { a = a - mod }
        b = b / 2
    }
    return result
}

function main() -> i32 {
    # primes <= 190
    let primes: ptr<i64> = calloc(50, 8)
    let mut pc: i64 = 0
    let mut n: i64 = 2
    while n <= 190 {
        let mut ok: bool = true
        let mut d: i64 = 2
        while d * d <= n {
            if n % d == 0 { ok = false; break }
            d = d + 1
        }
        if ok {
            primes[pc] = n
            pc = pc + 1
        }
        n = n + 1
    }
    let logs: ptr<f64> = calloc(pc, 8)
    let mut log_product: f64 = 0.0
    let mut i: i64 = 0
    while i < pc {
        logs[i] = log(primes[i] as f64)
        log_product = log_product + logs[i]
        i = i + 1
    }
    let log_root: f64 = log_product * 0.5
    let half: i64 = pc / 2
    let one: i64 = 1
    let right_n: i64 = one << half
    let right_log: ptr<f64> = calloc(right_n, 8)
    let right_mask: ptr<i64> = calloc(right_n, 8)
    let mut rc: i64 = 0
    let mut bitmask: i64 = 0
    while bitmask < right_n {
        let mut log_right: f64 = 0.0
        let mut pos: i64 = 0
        while pos < half {
            if (bitmask & (one << pos)) != 0 {
                log_right = log_right + logs[pos + half]
            }
            pos = pos + 1
        }
        if log_right <= log_root {
            right_log[rc] = log_right
            right_mask[rc] = bitmask
            rc = rc + 1
        }
        bitmask = bitmask + 1
    }
    # sort right by log (shell sort)
    let mut gap: i64 = rc / 2
    while gap > 0 {
        i = gap
        while i < rc {
            let mut j: i64 = i
            let key_l: f64 = right_log[i]
            let key_m: i64 = right_mask[i]
            while j >= gap && right_log[j - gap] > key_l {
                right_log[j] = right_log[j - gap]
                right_mask[j] = right_mask[j - gap]
                j = j - gap
            }
            right_log[j] = key_l
            right_mask[j] = key_m
            i = i + 1
        }
        gap = gap / 2
    }
    let mut best: f64 = 0.0
    let mut left_mask: i64 = 0
    let mut right_best: i64 = 0
    bitmask = 0
    while bitmask < right_n {
        let mut log_left: f64 = 0.0
        let mut pos: i64 = 0
        while pos < half {
            if (bitmask & (one << pos)) != 0 {
                log_left = log_left + logs[pos]
            }
            pos = pos + 1
        }
        let missing: f64 = log_root - log_left
        # upper_bound binary search
        let mut lo: i64 = 0
        let mut hi: i64 = rc
        while lo < hi {
            let mid: i64 = (lo + hi) / 2
            if right_log[mid] <= missing {
                lo = mid + 1
            } else {
                hi = mid
            }
        }
        if lo > 0 {
            let idx: i64 = lo - 1
            let cand: f64 = log_left + right_log[idx]
            if cand > best {
                best = cand
                left_mask = bitmask
                right_best = right_mask[idx]
            }
        }
        bitmask = bitmask + 1
    }
    let mod: i64 = 10000000000000000
    let mut result: i64 = 1
    i = 0
    while i < half {
        if (left_mask & 1) != 0 {
            result = mulmod(result, primes[i], mod)
        }
        left_mask = left_mask / 2
        i = i + 1
    }
    i = half
    while i < pc {
        if (right_best & 1) != 0 {
            result = mulmod(result, primes[i], mod)
        }
        right_best = right_best / 2
        i = i + 1
    }
    printf("%lld\n", result)
    free(primes); free(logs); free(right_log); free(right_mask)
    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 mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod);
int32_t main(void);





int64_t mulmod_i64_i64_i64(int64_t a0, int64_t b0, int64_t mod) {
    int64_t a = FLOW_CHECKED_MOD((a0), (mod));
    int64_t b = FLOW_CHECKED_MOD((b0), (mod));
    int64_t result = 0;
    while (b > 0) {
        if (FLOW_CHECKED_MOD((b), (2)) == 1) {
            result = (result + a);
            if (result >= mod) {
                result = (result - mod);
            }
        }
        a = (a + a);
        if (a >= mod) {
            a = (a - mod);
        }
        b = FLOW_CHECKED_DIV((b), (2));
    }
    return result;
}

int32_t main(void) {
    int64_t* primes = (int64_t*)(calloc(50, 8));
    int64_t pc = 0;
    int64_t n = 2;
    while (n <= 190) {
        bool ok = 1;
        int64_t d = 2;
        while ((d * d) <= n) {
            if (FLOW_CHECKED_MOD((n), (d)) == 0) {
                ok = 0;
                break;
            }
            d = (d + 1);
        }
        if (ok) {
            primes[pc] = n;
            pc = (pc + 1);
        }
        n = (n + 1);
    }
    double* logs = (double*)(calloc(pc, 8));
    double log_product = 0.0;
    int64_t i = 0;
    while (i < pc) {
        logs[i] = log(((double)(primes[i])));
        log_product = (log_product + logs[i]);
        i = (i + 1);
    }
    double log_root = (log_product * 0.5);
    int64_t half = FLOW_CHECKED_DIV((pc), (2));
    int64_t one = 1;
    int64_t right_n = FLOW_CHECKED_SHL((one), (half));
    double* right_log = (double*)(calloc(right_n, 8));
    int64_t* right_mask = (int64_t*)(calloc(right_n, 8));
    int64_t rc = 0;
    int64_t bitmask = 0;
    while (bitmask < right_n) {
        double log_right = 0.0;
        int64_t pos = 0;
        while (pos < half) {
            if ((bitmask & FLOW_CHECKED_SHL((one), (pos))) != 0) {
                log_right = (log_right + logs[(pos + half)]);
            }
            pos = (pos + 1);
        }
        if (log_right <= log_root) {
            right_log[rc] = log_right;
            right_mask[rc] = bitmask;
            rc = (rc + 1);
        }
        bitmask = (bitmask + 1);
    }
    int64_t gap = FLOW_CHECKED_DIV((rc), (2));
    while (gap > 0) {
        i = gap;
        while (i < rc) {
            int64_t j = i;
            double key_l = right_log[i];
            int64_t key_m = right_mask[i];
            while ((j >= gap && right_log[(j - gap)] > key_l)) {
                right_log[j] = right_log[(j - gap)];
                right_mask[j] = right_mask[(j - gap)];
                j = (j - gap);
            }
            right_log[j] = key_l;
            right_mask[j] = key_m;
            i = (i + 1);
        }
        gap = FLOW_CHECKED_DIV((gap), (2));
    }
    double best = 0.0;
    int64_t left_mask = 0;
    int64_t right_best = 0;
    bitmask = 0;
    while (bitmask < right_n) {
        double log_left = 0.0;
        int64_t pos = 0;
        while (pos < half) {
            if ((bitmask & FLOW_CHECKED_SHL((one), (pos))) != 0) {
                log_left = (log_left + logs[pos]);
            }
            pos = (pos + 1);
        }
        double missing = (log_root - log_left);
        int64_t lo = 0;
        int64_t hi = rc;
        while (lo < hi) {
            int64_t mid = FLOW_CHECKED_DIV(((lo + hi)), (2));
            if (right_log[mid] <= missing) {
                lo = (mid + 1);
            } else {
                hi = mid;
            }
        }
        if (lo > 0) {
            int64_t idx = (lo - 1);
            double cand = (log_left + right_log[idx]);
            if (cand > best) {
                best = cand;
                left_mask = bitmask;
                right_best = right_mask[idx];
            }
        }
        bitmask = (bitmask + 1);
    }
    int64_t mod = 10000000000000000;
    int64_t result = 1;
    i = 0;
    while (i < half) {
        if ((left_mask & 1) != 0) {
            result = mulmod_i64_i64_i64(result, primes[i], mod);
        }
        left_mask = FLOW_CHECKED_DIV((left_mask), (2));
        i = (i + 1);
    }
    i = half;
    while (i < pc) {
        if ((right_best & 1) != 0) {
            result = mulmod_i64_i64_i64(result, primes[i], mod);
        }
        right_best = FLOW_CHECKED_DIV((right_best), (2));
        i = (i + 1);
    }
    printf("%lld\n", result);
    free(primes);
    free(logs);
    free(right_log);
    free(right_mask);
    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 private @log(f64) -> f64
  func.func private @exp(f64) -> f64
  func.func @mulmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = arith.remsi %arg0, %arg2 : i64
    %1 = llvm.mlir.constant(1 : i64) : i64
    %2 = llvm.alloca %1 x i64 : (i64) -> !llvm.ptr
    llvm.store %0, %2 : i64, !llvm.ptr
    %3 = arith.remsi %arg1, %arg2 : 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 0 : i32
    %7 = arith.extsi %6 : i32 to i64
    %8 = llvm.mlir.constant(1 : i64) : i64
    %9 = llvm.alloca %8 x i64 : (i64) -> !llvm.ptr
    llvm.store %7, %9 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %10 = llvm.load %5 : !llvm.ptr -> i64
    %11 = arith.constant 0 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.cmpi sgt, %10, %13 : i64
    cf.cond_br %12, ^bb1, ^bb2
    ^bb1:
      %14 = llvm.load %5 : !llvm.ptr -> i64
      %15 = arith.constant 2 : i32
      %17 = arith.extsi %15 : i32 to i64
      %16 = arith.remsi %14, %17 : i64
      %18 = arith.constant 1 : i32
      %20 = arith.extsi %18 : i32 to i64
      %19 = arith.cmpi eq, %16, %20 : i64
      cf.cond_br %19, ^bb3, ^bb4
      ^bb3:
        %21 = llvm.load %9 : !llvm.ptr -> i64
        %22 = llvm.load %2 : !llvm.ptr -> i64
        %23 = arith.addi %21, %22 : i64
        llvm.store %23, %9 : i64, !llvm.ptr
        %24 = llvm.load %9 : !llvm.ptr -> i64
        %25 = arith.cmpi sge, %24, %arg2 : i64
        cf.cond_br %25, ^bb6, ^bb7
        ^bb6:
          %26 = llvm.load %9 : !llvm.ptr -> i64
          %27 = arith.subi %26, %arg2 : i64
          llvm.store %27, %9 : i64, !llvm.ptr
          cf.br ^bb8
        ^bb7:
          cf.br ^bb8
        ^bb8:
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %28 = llvm.load %2 : !llvm.ptr -> i64
      %29 = llvm.load %2 : !llvm.ptr -> i64
      %30 = arith.addi %28, %29 : i64
      llvm.store %30, %2 : i64, !llvm.ptr
      %31 = llvm.load %2 : !llvm.ptr -> i64
      %32 = arith.cmpi sge, %31, %arg2 : i64
      cf.cond_br %32, ^bb9, ^bb10
      ^bb9:
        %33 = llvm.load %2 : !llvm.ptr -> i64
        %34 = arith.subi %33, %arg2 : i64
        llvm.store %34, %2 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %35 = llvm.load %5 : !llvm.ptr -> i64
      %36 = arith.constant 2 : i32
      %38 = arith.extsi %36 : i32 to i64
      %37 = arith.divsi %35, %38 : i64
      llvm.store %37, %5 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %39 = llvm.load %9 : !llvm.ptr -> i64
    func.return %39 : i64
  }
  func.func @main() -> i32 {
    %41 = arith.constant 50 : i32
    %42 = arith.constant 8 : i32
    %43 = arith.extsi %41 : i32 to i64
    %44 = arith.extsi %42 : i32 to i64
    %40 = func.call @calloc(%43, %44) : (i64, i64) -> !llvm.ptr
    %45 = arith.constant 0 : i32
    %46 = arith.extsi %45 : i32 to i64
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
    llvm.store %46, %48 : i64, !llvm.ptr
    %49 = arith.constant 2 : i32
    %50 = arith.extsi %49 : i32 to i64
    %51 = llvm.mlir.constant(1 : i64) : i64
    %52 = llvm.alloca %51 x i64 : (i64) -> !llvm.ptr
    llvm.store %50, %52 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %53 = llvm.load %52 : !llvm.ptr -> i64
    %54 = arith.constant 190 : i32
    %56 = arith.extsi %54 : i32 to i64
    %55 = arith.cmpi sle, %53, %56 : i64
    cf.cond_br %55, ^bb13, ^bb14
    ^bb13:
      %57 = arith.constant 1 : i1
      %58 = llvm.mlir.constant(1 : i64) : i64
      %59 = llvm.alloca %58 x i1 : (i64) -> !llvm.ptr
      llvm.store %57, %59 : i1, !llvm.ptr
      %60 = arith.constant 2 : i32
      %61 = arith.extsi %60 : i32 to i64
      %62 = llvm.mlir.constant(1 : i64) : i64
      %63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
      llvm.store %61, %63 : i64, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %64 = llvm.load %63 : !llvm.ptr -> i64
      %65 = llvm.load %63 : !llvm.ptr -> i64
      %66 = arith.muli %64, %65 : i64
      %67 = llvm.load %52 : !llvm.ptr -> i64
      %68 = arith.cmpi sle, %66, %67 : i64
      cf.cond_br %68, ^bb16, ^bb17
      ^bb16:
        %69 = llvm.load %52 : !llvm.ptr -> i64
        %70 = llvm.load %63 : !llvm.ptr -> i64
        %71 = arith.remsi %69, %70 : i64
        %72 = arith.constant 0 : i32
        %74 = arith.extsi %72 : i32 to i64
        %73 = arith.cmpi eq, %71, %74 : i64
        cf.cond_br %73, ^bb18, ^bb19
        ^bb18:
          %75 = arith.constant 0 : i1
          llvm.store %75, %59 : i1, !llvm.ptr
          cf.br ^bb17
        ^bb19:
          cf.br ^bb20
        ^bb20:
        %76 = llvm.load %63 : !llvm.ptr -> i64
        %77 = arith.constant 1 : i32
        %79 = arith.extsi %77 : i32 to i64
        %78 = arith.addi %76, %79 : i64
        llvm.store %78, %63 : i64, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %80 = llvm.load %59 : !llvm.ptr -> i1
      cf.cond_br %80, ^bb21, ^bb22
      ^bb21:
        %81 = llvm.load %52 : !llvm.ptr -> i64
        %82 = llvm.load %48 : !llvm.ptr -> i64
        %83 = llvm.getelementptr %40[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %81, %83 : i64, !llvm.ptr
        %84 = llvm.load %48 : !llvm.ptr -> i64
        %85 = arith.constant 1 : i32
        %87 = arith.extsi %85 : i32 to i64
        %86 = arith.addi %84, %87 : i64
        llvm.store %86, %48 : i64, !llvm.ptr
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %88 = llvm.load %52 : !llvm.ptr -> i64
      %89 = arith.constant 1 : i32
      %91 = arith.extsi %89 : i32 to i64
      %90 = arith.addi %88, %91 : i64
      llvm.store %90, %52 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %93 = llvm.load %48 : !llvm.ptr -> i64
    %94 = arith.constant 8 : i32
    %95 = arith.extsi %94 : i32 to i64
    %92 = func.call @calloc(%93, %95) : (i64, i64) -> !llvm.ptr
    %96 = arith.constant 0.0 : f32
    %97 = arith.extf %96 : f32 to f64
    %98 = llvm.mlir.constant(1 : i64) : i64
    %99 = llvm.alloca %98 x f64 : (i64) -> !llvm.ptr
    llvm.store %97, %99 : f64, !llvm.ptr
    %100 = arith.constant 0 : i32
    %101 = arith.extsi %100 : i32 to i64
    %102 = llvm.mlir.constant(1 : i64) : i64
    %103 = llvm.alloca %102 x i64 : (i64) -> !llvm.ptr
    llvm.store %101, %103 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %104 = llvm.load %103 : !llvm.ptr -> i64
    %105 = llvm.load %48 : !llvm.ptr -> i64
    %106 = arith.cmpi slt, %104, %105 : i64
    cf.cond_br %106, ^bb25, ^bb26
    ^bb25:
      %108 = llvm.load %103 : !llvm.ptr -> i64
      %109 = llvm.getelementptr %40[%108] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %107 = llvm.load %109 : !llvm.ptr -> i64
      %110 = arith.sitofp %107 : i64 to f64
      %111 = math.log %110 : f64
      %112 = llvm.load %103 : !llvm.ptr -> i64
      %113 = llvm.getelementptr %92[%112] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      llvm.store %111, %113 : f64, !llvm.ptr
      %114 = llvm.load %99 : !llvm.ptr -> f64
      %116 = llvm.load %103 : !llvm.ptr -> i64
      %117 = llvm.getelementptr %92[%116] : (!llvm.ptr, i64) -> !llvm.ptr, f64
      %115 = llvm.load %117 : !llvm.ptr -> f64
      %118 = arith.addf %114, %115 : f64
      llvm.store %118, %99 : f64, !llvm.ptr
      %119 = llvm.load %103 : !llvm.ptr -> i64
      %120 = arith.constant 1 : i32
      %122 = arith.extsi %120 : i32 to i64
      %121 = arith.addi %119, %122 : i64
      llvm.store %121, %103 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %123 = llvm.load %99 : !llvm.ptr -> f64
    %124 = arith.constant 0.5 : f32
    %126 = arith.extf %124 : f32 to f64
    %125 = arith.mulf %123, %126 : f64
    %127 = llvm.load %48 : !llvm.ptr -> i64
    %128 = arith.constant 2 : i32
    %130 = arith.extsi %128 : i32 to i64
    %129 = arith.divsi %127, %130 : i64
    %131 = arith.constant 1 : i32
    %132 = arith.extsi %131 : i32 to i64
    %133 = arith.shli %132, %129 : i64
    %135 = arith.constant 8 : i32
    %136 = arith.extsi %135 : i32 to i64
    %134 = func.call @calloc(%133, %136) : (i64, i64) -> !llvm.ptr
    %138 = arith.constant 8 : i32
    %139 = arith.extsi %138 : i32 to i64
    %137 = func.call @calloc(%133, %139) : (i64, i64) -> !llvm.ptr
    %140 = arith.constant 0 : i32
    %141 = arith.extsi %140 : i32 to i64
    %142 = llvm.mlir.constant(1 : i64) : i64
    %143 = llvm.alloca %142 x i64 : (i64) -> !llvm.ptr
    llvm.store %141, %143 : i64, !llvm.ptr
    %144 = arith.constant 0 : i32
    %145 = arith.extsi %144 : i32 to i64
    %146 = llvm.mlir.constant(1 : i64) : i64
    %147 = llvm.alloca %146 x i64 : (i64) -> !llvm.ptr
    llvm.store %145, %147 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %148 = llvm.load %147 : !llvm.ptr -> i64
    %149 = arith.cmpi slt, %148, %133 : i64
    cf.cond_br %149, ^bb28, ^bb29
    ^bb28:
      %150 = arith.constant 0.0 : f32
      %151 = arith.extf %150 : f32 to f64
      %152 = llvm.mlir.constant(1 : i64) : i64
      %153 = llvm.alloca %152 x f64 : (i64) -> !llvm.ptr
      llvm.store %151, %153 : f64, !llvm.ptr
      %154 = arith.constant 0 : i32
      %155 = arith.extsi %154 : i32 to i64
      %156 = llvm.mlir.constant(1 : i64) : i64
      %157 = llvm.alloca %156 x i64 : (i64) -> !llvm.ptr
      llvm.store %155, %157 : i64, !llvm.ptr
      cf.br ^bb30
      ^bb30:
      %158 = llvm.load %157 : !llvm.ptr -> i64
      %159 = arith.cmpi slt, %158, %129 : i64
      cf.cond_br %159, ^bb31, ^bb32
      ^bb31:
        %160 = llvm.load %147 : !llvm.ptr -> i64
        %161 = llvm.load %157 : !llvm.ptr -> i64
        %162 = arith.shli %132, %161 : i64
        %163 = arith.andi %160, %162 : i64
        %164 = arith.constant 0 : i32
        %166 = arith.extsi %164 : i32 to i64
        %165 = arith.cmpi ne, %163, %166 : i64
        cf.cond_br %165, ^bb33, ^bb34
        ^bb33:
          %167 = llvm.load %153 : !llvm.ptr -> f64
          %169 = llvm.load %157 : !llvm.ptr -> i64
          %170 = arith.addi %169, %129 : i64
          %171 = llvm.getelementptr %92[%170] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          %168 = llvm.load %171 : !llvm.ptr -> f64
          %172 = arith.addf %167, %168 : f64
          llvm.store %172, %153 : f64, !llvm.ptr
          cf.br ^bb35
        ^bb34:
          cf.br ^bb35
        ^bb35:
        %173 = llvm.load %157 : !llvm.ptr -> i64
        %174 = arith.constant 1 : i32
        %176 = arith.extsi %174 : i32 to i64
        %175 = arith.addi %173, %176 : i64
        llvm.store %175, %157 : i64, !llvm.ptr
        cf.br ^bb30
      ^bb32:
      %177 = llvm.load %153 : !llvm.ptr -> f64
      %178 = arith.cmpf ole, %177, %125 : f64
      cf.cond_br %178, ^bb36, ^bb37
      ^bb36:
        %179 = llvm.load %153 : !llvm.ptr -> f64
        %180 = llvm.load %143 : !llvm.ptr -> i64
        %181 = llvm.getelementptr %134[%180] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %179, %181 : f64, !llvm.ptr
        %182 = llvm.load %147 : !llvm.ptr -> i64
        %183 = llvm.load %143 : !llvm.ptr -> i64
        %184 = llvm.getelementptr %137[%183] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %182, %184 : i64, !llvm.ptr
        %185 = llvm.load %143 : !llvm.ptr -> i64
        %186 = arith.constant 1 : i32
        %188 = arith.extsi %186 : i32 to i64
        %187 = arith.addi %185, %188 : i64
        llvm.store %187, %143 : i64, !llvm.ptr
        cf.br ^bb38
      ^bb37:
        cf.br ^bb38
      ^bb38:
      %189 = llvm.load %147 : !llvm.ptr -> i64
      %190 = arith.constant 1 : i32
      %192 = arith.extsi %190 : i32 to i64
      %191 = arith.addi %189, %192 : i64
      llvm.store %191, %147 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %193 = llvm.load %143 : !llvm.ptr -> i64
    %194 = arith.constant 2 : i32
    %196 = arith.extsi %194 : i32 to i64
    %195 = arith.divsi %193, %196 : i64
    %197 = llvm.mlir.constant(1 : i64) : i64
    %198 = llvm.alloca %197 x i64 : (i64) -> !llvm.ptr
    llvm.store %195, %198 : i64, !llvm.ptr
    cf.br ^bb39
    ^bb39:
    %199 = llvm.load %198 : !llvm.ptr -> i64
    %200 = arith.constant 0 : i32
    %202 = arith.extsi %200 : i32 to i64
    %201 = arith.cmpi sgt, %199, %202 : i64
    cf.cond_br %201, ^bb40, ^bb41
    ^bb40:
      %203 = llvm.load %198 : !llvm.ptr -> i64
      llvm.store %203, %103 : i64, !llvm.ptr
      cf.br ^bb42
      ^bb42:
      %204 = llvm.load %103 : !llvm.ptr -> i64
      %205 = llvm.load %143 : !llvm.ptr -> i64
      %206 = arith.cmpi slt, %204, %205 : i64
      cf.cond_br %206, ^bb43, ^bb44
      ^bb43:
        %207 = llvm.load %103 : !llvm.ptr -> i64
        %208 = llvm.mlir.constant(1 : i64) : i64
        %209 = llvm.alloca %208 x i64 : (i64) -> !llvm.ptr
        llvm.store %207, %209 : i64, !llvm.ptr
        %211 = llvm.load %103 : !llvm.ptr -> i64
        %212 = llvm.getelementptr %134[%211] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %210 = llvm.load %212 : !llvm.ptr -> f64
        %214 = llvm.load %103 : !llvm.ptr -> i64
        %215 = llvm.getelementptr %137[%214] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %213 = llvm.load %215 : !llvm.ptr -> i64
        cf.br ^bb45
        ^bb45:
        %216 = llvm.load %209 : !llvm.ptr -> i64
        %217 = llvm.load %198 : !llvm.ptr -> i64
        %218 = arith.cmpi sge, %216, %217 : i64
        %219 = scf.if %218 -> (i1) {
          %221 = llvm.load %209 : !llvm.ptr -> i64
          %222 = llvm.load %198 : !llvm.ptr -> i64
          %223 = arith.subi %221, %222 : i64
          %224 = llvm.getelementptr %134[%223] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          %220 = llvm.load %224 : !llvm.ptr -> f64
          %225 = arith.cmpf ogt, %220, %210 : f64
          scf.yield %225 : i1
        } else {
          %226 = arith.constant false
          scf.yield %226 : i1
        }
        cf.cond_br %219, ^bb46, ^bb47
        ^bb46:
          %228 = llvm.load %209 : !llvm.ptr -> i64
          %229 = llvm.load %198 : !llvm.ptr -> i64
          %230 = arith.subi %228, %229 : i64
          %231 = llvm.getelementptr %134[%230] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          %227 = llvm.load %231 : !llvm.ptr -> f64
          %232 = llvm.load %209 : !llvm.ptr -> i64
          %233 = llvm.getelementptr %134[%232] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          llvm.store %227, %233 : f64, !llvm.ptr
          %235 = llvm.load %209 : !llvm.ptr -> i64
          %236 = llvm.load %198 : !llvm.ptr -> i64
          %237 = arith.subi %235, %236 : i64
          %238 = llvm.getelementptr %137[%237] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %234 = llvm.load %238 : !llvm.ptr -> i64
          %239 = llvm.load %209 : !llvm.ptr -> i64
          %240 = llvm.getelementptr %137[%239] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %234, %240 : i64, !llvm.ptr
          %241 = llvm.load %209 : !llvm.ptr -> i64
          %242 = llvm.load %198 : !llvm.ptr -> i64
          %243 = arith.subi %241, %242 : i64
          llvm.store %243, %209 : i64, !llvm.ptr
          cf.br ^bb45
        ^bb47:
        %244 = llvm.load %209 : !llvm.ptr -> i64
        %245 = llvm.getelementptr %134[%244] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        llvm.store %210, %245 : f64, !llvm.ptr
        %246 = llvm.load %209 : !llvm.ptr -> i64
        %247 = llvm.getelementptr %137[%246] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %213, %247 : i64, !llvm.ptr
        %248 = llvm.load %103 : !llvm.ptr -> i64
        %249 = arith.constant 1 : i32
        %251 = arith.extsi %249 : i32 to i64
        %250 = arith.addi %248, %251 : i64
        llvm.store %250, %103 : i64, !llvm.ptr
        cf.br ^bb42
      ^bb44:
      %252 = llvm.load %198 : !llvm.ptr -> i64
      %253 = arith.constant 2 : i32
      %255 = arith.extsi %253 : i32 to i64
      %254 = arith.divsi %252, %255 : i64
      llvm.store %254, %198 : i64, !llvm.ptr
      cf.br ^bb39
    ^bb41:
    %256 = arith.constant 0.0 : f32
    %257 = arith.extf %256 : f32 to f64
    %258 = llvm.mlir.constant(1 : i64) : i64
    %259 = llvm.alloca %258 x f64 : (i64) -> !llvm.ptr
    llvm.store %257, %259 : f64, !llvm.ptr
    %260 = arith.constant 0 : i32
    %261 = arith.extsi %260 : i32 to i64
    %262 = llvm.mlir.constant(1 : i64) : i64
    %263 = llvm.alloca %262 x i64 : (i64) -> !llvm.ptr
    llvm.store %261, %263 : i64, !llvm.ptr
    %264 = arith.constant 0 : i32
    %265 = arith.extsi %264 : i32 to i64
    %266 = llvm.mlir.constant(1 : i64) : i64
    %267 = llvm.alloca %266 x i64 : (i64) -> !llvm.ptr
    llvm.store %265, %267 : i64, !llvm.ptr
    %268 = arith.constant 0 : i32
    %269 = arith.extsi %268 : i32 to i64
    llvm.store %269, %147 : i64, !llvm.ptr
    cf.br ^bb48
    ^bb48:
    %270 = llvm.load %147 : !llvm.ptr -> i64
    %271 = arith.cmpi slt, %270, %133 : i64
    cf.cond_br %271, ^bb49, ^bb50
    ^bb49:
      %272 = arith.constant 0.0 : f32
      %273 = arith.extf %272 : f32 to f64
      %274 = llvm.mlir.constant(1 : i64) : i64
      %275 = llvm.alloca %274 x f64 : (i64) -> !llvm.ptr
      llvm.store %273, %275 : f64, !llvm.ptr
      %276 = arith.constant 0 : i32
      %277 = arith.extsi %276 : i32 to i64
      %278 = llvm.mlir.constant(1 : i64) : i64
      %279 = llvm.alloca %278 x i64 : (i64) -> !llvm.ptr
      llvm.store %277, %279 : i64, !llvm.ptr
      cf.br ^bb51
      ^bb51:
      %280 = llvm.load %279 : !llvm.ptr -> i64
      %281 = arith.cmpi slt, %280, %129 : i64
      cf.cond_br %281, ^bb52, ^bb53
      ^bb52:
        %282 = llvm.load %147 : !llvm.ptr -> i64
        %283 = llvm.load %279 : !llvm.ptr -> i64
        %284 = arith.shli %132, %283 : i64
        %285 = arith.andi %282, %284 : i64
        %286 = arith.constant 0 : i32
        %288 = arith.extsi %286 : i32 to i64
        %287 = arith.cmpi ne, %285, %288 : i64
        cf.cond_br %287, ^bb54, ^bb55
        ^bb54:
          %289 = llvm.load %275 : !llvm.ptr -> f64
          %291 = llvm.load %279 : !llvm.ptr -> i64
          %292 = llvm.getelementptr %92[%291] : (!llvm.ptr, i64) -> !llvm.ptr, f64
          %290 = llvm.load %292 : !llvm.ptr -> f64
          %293 = arith.addf %289, %290 : f64
          llvm.store %293, %275 : f64, !llvm.ptr
          cf.br ^bb56
        ^bb55:
          cf.br ^bb56
        ^bb56:
        %294 = llvm.load %279 : !llvm.ptr -> i64
        %295 = arith.constant 1 : i32
        %297 = arith.extsi %295 : i32 to i64
        %296 = arith.addi %294, %297 : i64
        llvm.store %296, %279 : i64, !llvm.ptr
        cf.br ^bb51
      ^bb53:
      %298 = llvm.load %275 : !llvm.ptr -> f64
      %299 = arith.subf %125, %298 : f64
      %300 = arith.constant 0 : i32
      %301 = arith.extsi %300 : i32 to i64
      %302 = llvm.mlir.constant(1 : i64) : i64
      %303 = llvm.alloca %302 x i64 : (i64) -> !llvm.ptr
      llvm.store %301, %303 : i64, !llvm.ptr
      %304 = llvm.load %143 : !llvm.ptr -> i64
      %305 = llvm.mlir.constant(1 : i64) : i64
      %306 = llvm.alloca %305 x i64 : (i64) -> !llvm.ptr
      llvm.store %304, %306 : i64, !llvm.ptr
      cf.br ^bb57
      ^bb57:
      %307 = llvm.load %303 : !llvm.ptr -> i64
      %308 = llvm.load %306 : !llvm.ptr -> i64
      %309 = arith.cmpi slt, %307, %308 : i64
      cf.cond_br %309, ^bb58, ^bb59
      ^bb58:
        %310 = llvm.load %303 : !llvm.ptr -> i64
        %311 = llvm.load %306 : !llvm.ptr -> i64
        %312 = arith.addi %310, %311 : i64
        %313 = arith.constant 2 : i32
        %315 = arith.extsi %313 : i32 to i64
        %314 = arith.divsi %312, %315 : i64
        %317 = llvm.getelementptr %134[%314] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %316 = llvm.load %317 : !llvm.ptr -> f64
        %318 = arith.cmpf ole, %316, %299 : f64
        cf.cond_br %318, ^bb60, ^bb61
        ^bb60:
          %319 = arith.constant 1 : i32
          %321 = arith.extsi %319 : i32 to i64
          %320 = arith.addi %314, %321 : i64
          llvm.store %320, %303 : i64, !llvm.ptr
          cf.br ^bb62
        ^bb61:
          llvm.store %314, %306 : i64, !llvm.ptr
          cf.br ^bb62
        ^bb62:
        cf.br ^bb57
      ^bb59:
      %322 = llvm.load %303 : !llvm.ptr -> i64
      %323 = arith.constant 0 : i32
      %325 = arith.extsi %323 : i32 to i64
      %324 = arith.cmpi sgt, %322, %325 : i64
      cf.cond_br %324, ^bb63, ^bb64
      ^bb63:
        %326 = llvm.load %303 : !llvm.ptr -> i64
        %327 = arith.constant 1 : i32
        %329 = arith.extsi %327 : i32 to i64
        %328 = arith.subi %326, %329 : i64
        %330 = llvm.load %275 : !llvm.ptr -> f64
        %332 = llvm.getelementptr %134[%328] : (!llvm.ptr, i64) -> !llvm.ptr, f64
        %331 = llvm.load %332 : !llvm.ptr -> f64
        %333 = arith.addf %330, %331 : f64
        %334 = llvm.load %259 : !llvm.ptr -> f64
        %335 = arith.cmpf ogt, %333, %334 : f64
        cf.cond_br %335, ^bb66, ^bb67
        ^bb66:
          llvm.store %333, %259 : f64, !llvm.ptr
          %336 = llvm.load %147 : !llvm.ptr -> i64
          llvm.store %336, %263 : i64, !llvm.ptr
          %338 = llvm.getelementptr %137[%328] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %337 = llvm.load %338 : !llvm.ptr -> i64
          llvm.store %337, %267 : i64, !llvm.ptr
          cf.br ^bb68
        ^bb67:
          cf.br ^bb68
        ^bb68:
        cf.br ^bb65
      ^bb64:
        cf.br ^bb65
      ^bb65:
      %339 = llvm.load %147 : !llvm.ptr -> i64
      %340 = arith.constant 1 : i32
      %342 = arith.extsi %340 : i32 to i64
      %341 = arith.addi %339, %342 : i64
      llvm.store %341, %147 : i64, !llvm.ptr
      cf.br ^bb48
    ^bb50:
    %343 = arith.constant 9999995705032704 : i32
    %344 = arith.extsi %343 : i32 to i64
    %345 = arith.constant 1 : i32
    %346 = arith.extsi %345 : i32 to i64
    %347 = llvm.mlir.constant(1 : i64) : i64
    %348 = llvm.alloca %347 x i64 : (i64) -> !llvm.ptr
    llvm.store %346, %348 : i64, !llvm.ptr
    %349 = arith.constant 0 : i32
    %350 = arith.extsi %349 : i32 to i64
    llvm.store %350, %103 : i64, !llvm.ptr
    cf.br ^bb69
    ^bb69:
    %351 = llvm.load %103 : !llvm.ptr -> i64
    %352 = arith.cmpi slt, %351, %129 : i64
    cf.cond_br %352, ^bb70, ^bb71
    ^bb70:
      %353 = llvm.load %263 : !llvm.ptr -> i64
      %354 = arith.constant 1 : i32
      %356 = arith.extsi %354 : i32 to i64
      %355 = arith.andi %353, %356 : i64
      %357 = arith.constant 0 : i32
      %359 = arith.extsi %357 : i32 to i64
      %358 = arith.cmpi ne, %355, %359 : i64
      cf.cond_br %358, ^bb72, ^bb73
      ^bb72:
        %361 = llvm.load %348 : !llvm.ptr -> i64
        %363 = llvm.load %103 : !llvm.ptr -> i64
        %364 = llvm.getelementptr %40[%363] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %362 = llvm.load %364 : !llvm.ptr -> i64
        %360 = func.call @mulmod(%361, %362, %344) : (i64, i64, i64) -> i64
        llvm.store %360, %348 : i64, !llvm.ptr
        cf.br ^bb74
      ^bb73:
        cf.br ^bb74
      ^bb74:
      %365 = llvm.load %263 : !llvm.ptr -> i64
      %366 = arith.constant 2 : i32
      %368 = arith.extsi %366 : i32 to i64
      %367 = arith.divsi %365, %368 : i64
      llvm.store %367, %263 : i64, !llvm.ptr
      %369 = llvm.load %103 : !llvm.ptr -> i64
      %370 = arith.constant 1 : i32
      %372 = arith.extsi %370 : i32 to i64
      %371 = arith.addi %369, %372 : i64
      llvm.store %371, %103 : i64, !llvm.ptr
      cf.br ^bb69
    ^bb71:
    llvm.store %129, %103 : i64, !llvm.ptr
    cf.br ^bb75
    ^bb75:
    %373 = llvm.load %103 : !llvm.ptr -> i64
    %374 = llvm.load %48 : !llvm.ptr -> i64
    %375 = arith.cmpi slt, %373, %374 : i64
    cf.cond_br %375, ^bb76, ^bb77
    ^bb76:
      %376 = llvm.load %267 : !llvm.ptr -> i64
      %377 = arith.constant 1 : i32
      %379 = arith.extsi %377 : i32 to i64
      %378 = arith.andi %376, %379 : i64
      %380 = arith.constant 0 : i32
      %382 = arith.extsi %380 : i32 to i64
      %381 = arith.cmpi ne, %378, %382 : i64
      cf.cond_br %381, ^bb78, ^bb79
      ^bb78:
        %384 = llvm.load %348 : !llvm.ptr -> i64
        %386 = llvm.load %103 : !llvm.ptr -> i64
        %387 = llvm.getelementptr %40[%386] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %385 = llvm.load %387 : !llvm.ptr -> i64
        %383 = func.call @mulmod(%384, %385, %344) : (i64, i64, i64) -> i64
        llvm.store %383, %348 : i64, !llvm.ptr
        cf.br ^bb80
      ^bb79:
        cf.br ^bb80
      ^bb80:
      %388 = llvm.load %267 : !llvm.ptr -> i64
      %389 = arith.constant 2 : i32
      %391 = arith.extsi %389 : i32 to i64
      %390 = arith.divsi %388, %391 : i64
      llvm.store %390, %267 : i64, !llvm.ptr
      %392 = llvm.load %103 : !llvm.ptr -> i64
      %393 = arith.constant 1 : i32
      %395 = arith.extsi %393 : i32 to i64
      %394 = arith.addi %392, %395 : i64
      llvm.store %394, %103 : i64, !llvm.ptr
      cf.br ^bb75
    ^bb77:
    %396 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %397 = llvm.load %348 : !llvm.ptr -> i64
    %398 = llvm.call @printf(%396, %397) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%40) : (!llvm.ptr) -> ()
    func.call @free(%92) : (!llvm.ptr) -> ()
    func.call @free(%134) : (!llvm.ptr) -> ()
    func.call @free(%137) : (!llvm.ptr) -> ()
    %403 = arith.constant 0 : i32
    func.return %403 : i32
  }
}