Problem 648

F(1000) mod 10^9 using polynomial recurrence.

Answer301483197
Output301483197
StatusPASS
Native helperno
Runtime850 ms
Peak memory1424 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 648: Skipping Squares
# F(1000) mod 10^9 using polynomial recurrence.

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

const MOD: i64 = 1000000000
const N: i64 = 1000

# Multiply two polynomials (little-endian), truncate to out_len coefficients, mod MOD
function mul_trunc(a: ptr<i64>, b: ptr<i64>, out_len: i64) -> ptr<i64> {
    let out: ptr<i64> = calloc(out_len, 8)
    for i in 0..out_len {
        let mut s: i64 = 0
        for j in 0..(i + 1) {
            if j < out_len && (i - j) < out_len {
                s = (s + a[j] * b[i - j]) % MOD
            }
        }
        out[i] = s
    }
    return out
}

function main() -> i32 {
    # a[k] = coefficients of f(rho) = sum S_m
    let a: ptr<i64> = calloc(N + 1, 8)

    # S_1 = 1 - rho
    let S: ptr<i64> = calloc(N + 1, 8)
    S[0] = 1
    S[1] = MOD - 1
    let mut S_offset: i64 = 0

    # Add S_1 into f
    for i in 0..(N + 1) {
        a[i] = (a[i] + S[i]) % MOD
    }

    # v_prev2 = v_0 = [0, 0, ...]
    let v_prev2: ptr<i64> = calloc(N + 1, 8)
    # v_prev1 = v_1 = S = 1 - rho
    let v_prev1: ptr<i64> = calloc(N + 1, 8)
    for i in 0..(N + 1) { v_prev1[i] = S[i] }

    let vk: ptr<i64> = calloc(N + 1, 8)

    for k in 2..(2 * N + 1) {
        # Compute v_k = v_{k-2} + rho*(v_{k-1} - v_{k-2})
        # v[0] = v_prev2[0]
        # v[i+1] = v_prev2[i+1] + v_prev1[i] - v_prev2[i]
        vk[0] = v_prev2[0]
        for i in 0..N {
            let mut val: i64 = v_prev2[i + 1] + v_prev1[i] - v_prev2[i]
            val = val % MOD
            if val < 0 { val = val + MOD }
            vk[i + 1] = val
        }

        # Shift: v_prev2 = v_prev1, v_prev1 = vk
        for i in 0..(N + 1) { v_prev2[i] = v_prev1[i] }
        for i in 0..(N + 1) { v_prev1[i] = vk[i] }

        if k % 2 != 0 { continue }

        # n = k/2, b_n = v_k
        # S_new = S * b_n (truncated)
        # b_n has no constant term, so factor = b[1..maxdeg]
        let maxdeg_factor: i64 = N - S_offset
        let out_len: i64 = N - S_offset  # degrees S_offset+1 .. N

        if out_len <= 0 { continue }

        # left = S[S_offset .. S_offset + out_len]
        # right = b[1 .. 1 + out_len]  (but need to be careful with lengths)
        let left: ptr<i64> = calloc(out_len, 8)
        let right: ptr<i64> = calloc(out_len, 8)
        for i in 0..out_len {
            left[i] = S[S_offset + i]
        }
        for i in 0..out_len {
            if i + 1 <= N {
                right[i] = vk[i + 1]
            }
        }

        let prod: ptr<i64> = mul_trunc(left, right, out_len)

        S_offset = S_offset + 1

        # Rebuild S
        let new_S: ptr<i64> = calloc(N + 1, 8)
        for i in 0..out_len {
            new_S[S_offset + i] = prod[i]
        }
        for i in 0..(N + 1) { S[i] = new_S[i] }

        # Add S_{n+1} into f
        for i in 0..out_len {
            a[S_offset + i] = (a[S_offset + i] + prod[i]) % MOD
        }

        free(prod)
        free(right)
        free(left)
        free(new_S)
    }

    # F(N) = sum a[0..N]
    let mut ans: i64 = 0
    for i in 0..(N + 1) {
        ans = (ans + a[i]) % MOD
    }

    printf("%lld\n", ans)

    free(vk)
    free(v_prev1)
    free(v_prev2)
    free(S)
    free(a)
    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* mul_trunc_ptr_i64_ptr_i64_i64(int64_t* a, int64_t* b, int64_t out_len);
int32_t main(void);

static const int64_t MOD = 1000000000;
static const int64_t N = 1000;



int64_t* mul_trunc_ptr_i64_ptr_i64_i64(int64_t* a, int64_t* b, int64_t out_len) {
    int64_t* out = (int64_t*)(calloc(out_len, 8));
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
        int64_t s = 0;
        int32_t __flow_step_2 = 1;
        for (int32_t j = 0; (0 <= (i + 1)) ? j < (i + 1) : j > (i + 1); j += (0 <= (i + 1)) ? 1 : -1) {
            if ((j < out_len && (i - j) < out_len)) {
                s = FLOW_CHECKED_MOD(((s + (a[j] * b[(i - j)]))), (MOD));
            }
        }
        out[i] = s;
    }
    return out;
}

int32_t main(void) {
    int64_t* a = (int64_t*)(calloc((N + 1), 8));
    int64_t* S = (int64_t*)(calloc((N + 1), 8));
    S[0] = 1;
    S[1] = (MOD - 1);
    int64_t S_offset = 0;
    int32_t __flow_step_3 = 1;
    for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
        a[i] = FLOW_CHECKED_MOD(((a[i] + S[i])), (MOD));
    }
    int64_t* v_prev2 = (int64_t*)(calloc((N + 1), 8));
    int64_t* v_prev1 = (int64_t*)(calloc((N + 1), 8));
    int32_t __flow_step_4 = 1;
    for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
        v_prev1[i] = S[i];
    }
    int64_t* vk = (int64_t*)(calloc((N + 1), 8));
    int32_t __flow_step_5 = 1;
    for (int32_t k = 2; (2 <= ((2 * N) + 1)) ? k < ((2 * N) + 1) : k > ((2 * N) + 1); k += (2 <= ((2 * N) + 1)) ? 1 : -1) {
        vk[0] = v_prev2[0];
        int32_t __flow_step_6 = 1;
        for (int32_t i = 0; (0 <= N) ? i < N : i > N; i += (0 <= N) ? 1 : -1) {
            int64_t val = ((v_prev2[(i + 1)] + v_prev1[i]) - v_prev2[i]);
            val = FLOW_CHECKED_MOD((val), (MOD));
            if (val < 0) {
                val = (val + MOD);
            }
            vk[(i + 1)] = val;
        }
        int32_t __flow_step_7 = 1;
        for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
            v_prev2[i] = v_prev1[i];
        }
        int32_t __flow_step_8 = 1;
        for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
            v_prev1[i] = vk[i];
        }
        if (FLOW_CHECKED_MOD((k), (2)) != 0) {
            continue;
        }
        int64_t maxdeg_factor = (N - S_offset);
        int64_t out_len = (N - S_offset);
        if (out_len <= 0) {
            continue;
        }
        int64_t* left = (int64_t*)(calloc(out_len, 8));
        int64_t* right = (int64_t*)(calloc(out_len, 8));
        int32_t __flow_step_9 = 1;
        for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
            left[i] = S[(S_offset + i)];
        }
        int32_t __flow_step_10 = 1;
        for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
            if ((i + 1) <= N) {
                right[i] = vk[(i + 1)];
            }
        }
        int64_t* prod = (int64_t*)(mul_trunc_ptr_i64_ptr_i64_i64(left, right, out_len));
        S_offset = (S_offset + 1);
        int64_t* new_S = (int64_t*)(calloc((N + 1), 8));
        int32_t __flow_step_11 = 1;
        for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
            new_S[(S_offset + i)] = prod[i];
        }
        int32_t __flow_step_12 = 1;
        for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
            S[i] = new_S[i];
        }
        int32_t __flow_step_13 = 1;
        for (int32_t i = 0; (0 <= out_len) ? i < out_len : i > out_len; i += (0 <= out_len) ? 1 : -1) {
            a[(S_offset + i)] = FLOW_CHECKED_MOD(((a[(S_offset + i)] + prod[i])), (MOD));
        }
        free(prod);
        free(right);
        free(left);
        free(new_S);
    }
    int64_t ans = 0;
    int32_t __flow_step_14 = 1;
    for (int32_t i = 0; (0 <= (N + 1)) ? i < (N + 1) : i > (N + 1); i += (0 <= (N + 1)) ? 1 : -1) {
        ans = FLOW_CHECKED_MOD(((ans + a[i])), (MOD));
    }
    printf("%lld\n", ans);
    free(vk);
    free(v_prev1);
    free(v_prev2);
    free(S);
    free(a);
    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) -> ()
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000000 : i64) : i64
  // Constant: N
  llvm.mlir.global internal constant @N(1000 : i64) : i64
  func.func @mul_trunc(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64) -> !llvm.ptr {
    %1 = arith.constant 8 : i32
    %2 = arith.extsi %1 : i32 to i64
    %0 = func.call @calloc(%arg2, %2) : (i64, i64) -> !llvm.ptr
    %3 = arith.constant 0 : i32
    %4 = arith.index_cast %3 : i32 to index
    %5 = arith.index_cast %arg2 : i32 to index
    %7 = arith.constant 1 : index
    %8 = arith.constant -1 : index
    %9 = arith.cmpi sle, %4, %5 : index
    %6 = arith.select %9, %7, %8 : index
    cf.br ^bb0(%4 : index)
    ^bb0(%10: index):
    %11 = arith.cmpi slt, %10, %5 : index
    %12 = arith.cmpi sgt, %10, %5 : index
    %13 = arith.select %9, %11, %12 : i1
    cf.cond_br %13, ^bb1(%10 : index), ^bb2(%10 : index)
    ^bb1(%14: index):
      %15 = arith.constant 0 : i32
      %16 = arith.extsi %15 : i32 to i64
      %17 = llvm.mlir.constant(1 : i64) : i64
      %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
      llvm.store %16, %18 : i64, !llvm.ptr
      %19 = arith.constant 0 : i32
      %20 = arith.constant 1 : i32
      %22 = arith.index_cast %14 : index to i32
      %21 = arith.addi %22, %20 : i32
      %23 = arith.index_cast %19 : i32 to index
      %24 = arith.index_cast %21 : i32 to index
      %26 = arith.constant 1 : index
      %27 = arith.constant -1 : index
      %28 = arith.cmpi sle, %23, %24 : index
      %25 = arith.select %28, %26, %27 : index
      cf.br ^bb3(%23 : index)
      ^bb3(%29: index):
      %30 = arith.cmpi slt, %29, %24 : index
      %31 = arith.cmpi sgt, %29, %24 : index
      %32 = arith.select %28, %30, %31 : i1
      cf.cond_br %32, ^bb4(%29 : index), ^bb5(%29 : index)
      ^bb4(%33: index):
        %35 = arith.index_cast %33 : index to i32
        %36 = arith.trunci %arg2 : i64 to i32
        %34 = arith.cmpi slt, %35, %36 : i32
        %37 = scf.if %34 -> (i1) {
          %38 = arith.subi %14, %33 : index
          %40 = arith.index_cast %38 : index to i32
          %41 = arith.trunci %arg2 : i64 to i32
          %39 = arith.cmpi slt, %40, %41 : i32
          scf.yield %39 : i1
        } else {
          %42 = arith.constant false
          scf.yield %42 : i1
        }
        cf.cond_br %37, ^bb6, ^bb7
        ^bb6:
          %43 = llvm.load %18 : !llvm.ptr -> i64
          %45 = arith.index_cast %33 : index to i64
          %46 = llvm.getelementptr %arg0[%45] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %44 = llvm.load %46 : !llvm.ptr -> i64
          %48 = arith.subi %14, %33 : index
          %49 = arith.index_cast %48 : index to i64
          %50 = llvm.getelementptr %arg1[%49] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %47 = llvm.load %50 : !llvm.ptr -> i64
          %51 = arith.muli %44, %47 : i64
          %52 = arith.addi %43, %51 : i64
          %53 = llvm.mlir.addressof @MOD : !llvm.ptr
          %54 = llvm.load %53 : !llvm.ptr -> i64
          %55 = arith.remsi %52, %54 : i64
          llvm.store %55, %18 : i64, !llvm.ptr
          cf.br ^bb8
        ^bb7:
          cf.br ^bb8
        ^bb8:
        %56 = arith.addi %33, %25 : index
        cf.br ^bb3(%56 : index)
      ^bb5(%57: index):
      %58 = llvm.load %18 : !llvm.ptr -> i64
      %59 = arith.index_cast %14 : index to i64
      %60 = llvm.getelementptr %0[%59] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %58, %60 : i64, !llvm.ptr
      %61 = arith.addi %14, %6 : index
      cf.br ^bb0(%61 : index)
    ^bb2(%62: index):
    func.return %0 : !llvm.ptr
  }
  func.func @main() -> i32 {
    %64 = llvm.mlir.addressof @N : !llvm.ptr
    %65 = llvm.load %64 : !llvm.ptr -> i64
    %66 = arith.constant 1 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.addi %65, %68 : i64
    %69 = arith.constant 8 : i32
    %70 = arith.extsi %69 : i32 to i64
    %63 = func.call @calloc(%67, %70) : (i64, i64) -> !llvm.ptr
    %72 = llvm.mlir.addressof @N : !llvm.ptr
    %73 = llvm.load %72 : !llvm.ptr -> i64
    %74 = arith.constant 1 : i32
    %76 = arith.extsi %74 : i32 to i64
    %75 = arith.addi %73, %76 : i64
    %77 = arith.constant 8 : i32
    %78 = arith.extsi %77 : i32 to i64
    %71 = func.call @calloc(%75, %78) : (i64, i64) -> !llvm.ptr
    %79 = arith.constant 1 : i32
    %80 = arith.constant 0 : i32
    %81 = arith.extsi %79 : i32 to i64
    %82 = arith.extsi %80 : i32 to i64
    %83 = llvm.getelementptr %71[%82] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %81, %83 : i64, !llvm.ptr
    %84 = llvm.mlir.addressof @MOD : !llvm.ptr
    %85 = llvm.load %84 : !llvm.ptr -> i64
    %86 = arith.constant 1 : i32
    %88 = arith.extsi %86 : i32 to i64
    %87 = arith.subi %85, %88 : i64
    %89 = arith.constant 1 : i32
    %90 = arith.extsi %89 : i32 to i64
    %91 = llvm.getelementptr %71[%90] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %87, %91 : i64, !llvm.ptr
    %92 = arith.constant 0 : i32
    %93 = arith.extsi %92 : i32 to i64
    %94 = llvm.mlir.constant(1 : i64) : i64
    %95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
    llvm.store %93, %95 : i64, !llvm.ptr
    %96 = arith.constant 0 : i32
    %97 = llvm.mlir.addressof @N : !llvm.ptr
    %98 = llvm.load %97 : !llvm.ptr -> i64
    %99 = arith.constant 1 : i32
    %101 = arith.extsi %99 : i32 to i64
    %100 = arith.addi %98, %101 : i64
    %102 = arith.index_cast %96 : i32 to index
    %103 = arith.index_cast %100 : i32 to index
    %105 = arith.constant 1 : index
    %106 = arith.constant -1 : index
    %107 = arith.cmpi sle, %102, %103 : index
    %104 = arith.select %107, %105, %106 : index
    cf.br ^bb9(%102 : index)
    ^bb9(%108: index):
    %109 = arith.cmpi slt, %108, %103 : index
    %110 = arith.cmpi sgt, %108, %103 : index
    %111 = arith.select %107, %109, %110 : i1
    cf.cond_br %111, ^bb10(%108 : index), ^bb11(%108 : index)
    ^bb10(%112: index):
      %114 = arith.index_cast %112 : index to i64
      %115 = llvm.getelementptr %63[%114] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %113 = llvm.load %115 : !llvm.ptr -> i64
      %117 = arith.index_cast %112 : index to i64
      %118 = llvm.getelementptr %71[%117] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %116 = llvm.load %118 : !llvm.ptr -> i64
      %119 = arith.addi %113, %116 : i64
      %120 = llvm.mlir.addressof @MOD : !llvm.ptr
      %121 = llvm.load %120 : !llvm.ptr -> i64
      %122 = arith.remsi %119, %121 : i64
      %123 = arith.index_cast %112 : index to i64
      %124 = llvm.getelementptr %63[%123] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %122, %124 : i64, !llvm.ptr
      %125 = arith.addi %112, %104 : index
      cf.br ^bb9(%125 : index)
    ^bb11(%126: index):
    %128 = llvm.mlir.addressof @N : !llvm.ptr
    %129 = llvm.load %128 : !llvm.ptr -> i64
    %130 = arith.constant 1 : i32
    %132 = arith.extsi %130 : i32 to i64
    %131 = arith.addi %129, %132 : i64
    %133 = arith.constant 8 : i32
    %134 = arith.extsi %133 : i32 to i64
    %127 = func.call @calloc(%131, %134) : (i64, i64) -> !llvm.ptr
    %136 = llvm.mlir.addressof @N : !llvm.ptr
    %137 = llvm.load %136 : !llvm.ptr -> i64
    %138 = arith.constant 1 : i32
    %140 = arith.extsi %138 : i32 to i64
    %139 = arith.addi %137, %140 : i64
    %141 = arith.constant 8 : i32
    %142 = arith.extsi %141 : i32 to i64
    %135 = func.call @calloc(%139, %142) : (i64, i64) -> !llvm.ptr
    %143 = arith.constant 0 : i32
    %144 = llvm.mlir.addressof @N : !llvm.ptr
    %145 = llvm.load %144 : !llvm.ptr -> i64
    %146 = arith.constant 1 : i32
    %148 = arith.extsi %146 : i32 to i64
    %147 = arith.addi %145, %148 : i64
    %149 = arith.index_cast %143 : i32 to index
    %150 = arith.index_cast %147 : i32 to index
    %152 = arith.constant 1 : index
    %153 = arith.constant -1 : index
    %154 = arith.cmpi sle, %149, %150 : index
    %151 = arith.select %154, %152, %153 : index
    cf.br ^bb12(%149 : index)
    ^bb12(%155: index):
    %156 = arith.cmpi slt, %155, %150 : index
    %157 = arith.cmpi sgt, %155, %150 : index
    %158 = arith.select %154, %156, %157 : i1
    cf.cond_br %158, ^bb13(%155 : index), ^bb14(%155 : index)
    ^bb13(%159: index):
      %161 = arith.index_cast %159 : index to i64
      %162 = llvm.getelementptr %71[%161] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %160 = llvm.load %162 : !llvm.ptr -> i64
      %163 = arith.index_cast %159 : index to i64
      %164 = llvm.getelementptr %135[%163] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %160, %164 : i64, !llvm.ptr
      %165 = arith.addi %159, %151 : index
      cf.br ^bb12(%165 : index)
    ^bb14(%166: index):
    %168 = llvm.mlir.addressof @N : !llvm.ptr
    %169 = llvm.load %168 : !llvm.ptr -> i64
    %170 = arith.constant 1 : i32
    %172 = arith.extsi %170 : i32 to i64
    %171 = arith.addi %169, %172 : i64
    %173 = arith.constant 8 : i32
    %174 = arith.extsi %173 : i32 to i64
    %167 = func.call @calloc(%171, %174) : (i64, i64) -> !llvm.ptr
    %175 = arith.constant 2 : i32
    %176 = arith.constant 2 : i32
    %177 = llvm.mlir.addressof @N : !llvm.ptr
    %178 = llvm.load %177 : !llvm.ptr -> i64
    %180 = arith.extsi %176 : i32 to i64
    %179 = arith.muli %180, %178 : i64
    %181 = arith.constant 1 : i32
    %183 = arith.extsi %181 : i32 to i64
    %182 = arith.addi %179, %183 : i64
    %184 = arith.index_cast %175 : i32 to index
    %185 = arith.index_cast %182 : i32 to index
    %187 = arith.constant 1 : index
    %188 = arith.constant -1 : index
    %189 = arith.cmpi sle, %184, %185 : index
    %186 = arith.select %189, %187, %188 : index
    cf.br ^bb15(%184 : index)
    ^bb15(%190: index):
    %191 = arith.cmpi slt, %190, %185 : index
    %192 = arith.cmpi sgt, %190, %185 : index
    %193 = arith.select %189, %191, %192 : i1
    cf.cond_br %193, ^bb16(%190 : index), ^bb17(%190 : index)
    ^bb16(%194: index):
      %196 = arith.constant 0 : i32
      %197 = arith.extsi %196 : i32 to i64
      %198 = llvm.getelementptr %127[%197] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %195 = llvm.load %198 : !llvm.ptr -> i64
      %199 = arith.constant 0 : i32
      %200 = arith.extsi %199 : i32 to i64
      %201 = llvm.getelementptr %167[%200] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %195, %201 : i64, !llvm.ptr
      %202 = arith.constant 0 : i32
      %203 = llvm.mlir.addressof @N : !llvm.ptr
      %204 = llvm.load %203 : !llvm.ptr -> i64
      %205 = arith.index_cast %202 : i32 to index
      %206 = arith.index_cast %204 : i32 to index
      %208 = arith.constant 1 : index
      %209 = arith.constant -1 : index
      %210 = arith.cmpi sle, %205, %206 : index
      %207 = arith.select %210, %208, %209 : index
      cf.br ^bb18(%205 : index)
      ^bb18(%211: index):
      %212 = arith.cmpi slt, %211, %206 : index
      %213 = arith.cmpi sgt, %211, %206 : index
      %214 = arith.select %210, %212, %213 : i1
      cf.cond_br %214, ^bb19(%211 : index), ^bb20(%211 : index)
      ^bb19(%215: index):
        %217 = arith.constant 1 : i32
        %219 = arith.index_cast %215 : index to i32
        %218 = arith.addi %219, %217 : i32
        %220 = arith.extsi %218 : i32 to i64
        %221 = llvm.getelementptr %127[%220] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %216 = llvm.load %221 : !llvm.ptr -> i64
        %223 = arith.index_cast %215 : index to i64
        %224 = llvm.getelementptr %135[%223] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %222 = llvm.load %224 : !llvm.ptr -> i64
        %225 = arith.addi %216, %222 : i64
        %227 = arith.index_cast %215 : index to i64
        %228 = llvm.getelementptr %127[%227] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %226 = llvm.load %228 : !llvm.ptr -> i64
        %229 = arith.subi %225, %226 : i64
        %230 = llvm.mlir.constant(1 : i64) : i64
        %231 = llvm.alloca %230 x i64 : (i64) -> !llvm.ptr
        llvm.store %229, %231 : i64, !llvm.ptr
        %232 = llvm.load %231 : !llvm.ptr -> i64
        %233 = llvm.mlir.addressof @MOD : !llvm.ptr
        %234 = llvm.load %233 : !llvm.ptr -> i64
        %235 = arith.remsi %232, %234 : i64
        llvm.store %235, %231 : i64, !llvm.ptr
        %236 = llvm.load %231 : !llvm.ptr -> i64
        %237 = arith.constant 0 : i32
        %239 = arith.extsi %237 : i32 to i64
        %238 = arith.cmpi slt, %236, %239 : i64
        cf.cond_br %238, ^bb21, ^bb22
        ^bb21:
          %240 = llvm.load %231 : !llvm.ptr -> i64
          %241 = llvm.mlir.addressof @MOD : !llvm.ptr
          %242 = llvm.load %241 : !llvm.ptr -> i64
          %243 = arith.addi %240, %242 : i64
          llvm.store %243, %231 : i64, !llvm.ptr
          cf.br ^bb23
        ^bb22:
          cf.br ^bb23
        ^bb23:
        %244 = llvm.load %231 : !llvm.ptr -> i64
        %245 = arith.constant 1 : i32
        %247 = arith.index_cast %215 : index to i32
        %246 = arith.addi %247, %245 : i32
        %248 = arith.extsi %246 : i32 to i64
        %249 = llvm.getelementptr %167[%248] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %244, %249 : i64, !llvm.ptr
        %250 = arith.addi %215, %207 : index
        cf.br ^bb18(%250 : index)
      ^bb20(%251: index):
      %252 = arith.constant 0 : i32
      %253 = llvm.mlir.addressof @N : !llvm.ptr
      %254 = llvm.load %253 : !llvm.ptr -> i64
      %255 = arith.constant 1 : i32
      %257 = arith.extsi %255 : i32 to i64
      %256 = arith.addi %254, %257 : i64
      %258 = arith.index_cast %252 : i32 to index
      %259 = arith.index_cast %256 : i32 to index
      %261 = arith.constant 1 : index
      %262 = arith.constant -1 : index
      %263 = arith.cmpi sle, %258, %259 : index
      %260 = arith.select %263, %261, %262 : index
      cf.br ^bb24(%258 : index)
      ^bb24(%264: index):
      %265 = arith.cmpi slt, %264, %259 : index
      %266 = arith.cmpi sgt, %264, %259 : index
      %267 = arith.select %263, %265, %266 : i1
      cf.cond_br %267, ^bb25(%264 : index), ^bb26(%264 : index)
      ^bb25(%268: index):
        %270 = arith.index_cast %268 : index to i64
        %271 = llvm.getelementptr %135[%270] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %269 = llvm.load %271 : !llvm.ptr -> i64
        %272 = arith.index_cast %268 : index to i64
        %273 = llvm.getelementptr %127[%272] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %269, %273 : i64, !llvm.ptr
        %274 = arith.addi %268, %260 : index
        cf.br ^bb24(%274 : index)
      ^bb26(%275: index):
      %276 = arith.constant 0 : i32
      %277 = llvm.mlir.addressof @N : !llvm.ptr
      %278 = llvm.load %277 : !llvm.ptr -> i64
      %279 = arith.constant 1 : i32
      %281 = arith.extsi %279 : i32 to i64
      %280 = arith.addi %278, %281 : i64
      %282 = arith.index_cast %276 : i32 to index
      %283 = arith.index_cast %280 : i32 to index
      %285 = arith.constant 1 : index
      %286 = arith.constant -1 : index
      %287 = arith.cmpi sle, %282, %283 : index
      %284 = arith.select %287, %285, %286 : index
      cf.br ^bb27(%282 : index)
      ^bb27(%288: index):
      %289 = arith.cmpi slt, %288, %283 : index
      %290 = arith.cmpi sgt, %288, %283 : index
      %291 = arith.select %287, %289, %290 : i1
      cf.cond_br %291, ^bb28(%288 : index), ^bb29(%288 : index)
      ^bb28(%292: index):
        %294 = arith.index_cast %292 : index to i64
        %295 = llvm.getelementptr %167[%294] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %293 = llvm.load %295 : !llvm.ptr -> i64
        %296 = arith.index_cast %292 : index to i64
        %297 = llvm.getelementptr %135[%296] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %293, %297 : i64, !llvm.ptr
        %298 = arith.addi %292, %284 : index
        cf.br ^bb27(%298 : index)
      ^bb29(%299: index):
      %300 = arith.constant 2 : i32
      %302 = arith.index_cast %194 : index to i32
      %301 = arith.remsi %302, %300 : i32
      %303 = arith.constant 0 : i32
      %304 = arith.cmpi ne, %301, %303 : i32
      cf.cond_br %304, ^bb30, ^bb31
      ^bb30:
        %305 = arith.addi %194, %186 : index
        cf.br ^bb15(%305 : index)
      ^bb31:
        cf.br ^bb32
      ^bb32:
      %306 = llvm.mlir.addressof @N : !llvm.ptr
      %307 = llvm.load %306 : !llvm.ptr -> i64
      %308 = llvm.load %95 : !llvm.ptr -> i64
      %309 = arith.subi %307, %308 : i64
      %310 = llvm.mlir.addressof @N : !llvm.ptr
      %311 = llvm.load %310 : !llvm.ptr -> i64
      %312 = llvm.load %95 : !llvm.ptr -> i64
      %313 = arith.subi %311, %312 : i64
      %314 = arith.constant 0 : i32
      %316 = arith.extsi %314 : i32 to i64
      %315 = arith.cmpi sle, %313, %316 : i64
      cf.cond_br %315, ^bb33, ^bb34
      ^bb33:
        %317 = arith.addi %194, %186 : index
        cf.br ^bb15(%317 : index)
      ^bb34:
        cf.br ^bb35
      ^bb35:
      %319 = arith.constant 8 : i32
      %320 = arith.extsi %319 : i32 to i64
      %318 = func.call @calloc(%313, %320) : (i64, i64) -> !llvm.ptr
      %322 = arith.constant 8 : i32
      %323 = arith.extsi %322 : i32 to i64
      %321 = func.call @calloc(%313, %323) : (i64, i64) -> !llvm.ptr
      %324 = arith.constant 0 : i32
      %325 = arith.index_cast %324 : i32 to index
      %326 = arith.index_cast %313 : i32 to index
      %328 = arith.constant 1 : index
      %329 = arith.constant -1 : index
      %330 = arith.cmpi sle, %325, %326 : index
      %327 = arith.select %330, %328, %329 : index
      cf.br ^bb36(%325 : index)
      ^bb36(%331: index):
      %332 = arith.cmpi slt, %331, %326 : index
      %333 = arith.cmpi sgt, %331, %326 : index
      %334 = arith.select %330, %332, %333 : i1
      cf.cond_br %334, ^bb37(%331 : index), ^bb38(%331 : index)
      ^bb37(%335: index):
        %337 = llvm.load %95 : !llvm.ptr -> i64
        %339 = arith.trunci %337 : i64 to i32
        %340 = arith.index_cast %335 : index to i32
        %338 = arith.addi %339, %340 : i32
        %341 = arith.extsi %338 : i32 to i64
        %342 = llvm.getelementptr %71[%341] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %336 = llvm.load %342 : !llvm.ptr -> i64
        %343 = arith.index_cast %335 : index to i64
        %344 = llvm.getelementptr %318[%343] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %336, %344 : i64, !llvm.ptr
        %345 = arith.addi %335, %327 : index
        cf.br ^bb36(%345 : index)
      ^bb38(%346: index):
      %347 = arith.constant 0 : i32
      %348 = arith.index_cast %347 : i32 to index
      %349 = arith.index_cast %313 : i32 to index
      %351 = arith.constant 1 : index
      %352 = arith.constant -1 : index
      %353 = arith.cmpi sle, %348, %349 : index
      %350 = arith.select %353, %351, %352 : index
      cf.br ^bb39(%348 : index)
      ^bb39(%354: index):
      %355 = arith.cmpi slt, %354, %349 : index
      %356 = arith.cmpi sgt, %354, %349 : index
      %357 = arith.select %353, %355, %356 : i1
      cf.cond_br %357, ^bb40(%354 : index), ^bb41(%354 : index)
      ^bb40(%358: index):
        %359 = arith.constant 1 : i32
        %361 = arith.index_cast %358 : index to i32
        %360 = arith.addi %361, %359 : i32
        %362 = llvm.mlir.addressof @N : !llvm.ptr
        %363 = llvm.load %362 : !llvm.ptr -> i64
        %365 = arith.extsi %360 : i32 to i64
        %364 = arith.cmpi sle, %365, %363 : i64
        cf.cond_br %364, ^bb42, ^bb43
        ^bb42:
          %367 = arith.constant 1 : i32
          %369 = arith.index_cast %358 : index to i32
          %368 = arith.addi %369, %367 : i32
          %370 = arith.extsi %368 : i32 to i64
          %371 = llvm.getelementptr %167[%370] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %366 = llvm.load %371 : !llvm.ptr -> i64
          %372 = arith.index_cast %358 : index to i64
          %373 = llvm.getelementptr %321[%372] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %366, %373 : i64, !llvm.ptr
          cf.br ^bb44
        ^bb43:
          cf.br ^bb44
        ^bb44:
        %374 = arith.addi %358, %350 : index
        cf.br ^bb39(%374 : index)
      ^bb41(%375: index):
      %376 = func.call @mul_trunc(%318, %321, %313) : (!llvm.ptr, !llvm.ptr, i64) -> !llvm.ptr
      %377 = llvm.load %95 : !llvm.ptr -> i64
      %378 = arith.constant 1 : i32
      %380 = arith.extsi %378 : i32 to i64
      %379 = arith.addi %377, %380 : i64
      llvm.store %379, %95 : i64, !llvm.ptr
      %382 = llvm.mlir.addressof @N : !llvm.ptr
      %383 = llvm.load %382 : !llvm.ptr -> i64
      %384 = arith.constant 1 : i32
      %386 = arith.extsi %384 : i32 to i64
      %385 = arith.addi %383, %386 : i64
      %387 = arith.constant 8 : i32
      %388 = arith.extsi %387 : i32 to i64
      %381 = func.call @calloc(%385, %388) : (i64, i64) -> !llvm.ptr
      %389 = arith.constant 0 : i32
      %390 = arith.index_cast %389 : i32 to index
      %391 = arith.index_cast %313 : i32 to index
      %393 = arith.constant 1 : index
      %394 = arith.constant -1 : index
      %395 = arith.cmpi sle, %390, %391 : index
      %392 = arith.select %395, %393, %394 : index
      cf.br ^bb45(%390 : index)
      ^bb45(%396: index):
      %397 = arith.cmpi slt, %396, %391 : index
      %398 = arith.cmpi sgt, %396, %391 : index
      %399 = arith.select %395, %397, %398 : i1
      cf.cond_br %399, ^bb46(%396 : index), ^bb47(%396 : index)
      ^bb46(%400: index):
        %402 = arith.index_cast %400 : index to i64
        %403 = llvm.getelementptr %376[%402] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %401 = llvm.load %403 : !llvm.ptr -> i64
        %404 = llvm.load %95 : !llvm.ptr -> i64
        %406 = arith.trunci %404 : i64 to i32
        %407 = arith.index_cast %400 : index to i32
        %405 = arith.addi %406, %407 : i32
        %408 = arith.extsi %405 : i32 to i64
        %409 = llvm.getelementptr %381[%408] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %401, %409 : i64, !llvm.ptr
        %410 = arith.addi %400, %392 : index
        cf.br ^bb45(%410 : index)
      ^bb47(%411: index):
      %412 = arith.constant 0 : i32
      %413 = llvm.mlir.addressof @N : !llvm.ptr
      %414 = llvm.load %413 : !llvm.ptr -> i64
      %415 = arith.constant 1 : i32
      %417 = arith.extsi %415 : i32 to i64
      %416 = arith.addi %414, %417 : i64
      %418 = arith.index_cast %412 : i32 to index
      %419 = arith.index_cast %416 : i32 to index
      %421 = arith.constant 1 : index
      %422 = arith.constant -1 : index
      %423 = arith.cmpi sle, %418, %419 : index
      %420 = arith.select %423, %421, %422 : index
      cf.br ^bb48(%418 : index)
      ^bb48(%424: index):
      %425 = arith.cmpi slt, %424, %419 : index
      %426 = arith.cmpi sgt, %424, %419 : index
      %427 = arith.select %423, %425, %426 : i1
      cf.cond_br %427, ^bb49(%424 : index), ^bb50(%424 : index)
      ^bb49(%428: index):
        %430 = arith.index_cast %428 : index to i64
        %431 = llvm.getelementptr %381[%430] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %429 = llvm.load %431 : !llvm.ptr -> i64
        %432 = arith.index_cast %428 : index to i64
        %433 = llvm.getelementptr %71[%432] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %429, %433 : i64, !llvm.ptr
        %434 = arith.addi %428, %420 : index
        cf.br ^bb48(%434 : index)
      ^bb50(%435: index):
      %436 = arith.constant 0 : i32
      %437 = arith.index_cast %436 : i32 to index
      %438 = arith.index_cast %313 : i32 to index
      %440 = arith.constant 1 : index
      %441 = arith.constant -1 : index
      %442 = arith.cmpi sle, %437, %438 : index
      %439 = arith.select %442, %440, %441 : index
      cf.br ^bb51(%437 : index)
      ^bb51(%443: index):
      %444 = arith.cmpi slt, %443, %438 : index
      %445 = arith.cmpi sgt, %443, %438 : index
      %446 = arith.select %442, %444, %445 : i1
      cf.cond_br %446, ^bb52(%443 : index), ^bb53(%443 : index)
      ^bb52(%447: index):
        %449 = llvm.load %95 : !llvm.ptr -> i64
        %451 = arith.trunci %449 : i64 to i32
        %452 = arith.index_cast %447 : index to i32
        %450 = arith.addi %451, %452 : i32
        %453 = arith.extsi %450 : i32 to i64
        %454 = llvm.getelementptr %63[%453] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %448 = llvm.load %454 : !llvm.ptr -> i64
        %456 = arith.index_cast %447 : index to i64
        %457 = llvm.getelementptr %376[%456] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %455 = llvm.load %457 : !llvm.ptr -> i64
        %458 = arith.addi %448, %455 : i64
        %459 = llvm.mlir.addressof @MOD : !llvm.ptr
        %460 = llvm.load %459 : !llvm.ptr -> i64
        %461 = arith.remsi %458, %460 : i64
        %462 = llvm.load %95 : !llvm.ptr -> i64
        %464 = arith.trunci %462 : i64 to i32
        %465 = arith.index_cast %447 : index to i32
        %463 = arith.addi %464, %465 : i32
        %466 = arith.extsi %463 : i32 to i64
        %467 = llvm.getelementptr %63[%466] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %461, %467 : i64, !llvm.ptr
        %468 = arith.addi %447, %439 : index
        cf.br ^bb51(%468 : index)
      ^bb53(%469: index):
      func.call @free(%376) : (!llvm.ptr) -> ()
      func.call @free(%321) : (!llvm.ptr) -> ()
      func.call @free(%318) : (!llvm.ptr) -> ()
      func.call @free(%381) : (!llvm.ptr) -> ()
      %474 = arith.addi %194, %186 : index
      cf.br ^bb15(%474 : index)
    ^bb17(%475: index):
    %476 = arith.constant 0 : i32
    %477 = arith.extsi %476 : i32 to i64
    %478 = llvm.mlir.constant(1 : i64) : i64
    %479 = llvm.alloca %478 x i64 : (i64) -> !llvm.ptr
    llvm.store %477, %479 : i64, !llvm.ptr
    %480 = arith.constant 0 : i32
    %481 = llvm.mlir.addressof @N : !llvm.ptr
    %482 = llvm.load %481 : !llvm.ptr -> i64
    %483 = arith.constant 1 : i32
    %485 = arith.extsi %483 : i32 to i64
    %484 = arith.addi %482, %485 : i64
    %486 = arith.index_cast %480 : i32 to index
    %487 = arith.index_cast %484 : i32 to index
    %489 = arith.constant 1 : index
    %490 = arith.constant -1 : index
    %491 = arith.cmpi sle, %486, %487 : index
    %488 = arith.select %491, %489, %490 : index
    cf.br ^bb54(%486 : index)
    ^bb54(%492: index):
    %493 = arith.cmpi slt, %492, %487 : index
    %494 = arith.cmpi sgt, %492, %487 : index
    %495 = arith.select %491, %493, %494 : i1
    cf.cond_br %495, ^bb55(%492 : index), ^bb56(%492 : index)
    ^bb55(%496: index):
      %497 = llvm.load %479 : !llvm.ptr -> i64
      %499 = arith.index_cast %496 : index to i64
      %500 = llvm.getelementptr %63[%499] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %498 = llvm.load %500 : !llvm.ptr -> i64
      %501 = arith.addi %497, %498 : i64
      %502 = llvm.mlir.addressof @MOD : !llvm.ptr
      %503 = llvm.load %502 : !llvm.ptr -> i64
      %504 = arith.remsi %501, %503 : i64
      llvm.store %504, %479 : i64, !llvm.ptr
      %505 = arith.addi %496, %488 : index
      cf.br ^bb54(%505 : index)
    ^bb56(%506: index):
    %507 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %508 = llvm.load %479 : !llvm.ptr -> i64
    %509 = llvm.call @printf(%507, %508) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%167) : (!llvm.ptr) -> ()
    func.call @free(%135) : (!llvm.ptr) -> ()
    func.call @free(%127) : (!llvm.ptr) -> ()
    func.call @free(%71) : (!llvm.ptr) -> ()
    func.call @free(%63) : (!llvm.ptr) -> ()
    %515 = arith.constant 0 : i32
    func.return %515 : i32
  }
}