Problem 705

Method: F(N) decomposes over digit pairs. Stream the digits of all primes below 10^8 (zeros skipped) left to right, keeping W[v] = weighted count of earlier positions whose chosen divisor is v, scaled by the product of the divisor counts of every other processed position. At each digit d with divisor set D and cnt = |D|: ans = ans*cnt + sum over y in D of sum_{x > y} W[x], then W[v] = W[v]*cnt (+ P for v in D), P = P*cnt. Primes come from an odd-only byte sieve of size 10^8. All arithmetic modulo 1e9+7.

Answer480440153
Output480440153
StatusPASS
Native helperno
Runtime1550 ms
Peak memory49952 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve of Eratosthenes
VerdictSuboptimal

Flow source

# Project Euler 705: Total Inversion Count of Divided Sequences
# Method: F(N) decomposes over digit pairs. Stream the digits of all primes
# below 10^8 (zeros skipped) left to right, keeping W[v] = weighted count of
# earlier positions whose chosen divisor is v, scaled by the product of the
# divisor counts of every other processed position. At each digit d with
# divisor set D and cnt = |D|: ans = ans*cnt + sum over y in D of
# sum_{x > y} W[x], then W[v] = W[v]*cnt (+ P for v in D), P = P*cnt.
# Primes come from an odd-only byte sieve of size 10^8. All arithmetic
# modulo 1e9+7.

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

function main() -> i32 {
    let MOD: i64 = 1000000007
    let N: i64 = 100000000

    # comp[i] marks odd number 2*i+1 as composite (index 0 -> 1, unused)
    let half: i64 = N / 2
    let comp: ptr<i8> = calloc(half, 1) as ptr<i8>
    let mut i: i64 = 1
    while (2 * i + 1) * (2 * i + 1) < N {
        if comp[i] == 0 {
            let p: i64 = 2 * i + 1
            let mut j: i64 = (p * p) / 2
            while j < half {
                comp[j] = 1
                j = j + p
            }
        }
        i = i + 1
    }

    # divisor sets per digit: bitmask over values 1..9, and counts
    let dmask: ptr<i64> = calloc(10, 8) as ptr<i64>
    let dcnt: ptr<i64> = calloc(10, 8) as ptr<i64>
    let mut d: i64 = 1
    while d <= 9 {
        let mut m: i64 = 0
        let mut c: i64 = 0
        let mut v: i64 = 1
        while v <= d {
            if d % v == 0 {
                m = m + (1 << v)
                c = c + 1
            }
            v = v + 1
        }
        dmask[d] = m
        dcnt[d] = c
        d = d + 1
    }

    let W: ptr<i64> = calloc(10, 8) as ptr<i64>
    let digs: ptr<i64> = calloc(16, 8) as ptr<i64>
    let mut P: i64 = 1
    let mut ans: i64 = 0

    let mut prime: i64 = 2
    while prime < N {
        # extract nonzero digits of prime, most significant first
        let mut nd: i64 = 0
        let mut t: i64 = prime
        while t > 0 {
            let dig: i64 = t % 10
            if dig != 0 {
                digs[nd] = dig
                nd = nd + 1
            }
            t = t / 10
        }
        let mut k: i64 = nd - 1
        while k >= 0 {
            let dg: i64 = digs[k]
            let mask: i64 = dmask[dg]
            let cnt: i64 = dcnt[dg]
            # add = sum over y in divs(dg) of sum_{x > y} W[x]
            let mut add: i64 = 0
            let mut suf: i64 = 0
            let mut v: i64 = 9
            while v >= 1 {
                # before including W[v], suf = sum_{x > v} W[x]
                if (mask / (1 << v)) % 2 == 1 {
                    add = (add + suf) % MOD
                }
                suf = (suf + W[v]) % MOD
                v = v - 1
            }
            ans = (ans * cnt + add) % MOD
            v = 1
            while v <= 9 {
                W[v] = (W[v] * cnt) % MOD
                v = v + 1
            }
            v = 1
            while v <= 9 {
                if (mask / (1 << v)) % 2 == 1 {
                    W[v] = (W[v] + P) % MOD
                }
                v = v + 1
            }
            P = (P * cnt) % MOD
            k = k - 1
        }
        # advance to next prime
        if prime == 2 {
            prime = 3
        } else {
            let mut idx: i64 = prime / 2 + 1
            while idx < half && comp[idx] == 1 {
                idx = idx + 1
            }
            if idx >= half {
                prime = N
            } else {
                prime = 2 * idx + 1
            }
        }
    }

    free(comp as ptr<void>)
    free(dmask as ptr<void>)
    free(dcnt as ptr<void>)
    free(W as ptr<void>)
    free(digs as ptr<void>)

    printf("%lld\n", ans)
    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; }

int32_t main(void);



int32_t main(void) {
    int64_t MOD = 1000000007;
    int64_t N = 100000000;
    int64_t half = FLOW_CHECKED_DIV((N), (2));
    int8_t* comp = (int8_t*)(((int8_t*)(calloc(half, 1))));
    int64_t i = 1;
    while ((((2 * i) + 1) * ((2 * i) + 1)) < N) {
        if (comp[i] == 0) {
            int64_t p = ((2 * i) + 1);
            int64_t j = FLOW_CHECKED_DIV(((p * p)), (2));
            while (j < half) {
                comp[j] = 1;
                j = (j + p);
            }
        }
        i = (i + 1);
    }
    int64_t* dmask = (int64_t*)(((int64_t*)(calloc(10, 8))));
    int64_t* dcnt = (int64_t*)(((int64_t*)(calloc(10, 8))));
    int64_t d = 1;
    while (d <= 9) {
        int64_t m = 0;
        int64_t c = 0;
        int64_t v = 1;
        while (v <= d) {
            if (FLOW_CHECKED_MOD((d), (v)) == 0) {
                m = (m + FLOW_CHECKED_SHL((1), (v)));
                c = (c + 1);
            }
            v = (v + 1);
        }
        dmask[d] = m;
        dcnt[d] = c;
        d = (d + 1);
    }
    int64_t* W = (int64_t*)(((int64_t*)(calloc(10, 8))));
    int64_t* digs = (int64_t*)(((int64_t*)(calloc(16, 8))));
    int64_t P = 1;
    int64_t ans = 0;
    int64_t prime = 2;
    while (prime < N) {
        int64_t nd = 0;
        int64_t t = prime;
        while (t > 0) {
            int64_t dig = FLOW_CHECKED_MOD((t), (10));
            if (dig != 0) {
                digs[nd] = dig;
                nd = (nd + 1);
            }
            t = FLOW_CHECKED_DIV((t), (10));
        }
        int64_t k = (nd - 1);
        while (k >= 0) {
            int64_t dg = digs[k];
            int64_t mask = dmask[dg];
            int64_t cnt = dcnt[dg];
            int64_t add = 0;
            int64_t suf = 0;
            int64_t v = 9;
            while (v >= 1) {
                if (FLOW_CHECKED_MOD((FLOW_CHECKED_DIV((mask), (FLOW_CHECKED_SHL((1), (v))))), (2)) == 1) {
                    add = FLOW_CHECKED_MOD(((add + suf)), (MOD));
                }
                suf = FLOW_CHECKED_MOD(((suf + W[v])), (MOD));
                v = (v - 1);
            }
            ans = FLOW_CHECKED_MOD((((ans * cnt) + add)), (MOD));
            v = 1;
            while (v <= 9) {
                W[v] = FLOW_CHECKED_MOD(((W[v] * cnt)), (MOD));
                v = (v + 1);
            }
            v = 1;
            while (v <= 9) {
                if (FLOW_CHECKED_MOD((FLOW_CHECKED_DIV((mask), (FLOW_CHECKED_SHL((1), (v))))), (2)) == 1) {
                    W[v] = FLOW_CHECKED_MOD(((W[v] + P)), (MOD));
                }
                v = (v + 1);
            }
            P = FLOW_CHECKED_MOD(((P * cnt)), (MOD));
            k = (k - 1);
        }
        if (prime == 2) {
            prime = 3;
        } else {
            int64_t idx = (FLOW_CHECKED_DIV((prime), (2)) + 1);
            while ((idx < half && comp[idx] == 1)) {
                idx = (idx + 1);
            }
            if (idx >= half) {
                prime = N;
            } else {
                prime = ((2 * idx) + 1);
            }
        }
    }
    free(((void*)(comp)));
    free(((void*)(dmask)));
    free(((void*)(dcnt)));
    free(((void*)(W)));
    free(((void*)(digs)));
    printf("%lld\n", ans);
    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 @main() -> i32 {
    %0 = arith.constant 1000000007 : i32
    %1 = arith.extsi %0 : i32 to i64
    %2 = arith.constant 100000000 : i32
    %3 = arith.extsi %2 : i32 to i64
    %4 = arith.constant 2 : i32
    %6 = arith.extsi %4 : i32 to i64
    %5 = arith.divsi %3, %6 : i64
    %8 = arith.constant 1 : i32
    %9 = arith.extsi %8 : i32 to i64
    %7 = func.call @calloc(%5, %9) : (i64, i64) -> !llvm.ptr
    %10 = arith.constant 1 : i32
    %11 = arith.extsi %10 : i32 to i64
    %12 = llvm.mlir.constant(1 : i64) : i64
    %13 = llvm.alloca %12 x i64 : (i64) -> !llvm.ptr
    llvm.store %11, %13 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %14 = arith.constant 2 : i32
    %15 = llvm.load %13 : !llvm.ptr -> i64
    %17 = arith.extsi %14 : i32 to i64
    %16 = arith.muli %17, %15 : i64
    %18 = arith.constant 1 : i32
    %20 = arith.extsi %18 : i32 to i64
    %19 = arith.addi %16, %20 : i64
    %21 = arith.constant 2 : i32
    %22 = llvm.load %13 : !llvm.ptr -> i64
    %24 = arith.extsi %21 : i32 to i64
    %23 = arith.muli %24, %22 : i64
    %25 = arith.constant 1 : i32
    %27 = arith.extsi %25 : i32 to i64
    %26 = arith.addi %23, %27 : i64
    %28 = arith.muli %19, %26 : i64
    %29 = arith.cmpi slt, %28, %3 : i64
    cf.cond_br %29, ^bb1, ^bb2
    ^bb1:
      %31 = llvm.load %13 : !llvm.ptr -> i64
      %32 = llvm.getelementptr %7[%31] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %30 = llvm.load %32 : !llvm.ptr -> i8
      %33 = arith.constant 0 : i32
      %35 = arith.extsi %30 : i8 to i32
      %34 = arith.cmpi eq, %35, %33 : i32
      cf.cond_br %34, ^bb3, ^bb4
      ^bb3:
        %36 = arith.constant 2 : i32
        %37 = llvm.load %13 : !llvm.ptr -> i64
        %39 = arith.extsi %36 : i32 to i64
        %38 = arith.muli %39, %37 : i64
        %40 = arith.constant 1 : i32
        %42 = arith.extsi %40 : i32 to i64
        %41 = arith.addi %38, %42 : i64
        %43 = arith.muli %41, %41 : i64
        %44 = arith.constant 2 : i32
        %46 = arith.extsi %44 : i32 to i64
        %45 = arith.divsi %43, %46 : i64
        %47 = llvm.mlir.constant(1 : i64) : i64
        %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
        llvm.store %45, %48 : i64, !llvm.ptr
        cf.br ^bb6
        ^bb6:
        %49 = llvm.load %48 : !llvm.ptr -> i64
        %50 = arith.cmpi slt, %49, %5 : i64
        cf.cond_br %50, ^bb7, ^bb8
        ^bb7:
          %51 = arith.constant 1 : i32
          %52 = llvm.load %48 : !llvm.ptr -> i64
          %53 = arith.trunci %51 : i32 to i8
          %54 = llvm.getelementptr %7[%52] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          llvm.store %53, %54 : i8, !llvm.ptr
          %55 = llvm.load %48 : !llvm.ptr -> i64
          %56 = arith.addi %55, %41 : i64
          llvm.store %56, %48 : i64, !llvm.ptr
          cf.br ^bb6
        ^bb8:
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %57 = llvm.load %13 : !llvm.ptr -> i64
      %58 = arith.constant 1 : i32
      %60 = arith.extsi %58 : i32 to i64
      %59 = arith.addi %57, %60 : i64
      llvm.store %59, %13 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %62 = arith.constant 10 : i32
    %63 = arith.constant 8 : i32
    %64 = arith.extsi %62 : i32 to i64
    %65 = arith.extsi %63 : i32 to i64
    %61 = func.call @calloc(%64, %65) : (i64, i64) -> !llvm.ptr
    %67 = arith.constant 10 : i32
    %68 = arith.constant 8 : i32
    %69 = arith.extsi %67 : i32 to i64
    %70 = arith.extsi %68 : i32 to i64
    %66 = func.call @calloc(%69, %70) : (i64, i64) -> !llvm.ptr
    %71 = arith.constant 1 : i32
    %72 = arith.extsi %71 : i32 to i64
    %73 = llvm.mlir.constant(1 : i64) : i64
    %74 = llvm.alloca %73 x i64 : (i64) -> !llvm.ptr
    llvm.store %72, %74 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %75 = llvm.load %74 : !llvm.ptr -> i64
    %76 = arith.constant 9 : i32
    %78 = arith.extsi %76 : i32 to i64
    %77 = arith.cmpi sle, %75, %78 : i64
    cf.cond_br %77, ^bb10, ^bb11
    ^bb10:
      %79 = arith.constant 0 : i32
      %80 = arith.extsi %79 : i32 to i64
      %81 = llvm.mlir.constant(1 : i64) : i64
      %82 = llvm.alloca %81 x i64 : (i64) -> !llvm.ptr
      llvm.store %80, %82 : i64, !llvm.ptr
      %83 = arith.constant 0 : i32
      %84 = arith.extsi %83 : i32 to i64
      %85 = llvm.mlir.constant(1 : i64) : i64
      %86 = llvm.alloca %85 x i64 : (i64) -> !llvm.ptr
      llvm.store %84, %86 : i64, !llvm.ptr
      %87 = arith.constant 1 : i32
      %88 = arith.extsi %87 : i32 to i64
      %89 = llvm.mlir.constant(1 : i64) : i64
      %90 = llvm.alloca %89 x i64 : (i64) -> !llvm.ptr
      llvm.store %88, %90 : i64, !llvm.ptr
      cf.br ^bb12
      ^bb12:
      %91 = llvm.load %90 : !llvm.ptr -> i64
      %92 = llvm.load %74 : !llvm.ptr -> i64
      %93 = arith.cmpi sle, %91, %92 : i64
      cf.cond_br %93, ^bb13, ^bb14
      ^bb13:
        %94 = llvm.load %74 : !llvm.ptr -> i64
        %95 = llvm.load %90 : !llvm.ptr -> i64
        %96 = arith.remsi %94, %95 : i64
        %97 = arith.constant 0 : i32
        %99 = arith.extsi %97 : i32 to i64
        %98 = arith.cmpi eq, %96, %99 : i64
        cf.cond_br %98, ^bb15, ^bb16
        ^bb15:
          %100 = llvm.load %82 : !llvm.ptr -> i64
          %101 = arith.constant 1 : i32
          %102 = llvm.load %90 : !llvm.ptr -> i64
          %104 = arith.extsi %101 : i32 to i64
          %103 = arith.shli %104, %102 : i64
          %105 = arith.addi %100, %103 : i64
          llvm.store %105, %82 : i64, !llvm.ptr
          %106 = llvm.load %86 : !llvm.ptr -> i64
          %107 = arith.constant 1 : i32
          %109 = arith.extsi %107 : i32 to i64
          %108 = arith.addi %106, %109 : i64
          llvm.store %108, %86 : i64, !llvm.ptr
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        %110 = llvm.load %90 : !llvm.ptr -> i64
        %111 = arith.constant 1 : i32
        %113 = arith.extsi %111 : i32 to i64
        %112 = arith.addi %110, %113 : i64
        llvm.store %112, %90 : i64, !llvm.ptr
        cf.br ^bb12
      ^bb14:
      %114 = llvm.load %82 : !llvm.ptr -> i64
      %115 = llvm.load %74 : !llvm.ptr -> i64
      %116 = llvm.getelementptr %61[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %114, %116 : i64, !llvm.ptr
      %117 = llvm.load %86 : !llvm.ptr -> i64
      %118 = llvm.load %74 : !llvm.ptr -> i64
      %119 = llvm.getelementptr %66[%118] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %117, %119 : i64, !llvm.ptr
      %120 = llvm.load %74 : !llvm.ptr -> i64
      %121 = arith.constant 1 : i32
      %123 = arith.extsi %121 : i32 to i64
      %122 = arith.addi %120, %123 : i64
      llvm.store %122, %74 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %125 = arith.constant 10 : i32
    %126 = arith.constant 8 : i32
    %127 = arith.extsi %125 : i32 to i64
    %128 = arith.extsi %126 : i32 to i64
    %124 = func.call @calloc(%127, %128) : (i64, i64) -> !llvm.ptr
    %130 = arith.constant 16 : i32
    %131 = arith.constant 8 : i32
    %132 = arith.extsi %130 : i32 to i64
    %133 = arith.extsi %131 : i32 to i64
    %129 = func.call @calloc(%132, %133) : (i64, i64) -> !llvm.ptr
    %134 = arith.constant 1 : i32
    %135 = arith.extsi %134 : i32 to i64
    %136 = llvm.mlir.constant(1 : i64) : i64
    %137 = llvm.alloca %136 x i64 : (i64) -> !llvm.ptr
    llvm.store %135, %137 : i64, !llvm.ptr
    %138 = arith.constant 0 : i32
    %139 = arith.extsi %138 : i32 to i64
    %140 = llvm.mlir.constant(1 : i64) : i64
    %141 = llvm.alloca %140 x i64 : (i64) -> !llvm.ptr
    llvm.store %139, %141 : i64, !llvm.ptr
    %142 = arith.constant 2 : i32
    %143 = arith.extsi %142 : i32 to i64
    %144 = llvm.mlir.constant(1 : i64) : i64
    %145 = llvm.alloca %144 x i64 : (i64) -> !llvm.ptr
    llvm.store %143, %145 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %146 = llvm.load %145 : !llvm.ptr -> i64
    %147 = arith.cmpi slt, %146, %3 : i64
    cf.cond_br %147, ^bb19, ^bb20
    ^bb19:
      %148 = arith.constant 0 : i32
      %149 = arith.extsi %148 : i32 to i64
      %150 = llvm.mlir.constant(1 : i64) : i64
      %151 = llvm.alloca %150 x i64 : (i64) -> !llvm.ptr
      llvm.store %149, %151 : i64, !llvm.ptr
      %152 = llvm.load %145 : !llvm.ptr -> i64
      %153 = llvm.mlir.constant(1 : i64) : i64
      %154 = llvm.alloca %153 x i64 : (i64) -> !llvm.ptr
      llvm.store %152, %154 : i64, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %155 = llvm.load %154 : !llvm.ptr -> i64
      %156 = arith.constant 0 : i32
      %158 = arith.extsi %156 : i32 to i64
      %157 = arith.cmpi sgt, %155, %158 : i64
      cf.cond_br %157, ^bb22, ^bb23
      ^bb22:
        %159 = llvm.load %154 : !llvm.ptr -> i64
        %160 = arith.constant 10 : i32
        %162 = arith.extsi %160 : i32 to i64
        %161 = arith.remsi %159, %162 : i64
        %163 = arith.constant 0 : i32
        %165 = arith.extsi %163 : i32 to i64
        %164 = arith.cmpi ne, %161, %165 : i64
        cf.cond_br %164, ^bb24, ^bb25
        ^bb24:
          %166 = llvm.load %151 : !llvm.ptr -> i64
          %167 = llvm.getelementptr %129[%166] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %161, %167 : i64, !llvm.ptr
          %168 = llvm.load %151 : !llvm.ptr -> i64
          %169 = arith.constant 1 : i32
          %171 = arith.extsi %169 : i32 to i64
          %170 = arith.addi %168, %171 : i64
          llvm.store %170, %151 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          cf.br ^bb26
        ^bb26:
        %172 = llvm.load %154 : !llvm.ptr -> i64
        %173 = arith.constant 10 : i32
        %175 = arith.extsi %173 : i32 to i64
        %174 = arith.divsi %172, %175 : i64
        llvm.store %174, %154 : i64, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      %176 = llvm.load %151 : !llvm.ptr -> i64
      %177 = arith.constant 1 : i32
      %179 = arith.extsi %177 : i32 to i64
      %178 = arith.subi %176, %179 : i64
      %180 = llvm.mlir.constant(1 : i64) : i64
      %181 = llvm.alloca %180 x i64 : (i64) -> !llvm.ptr
      llvm.store %178, %181 : i64, !llvm.ptr
      cf.br ^bb27
      ^bb27:
      %182 = llvm.load %181 : !llvm.ptr -> i64
      %183 = arith.constant 0 : i32
      %185 = arith.extsi %183 : i32 to i64
      %184 = arith.cmpi sge, %182, %185 : i64
      cf.cond_br %184, ^bb28, ^bb29
      ^bb28:
        %187 = llvm.load %181 : !llvm.ptr -> i64
        %188 = llvm.getelementptr %129[%187] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %186 = llvm.load %188 : !llvm.ptr -> i64
        %190 = llvm.getelementptr %61[%186] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %189 = llvm.load %190 : !llvm.ptr -> i64
        %192 = llvm.getelementptr %66[%186] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %191 = llvm.load %192 : !llvm.ptr -> i64
        %193 = arith.constant 0 : i32
        %194 = arith.extsi %193 : i32 to i64
        %195 = llvm.mlir.constant(1 : i64) : i64
        %196 = llvm.alloca %195 x i64 : (i64) -> !llvm.ptr
        llvm.store %194, %196 : i64, !llvm.ptr
        %197 = arith.constant 0 : i32
        %198 = arith.extsi %197 : i32 to i64
        %199 = llvm.mlir.constant(1 : i64) : i64
        %200 = llvm.alloca %199 x i64 : (i64) -> !llvm.ptr
        llvm.store %198, %200 : i64, !llvm.ptr
        %201 = arith.constant 9 : i32
        %202 = arith.extsi %201 : i32 to i64
        %203 = llvm.mlir.constant(1 : i64) : i64
        %204 = llvm.alloca %203 x i64 : (i64) -> !llvm.ptr
        llvm.store %202, %204 : i64, !llvm.ptr
        cf.br ^bb30
        ^bb30:
        %205 = llvm.load %204 : !llvm.ptr -> i64
        %206 = arith.constant 1 : i32
        %208 = arith.extsi %206 : i32 to i64
        %207 = arith.cmpi sge, %205, %208 : i64
        cf.cond_br %207, ^bb31, ^bb32
        ^bb31:
          %209 = arith.constant 1 : i32
          %210 = llvm.load %204 : !llvm.ptr -> i64
          %212 = arith.extsi %209 : i32 to i64
          %211 = arith.shli %212, %210 : i64
          %213 = arith.divsi %189, %211 : i64
          %214 = arith.constant 2 : i32
          %216 = arith.extsi %214 : i32 to i64
          %215 = arith.remsi %213, %216 : i64
          %217 = arith.constant 1 : i32
          %219 = arith.extsi %217 : i32 to i64
          %218 = arith.cmpi eq, %215, %219 : i64
          cf.cond_br %218, ^bb33, ^bb34
          ^bb33:
            %220 = llvm.load %196 : !llvm.ptr -> i64
            %221 = llvm.load %200 : !llvm.ptr -> i64
            %222 = arith.addi %220, %221 : i64
            %223 = arith.remsi %222, %1 : i64
            llvm.store %223, %196 : i64, !llvm.ptr
            cf.br ^bb35
          ^bb34:
            cf.br ^bb35
          ^bb35:
          %224 = llvm.load %200 : !llvm.ptr -> i64
          %226 = llvm.load %204 : !llvm.ptr -> i64
          %227 = llvm.getelementptr %124[%226] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %225 = llvm.load %227 : !llvm.ptr -> i64
          %228 = arith.addi %224, %225 : i64
          %229 = arith.remsi %228, %1 : i64
          llvm.store %229, %200 : i64, !llvm.ptr
          %230 = llvm.load %204 : !llvm.ptr -> i64
          %231 = arith.constant 1 : i32
          %233 = arith.extsi %231 : i32 to i64
          %232 = arith.subi %230, %233 : i64
          llvm.store %232, %204 : i64, !llvm.ptr
          cf.br ^bb30
        ^bb32:
        %234 = llvm.load %141 : !llvm.ptr -> i64
        %235 = arith.muli %234, %191 : i64
        %236 = llvm.load %196 : !llvm.ptr -> i64
        %237 = arith.addi %235, %236 : i64
        %238 = arith.remsi %237, %1 : i64
        llvm.store %238, %141 : i64, !llvm.ptr
        %239 = arith.constant 1 : i32
        %240 = arith.extsi %239 : i32 to i64
        llvm.store %240, %204 : i64, !llvm.ptr
        cf.br ^bb36
        ^bb36:
        %241 = llvm.load %204 : !llvm.ptr -> i64
        %242 = arith.constant 9 : i32
        %244 = arith.extsi %242 : i32 to i64
        %243 = arith.cmpi sle, %241, %244 : i64
        cf.cond_br %243, ^bb37, ^bb38
        ^bb37:
          %246 = llvm.load %204 : !llvm.ptr -> i64
          %247 = llvm.getelementptr %124[%246] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %245 = llvm.load %247 : !llvm.ptr -> i64
          %248 = arith.muli %245, %191 : i64
          %249 = arith.remsi %248, %1 : i64
          %250 = llvm.load %204 : !llvm.ptr -> i64
          %251 = llvm.getelementptr %124[%250] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          llvm.store %249, %251 : i64, !llvm.ptr
          %252 = llvm.load %204 : !llvm.ptr -> i64
          %253 = arith.constant 1 : i32
          %255 = arith.extsi %253 : i32 to i64
          %254 = arith.addi %252, %255 : i64
          llvm.store %254, %204 : i64, !llvm.ptr
          cf.br ^bb36
        ^bb38:
        %256 = arith.constant 1 : i32
        %257 = arith.extsi %256 : i32 to i64
        llvm.store %257, %204 : i64, !llvm.ptr
        cf.br ^bb39
        ^bb39:
        %258 = llvm.load %204 : !llvm.ptr -> i64
        %259 = arith.constant 9 : i32
        %261 = arith.extsi %259 : i32 to i64
        %260 = arith.cmpi sle, %258, %261 : i64
        cf.cond_br %260, ^bb40, ^bb41
        ^bb40:
          %262 = arith.constant 1 : i32
          %263 = llvm.load %204 : !llvm.ptr -> i64
          %265 = arith.extsi %262 : i32 to i64
          %264 = arith.shli %265, %263 : i64
          %266 = arith.divsi %189, %264 : i64
          %267 = arith.constant 2 : i32
          %269 = arith.extsi %267 : i32 to i64
          %268 = arith.remsi %266, %269 : i64
          %270 = arith.constant 1 : i32
          %272 = arith.extsi %270 : i32 to i64
          %271 = arith.cmpi eq, %268, %272 : i64
          cf.cond_br %271, ^bb42, ^bb43
          ^bb42:
            %274 = llvm.load %204 : !llvm.ptr -> i64
            %275 = llvm.getelementptr %124[%274] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %273 = llvm.load %275 : !llvm.ptr -> i64
            %276 = llvm.load %137 : !llvm.ptr -> i64
            %277 = arith.addi %273, %276 : i64
            %278 = arith.remsi %277, %1 : i64
            %279 = llvm.load %204 : !llvm.ptr -> i64
            %280 = llvm.getelementptr %124[%279] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            llvm.store %278, %280 : i64, !llvm.ptr
            cf.br ^bb44
          ^bb43:
            cf.br ^bb44
          ^bb44:
          %281 = llvm.load %204 : !llvm.ptr -> i64
          %282 = arith.constant 1 : i32
          %284 = arith.extsi %282 : i32 to i64
          %283 = arith.addi %281, %284 : i64
          llvm.store %283, %204 : i64, !llvm.ptr
          cf.br ^bb39
        ^bb41:
        %285 = llvm.load %137 : !llvm.ptr -> i64
        %286 = arith.muli %285, %191 : i64
        %287 = arith.remsi %286, %1 : i64
        llvm.store %287, %137 : i64, !llvm.ptr
        %288 = llvm.load %181 : !llvm.ptr -> i64
        %289 = arith.constant 1 : i32
        %291 = arith.extsi %289 : i32 to i64
        %290 = arith.subi %288, %291 : i64
        llvm.store %290, %181 : i64, !llvm.ptr
        cf.br ^bb27
      ^bb29:
      %292 = llvm.load %145 : !llvm.ptr -> i64
      %293 = arith.constant 2 : i32
      %295 = arith.extsi %293 : i32 to i64
      %294 = arith.cmpi eq, %292, %295 : i64
      cf.cond_br %294, ^bb45, ^bb46
      ^bb45:
        %296 = arith.constant 3 : i32
        %297 = arith.extsi %296 : i32 to i64
        llvm.store %297, %145 : i64, !llvm.ptr
        cf.br ^bb47
      ^bb46:
        %298 = llvm.load %145 : !llvm.ptr -> i64
        %299 = arith.constant 2 : i32
        %301 = arith.extsi %299 : i32 to i64
        %300 = arith.divsi %298, %301 : i64
        %302 = arith.constant 1 : i32
        %304 = arith.extsi %302 : i32 to i64
        %303 = arith.addi %300, %304 : i64
        %305 = llvm.mlir.constant(1 : i64) : i64
        %306 = llvm.alloca %305 x i64 : (i64) -> !llvm.ptr
        llvm.store %303, %306 : i64, !llvm.ptr
        cf.br ^bb48
        ^bb48:
        %307 = llvm.load %306 : !llvm.ptr -> i64
        %308 = arith.cmpi slt, %307, %5 : i64
        %309 = scf.if %308 -> (i1) {
          %311 = llvm.load %306 : !llvm.ptr -> i64
          %312 = llvm.getelementptr %7[%311] : (!llvm.ptr, i64) -> !llvm.ptr, i8
          %310 = llvm.load %312 : !llvm.ptr -> i8
          %313 = arith.constant 1 : i32
          %315 = arith.extsi %310 : i8 to i32
          %314 = arith.cmpi eq, %315, %313 : i32
          scf.yield %314 : i1
        } else {
          %316 = arith.constant false
          scf.yield %316 : i1
        }
        cf.cond_br %309, ^bb49, ^bb50
        ^bb49:
          %317 = llvm.load %306 : !llvm.ptr -> i64
          %318 = arith.constant 1 : i32
          %320 = arith.extsi %318 : i32 to i64
          %319 = arith.addi %317, %320 : i64
          llvm.store %319, %306 : i64, !llvm.ptr
          cf.br ^bb48
        ^bb50:
        %321 = llvm.load %306 : !llvm.ptr -> i64
        %322 = arith.cmpi sge, %321, %5 : i64
        cf.cond_br %322, ^bb51, ^bb52
        ^bb51:
          llvm.store %3, %145 : i64, !llvm.ptr
          cf.br ^bb53
        ^bb52:
          %323 = arith.constant 2 : i32
          %324 = llvm.load %306 : !llvm.ptr -> i64
          %326 = arith.extsi %323 : i32 to i64
          %325 = arith.muli %326, %324 : i64
          %327 = arith.constant 1 : i32
          %329 = arith.extsi %327 : i32 to i64
          %328 = arith.addi %325, %329 : i64
          llvm.store %328, %145 : i64, !llvm.ptr
          cf.br ^bb53
        ^bb53:
        cf.br ^bb47
      ^bb47:
      cf.br ^bb18
    ^bb20:
    func.call @free(%7) : (!llvm.ptr) -> ()
    func.call @free(%61) : (!llvm.ptr) -> ()
    func.call @free(%66) : (!llvm.ptr) -> ()
    func.call @free(%124) : (!llvm.ptr) -> ()
    func.call @free(%129) : (!llvm.ptr) -> ()
    %335 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %336 = llvm.load %141 : !llvm.ptr -> i64
    %337 = llvm.call @printf(%335, %336) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %338 = arith.constant 0 : i32
    func.return %338 : i32
  }
}