Problem 743

Window into a Matrix — A(1e8,1e16) mod 1e9+7. Sum over t=0..k/2 of C(k,2t) * 2^(2t) * prod_{j=0}^{2t-1}(k-j) / (2t)! computed with batched modular inverses and prefix products.

Answer259158998
Output259158998
StatusPASS
Native helperno
Runtime1660 ms
Peak memory2704 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(1)O(1)
ApproachFlow solutionModular exponentiation
VerdictSuboptimal

Flow source

# Project Euler 743
# Window into a Matrix — A(1e8,1e16) mod 1e9+7.
#
# Sum over t=0..k/2 of C(k,2t) * 2^(2t) * prod_{j=0}^{2t-1}(k-j) / (2t)!
# computed with batched modular inverses and prefix products.

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

const MOD: i64 = 1000000007
const BLOCK: i64 = 200000

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

function inv_range(start: i64, end: i64, out: ptr<i64>) -> void {
    let l: i64 = end - start + 1
    let mut prod: i64 = 1
    let mut x: i64 = start
    let mut i: i64 = 0
    while i < l {
        prod = prod * x % MOD
        out[i] = prod
        x = x + 1
        i = i + 1
    }
    let inv_prod: i64 = modpow(prod, MOD - 2)
    let mut suffix: i64 = inv_prod
    x = end
    let mut i2: i64 = l - 1
    while i2 >= 0 {
        let prev: i64 = 0
        if i2 != 0 {
            prev = out[i2 - 1]
        }
        else {
            prev = 1
        }
        out[i2] = suffix * prev % MOD
        suffix = suffix * x % MOD
        x = x - 1
        i2 = i2 - 1
    }
}

function main() -> i32 {
    let k: i64 = 100000000
    let n: i64 = 10000000000000000
    let m: i64 = n / k
    let base: i64 = modpow(2, n)
    let pow2_2m: i64 = modpow(2, 2 * m)
    let r: i64 = modpow(pow2_2m, MOD - 2)
    let tmax: i64 = k / 2

    let mut term: i64 = 1
    let mut acc: i64 = 1
    let mut a: i64 = k

    let invs: ptr<i64> = malloc(BLOCK * 8)
    let mut start: i64 = 1

    while start <= tmax {
        let mut end: i64 = start + BLOCK - 1
        if end > tmax {
            end = tmax
        }
        inv_range(start, end, invs)
        let l: i64 = end - start + 1
        let mut i: i64 = 0
        while i < l {
            let inv: i64 = invs[i]
            let invsq_r: i64 = inv * inv % MOD * r % MOD
            term = term * a % MOD * (a - 1) % MOD * invsq_r % MOD
            acc = acc + term
            a = a - 2
            i = i + 1
        }
        start = end + 1
    }

    free(invs)
    printf("%lld\n", base * (acc % MOD) % MOD)
    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);
void inv_range_i64_i64_ptr_i64(int64_t start, int64_t end, int64_t* out);
int32_t main(void);

static const int64_t MOD = 1000000007;
static const int64_t BLOCK = 200000;




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

void inv_range_i64_i64_ptr_i64(int64_t start, int64_t end, int64_t* out) {
    int64_t l = ((end - start) + 1);
    int64_t prod = 1;
    int64_t x = start;
    int64_t i = 0;
    while (i < l) {
        prod = FLOW_CHECKED_MOD(((prod * x)), (MOD));
        out[i] = prod;
        x = (x + 1);
        i = (i + 1);
    }
    int64_t inv_prod = modpow_i64_i64(prod, (MOD - 2));
    int64_t suffix = inv_prod;
    x = end;
    int64_t i2 = (l - 1);
    while (i2 >= 0) {
        int64_t prev = 0;
        if (i2 != 0) {
            prev = out[(i2 - 1)];
        } else {
            prev = 1;
        }
        out[i2] = FLOW_CHECKED_MOD(((suffix * prev)), (MOD));
        suffix = FLOW_CHECKED_MOD(((suffix * x)), (MOD));
        x = (x - 1);
        i2 = (i2 - 1);
    }
}

int32_t main(void) {
    int64_t k = 100000000;
    int64_t n = 10000000000000000;
    int64_t m = FLOW_CHECKED_DIV((n), (k));
    int64_t base = modpow_i64_i64(2, n);
    int64_t pow2_2m = modpow_i64_i64(2, (2 * m));
    int64_t r = modpow_i64_i64(pow2_2m, (MOD - 2));
    int64_t tmax = FLOW_CHECKED_DIV((k), (2));
    int64_t term = 1;
    int64_t acc = 1;
    int64_t a = k;
    int64_t* invs = (int64_t*)(malloc((BLOCK * 8)));
    int64_t start = 1;
    while (start <= tmax) {
        int64_t end = ((start + BLOCK) - 1);
        if (end > tmax) {
            end = tmax;
        }
        inv_range_i64_i64_ptr_i64(start, end, invs);
        int64_t l = ((end - start) + 1);
        int64_t i = 0;
        while (i < l) {
            int64_t inv = invs[i];
            int64_t invsq_r = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((inv * inv)), (MOD)) * r)), (MOD));
            term = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((term * a)), (MOD)) * (a - 1))), (MOD)) * invsq_r)), (MOD));
            acc = (acc + term);
            a = (a - 2);
            i = (i + 1);
        }
        start = (end + 1);
    }
    free(invs);
    printf("%lld\n", FLOW_CHECKED_MOD(((base * FLOW_CHECKED_MOD((acc), (MOD)))), (MOD)));
    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 @malloc(i64) -> !llvm.ptr
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
  // Constant: BLOCK
  llvm.mlir.global internal constant @BLOCK(200000 : i64) : i64
  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 @inv_range(%arg0: i64, %arg1: i64, %arg2: !llvm.ptr) -> () {
    %39 = arith.subi %arg1, %arg0 : i64
    %40 = arith.constant 1 : i32
    %42 = arith.extsi %40 : i32 to i64
    %41 = arith.addi %39, %42 : i64
    %43 = arith.constant 1 : i32
    %44 = arith.extsi %43 : i32 to i64
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x i64 : (i64) -> !llvm.ptr
    llvm.store %44, %46 : i64, !llvm.ptr
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %48 : i64, !llvm.ptr
    %49 = arith.constant 0 : 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 ^bb6
    ^bb6:
    %53 = llvm.load %52 : !llvm.ptr -> i64
    %54 = arith.cmpi slt, %53, %41 : i64
    cf.cond_br %54, ^bb7, ^bb8
    ^bb7:
      %55 = llvm.load %46 : !llvm.ptr -> i64
      %56 = llvm.load %48 : !llvm.ptr -> i64
      %57 = arith.muli %55, %56 : i64
      %58 = llvm.mlir.addressof @MOD : !llvm.ptr
      %59 = llvm.load %58 : !llvm.ptr -> i64
      %60 = arith.remsi %57, %59 : i64
      llvm.store %60, %46 : i64, !llvm.ptr
      %61 = llvm.load %46 : !llvm.ptr -> i64
      %62 = llvm.load %52 : !llvm.ptr -> i64
      %63 = llvm.getelementptr %arg2[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %61, %63 : i64, !llvm.ptr
      %64 = llvm.load %48 : !llvm.ptr -> i64
      %65 = arith.constant 1 : i32
      %67 = arith.extsi %65 : i32 to i64
      %66 = arith.addi %64, %67 : i64
      llvm.store %66, %48 : i64, !llvm.ptr
      %68 = llvm.load %52 : !llvm.ptr -> i64
      %69 = arith.constant 1 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.addi %68, %71 : i64
      llvm.store %70, %52 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %73 = llvm.load %46 : !llvm.ptr -> i64
    %74 = llvm.mlir.addressof @MOD : !llvm.ptr
    %75 = llvm.load %74 : !llvm.ptr -> i64
    %76 = arith.constant 2 : i32
    %78 = arith.extsi %76 : i32 to i64
    %77 = arith.subi %75, %78 : i64
    %72 = func.call @modpow(%73, %77) : (i64, i64) -> i64
    %79 = llvm.mlir.constant(1 : i64) : i64
    %80 = llvm.alloca %79 x i64 : (i64) -> !llvm.ptr
    llvm.store %72, %80 : i64, !llvm.ptr
    llvm.store %arg1, %48 : i64, !llvm.ptr
    %81 = arith.constant 1 : i32
    %83 = arith.extsi %81 : i32 to i64
    %82 = arith.subi %41, %83 : i64
    %84 = llvm.mlir.constant(1 : i64) : i64
    %85 = llvm.alloca %84 x i64 : (i64) -> !llvm.ptr
    llvm.store %82, %85 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %86 = llvm.load %85 : !llvm.ptr -> i64
    %87 = arith.constant 0 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.cmpi sge, %86, %89 : i64
    cf.cond_br %88, ^bb10, ^bb11
    ^bb10:
      %90 = arith.constant 0 : i32
      %91 = arith.extsi %90 : i32 to i64
      %92 = llvm.load %85 : !llvm.ptr -> i64
      %93 = arith.constant 0 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.cmpi ne, %92, %95 : i64
      %96 = scf.if %94 -> (i64) {
        %98 = llvm.load %85 : !llvm.ptr -> i64
        %99 = arith.constant 1 : i32
        %101 = arith.extsi %99 : i32 to i64
        %100 = arith.subi %98, %101 : i64
        %102 = llvm.getelementptr %arg2[%100] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %97 = llvm.load %102 : !llvm.ptr -> i64
        scf.yield %97 : i64
      } else {
        %103 = arith.constant 1 : i32
        %104 = arith.extsi %103 : i32 to i64
        scf.yield %104 : i64
      }
      %105 = llvm.load %80 : !llvm.ptr -> i64
      %106 = arith.muli %105, %96 : i64
      %107 = llvm.mlir.addressof @MOD : !llvm.ptr
      %108 = llvm.load %107 : !llvm.ptr -> i64
      %109 = arith.remsi %106, %108 : i64
      %110 = llvm.load %85 : !llvm.ptr -> i64
      %111 = llvm.getelementptr %arg2[%110] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %109, %111 : i64, !llvm.ptr
      %112 = llvm.load %80 : !llvm.ptr -> i64
      %113 = llvm.load %48 : !llvm.ptr -> i64
      %114 = arith.muli %112, %113 : i64
      %115 = llvm.mlir.addressof @MOD : !llvm.ptr
      %116 = llvm.load %115 : !llvm.ptr -> i64
      %117 = arith.remsi %114, %116 : i64
      llvm.store %117, %80 : i64, !llvm.ptr
      %118 = llvm.load %48 : !llvm.ptr -> i64
      %119 = arith.constant 1 : i32
      %121 = arith.extsi %119 : i32 to i64
      %120 = arith.subi %118, %121 : i64
      llvm.store %120, %48 : i64, !llvm.ptr
      %122 = llvm.load %85 : !llvm.ptr -> i64
      %123 = arith.constant 1 : i32
      %125 = arith.extsi %123 : i32 to i64
      %124 = arith.subi %122, %125 : i64
      llvm.store %124, %85 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    func.return
  }
  func.func @main() -> i32 {
    %126 = arith.constant 100000000 : i32
    %127 = arith.extsi %126 : i32 to i64
    %128 = arith.constant 9999995705032704 : i32
    %129 = arith.extsi %128 : i32 to i64
    %130 = arith.divsi %129, %127 : i64
    %132 = arith.constant 2 : i32
    %133 = arith.extsi %132 : i32 to i64
    %131 = func.call @modpow(%133, %129) : (i64, i64) -> i64
    %135 = arith.constant 2 : i32
    %136 = arith.constant 2 : i32
    %138 = arith.extsi %136 : i32 to i64
    %137 = arith.muli %138, %130 : i64
    %139 = arith.extsi %135 : i32 to i64
    %134 = func.call @modpow(%139, %137) : (i64, i64) -> i64
    %141 = llvm.mlir.addressof @MOD : !llvm.ptr
    %142 = llvm.load %141 : !llvm.ptr -> i64
    %143 = arith.constant 2 : i32
    %145 = arith.extsi %143 : i32 to i64
    %144 = arith.subi %142, %145 : i64
    %140 = func.call @modpow(%134, %144) : (i64, i64) -> i64
    %146 = arith.constant 2 : i32
    %148 = arith.extsi %146 : i32 to i64
    %147 = arith.divsi %127, %148 : i64
    %149 = arith.constant 1 : i32
    %150 = arith.extsi %149 : i32 to i64
    %151 = llvm.mlir.constant(1 : i64) : i64
    %152 = llvm.alloca %151 x i64 : (i64) -> !llvm.ptr
    llvm.store %150, %152 : i64, !llvm.ptr
    %153 = arith.constant 1 : i32
    %154 = arith.extsi %153 : i32 to i64
    %155 = llvm.mlir.constant(1 : i64) : i64
    %156 = llvm.alloca %155 x i64 : (i64) -> !llvm.ptr
    llvm.store %154, %156 : i64, !llvm.ptr
    %157 = llvm.mlir.constant(1 : i64) : i64
    %158 = llvm.alloca %157 x i64 : (i64) -> !llvm.ptr
    llvm.store %127, %158 : i64, !llvm.ptr
    %160 = llvm.mlir.addressof @BLOCK : !llvm.ptr
    %161 = llvm.load %160 : !llvm.ptr -> i64
    %162 = arith.constant 8 : i32
    %164 = arith.extsi %162 : i32 to i64
    %163 = arith.muli %161, %164 : i64
    %159 = func.call @malloc(%163) : (i64) -> !llvm.ptr
    %165 = arith.constant 1 : i32
    %166 = arith.extsi %165 : i32 to i64
    %167 = llvm.mlir.constant(1 : i64) : i64
    %168 = llvm.alloca %167 x i64 : (i64) -> !llvm.ptr
    llvm.store %166, %168 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %169 = llvm.load %168 : !llvm.ptr -> i64
    %170 = arith.cmpi sle, %169, %147 : i64
    cf.cond_br %170, ^bb13, ^bb14
    ^bb13:
      %171 = llvm.load %168 : !llvm.ptr -> i64
      %172 = llvm.mlir.addressof @BLOCK : !llvm.ptr
      %173 = llvm.load %172 : !llvm.ptr -> i64
      %174 = arith.addi %171, %173 : i64
      %175 = arith.constant 1 : i32
      %177 = arith.extsi %175 : i32 to i64
      %176 = arith.subi %174, %177 : i64
      %178 = llvm.mlir.constant(1 : i64) : i64
      %179 = llvm.alloca %178 x i64 : (i64) -> !llvm.ptr
      llvm.store %176, %179 : i64, !llvm.ptr
      %180 = llvm.load %179 : !llvm.ptr -> i64
      %181 = arith.cmpi sgt, %180, %147 : i64
      cf.cond_br %181, ^bb15, ^bb16
      ^bb15:
        llvm.store %147, %179 : i64, !llvm.ptr
        cf.br ^bb17
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %183 = llvm.load %168 : !llvm.ptr -> i64
      %184 = llvm.load %179 : !llvm.ptr -> i64
      func.call @inv_range(%183, %184, %159) : (i64, i64, !llvm.ptr) -> ()
      %185 = llvm.load %179 : !llvm.ptr -> i64
      %186 = llvm.load %168 : !llvm.ptr -> i64
      %187 = arith.subi %185, %186 : i64
      %188 = arith.constant 1 : i32
      %190 = arith.extsi %188 : i32 to i64
      %189 = arith.addi %187, %190 : i64
      %191 = arith.constant 0 : i32
      %192 = arith.extsi %191 : i32 to i64
      %193 = llvm.mlir.constant(1 : i64) : i64
      %194 = llvm.alloca %193 x i64 : (i64) -> !llvm.ptr
      llvm.store %192, %194 : i64, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %195 = llvm.load %194 : !llvm.ptr -> i64
      %196 = arith.cmpi slt, %195, %189 : i64
      cf.cond_br %196, ^bb19, ^bb20
      ^bb19:
        %198 = llvm.load %194 : !llvm.ptr -> i64
        %199 = llvm.getelementptr %159[%198] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %197 = llvm.load %199 : !llvm.ptr -> i64
        %200 = arith.muli %197, %197 : i64
        %201 = llvm.mlir.addressof @MOD : !llvm.ptr
        %202 = llvm.load %201 : !llvm.ptr -> i64
        %203 = arith.remsi %200, %202 : i64
        %204 = arith.muli %203, %140 : i64
        %205 = llvm.mlir.addressof @MOD : !llvm.ptr
        %206 = llvm.load %205 : !llvm.ptr -> i64
        %207 = arith.remsi %204, %206 : i64
        %208 = llvm.load %152 : !llvm.ptr -> i64
        %209 = llvm.load %158 : !llvm.ptr -> i64
        %210 = arith.muli %208, %209 : i64
        %211 = llvm.mlir.addressof @MOD : !llvm.ptr
        %212 = llvm.load %211 : !llvm.ptr -> i64
        %213 = arith.remsi %210, %212 : i64
        %214 = llvm.load %158 : !llvm.ptr -> i64
        %215 = arith.constant 1 : i32
        %217 = arith.extsi %215 : i32 to i64
        %216 = arith.subi %214, %217 : i64
        %218 = arith.muli %213, %216 : i64
        %219 = llvm.mlir.addressof @MOD : !llvm.ptr
        %220 = llvm.load %219 : !llvm.ptr -> i64
        %221 = arith.remsi %218, %220 : i64
        %222 = arith.muli %221, %207 : i64
        %223 = llvm.mlir.addressof @MOD : !llvm.ptr
        %224 = llvm.load %223 : !llvm.ptr -> i64
        %225 = arith.remsi %222, %224 : i64
        llvm.store %225, %152 : i64, !llvm.ptr
        %226 = llvm.load %156 : !llvm.ptr -> i64
        %227 = llvm.load %152 : !llvm.ptr -> i64
        %228 = arith.addi %226, %227 : i64
        llvm.store %228, %156 : i64, !llvm.ptr
        %229 = llvm.load %158 : !llvm.ptr -> i64
        %230 = arith.constant 2 : i32
        %232 = arith.extsi %230 : i32 to i64
        %231 = arith.subi %229, %232 : i64
        llvm.store %231, %158 : i64, !llvm.ptr
        %233 = llvm.load %194 : !llvm.ptr -> i64
        %234 = arith.constant 1 : i32
        %236 = arith.extsi %234 : i32 to i64
        %235 = arith.addi %233, %236 : i64
        llvm.store %235, %194 : i64, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %237 = llvm.load %179 : !llvm.ptr -> i64
      %238 = arith.constant 1 : i32
      %240 = arith.extsi %238 : i32 to i64
      %239 = arith.addi %237, %240 : i64
      llvm.store %239, %168 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    func.call @free(%159) : (!llvm.ptr) -> ()
    %242 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %243 = llvm.load %156 : !llvm.ptr -> i64
    %244 = llvm.mlir.addressof @MOD : !llvm.ptr
    %245 = llvm.load %244 : !llvm.ptr -> i64
    %246 = arith.remsi %243, %245 : i64
    %247 = arith.muli %131, %246 : i64
    %248 = llvm.mlir.addressof @MOD : !llvm.ptr
    %249 = llvm.load %248 : !llvm.ptr -> i64
    %250 = arith.remsi %247, %249 : i64
    %251 = llvm.call @printf(%242, %250) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %252 = arith.constant 0 : i32
    func.return %252 : i32
  }
}