Problem 528

Constrained Sums — inclusion-exclusion for S(10^k,k,k), k=10..15.

Answer779027989
Output779027989
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(1)
Space complexityO(1)O(1)
ApproachFlow solutionClosed-form formula
VerdictSuboptimal

Flow source

# Project Euler 528
# Constrained Sums — inclusion-exclusion for S(10^k,k,k), k=10..15.

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

const MOD: i64 = 1000000007

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    let mut e: i64 = exp0
    while e > 0 {
        if (e & 1) == 1 {
            r = ((r as i128) * (b as i128) % (mod as i128)) as i64
        }
        b = ((b as i128) * (b as i128) % (mod as i128)) as i64
        e = e / 2
    }
    return r
}

function constrained(n: i64, k: i32, b: i64, w: ptr<i64>) -> i64 {
    let mut pw: i64 = 1
    let mut i: i32 = 0
    while i < k {
        pw = pw * b
        w[i] = pw + 1
        i = i + 1
    }
    let mut fact: i64 = 1
    i = 2
    while i <= k {
        fact = (fact * (i as i64)) % MOD
        i = i + 1
    }
    let invk: i64 = modpow(fact, MOD - 2, MOD)
    let m: i32 = 1 << k
    let mut ans: i64 = 0
    let mut mask: i32 = 0
    while mask < m {
        let mut shift: i64 = 0
        let mut bits: i32 = 0
        let mut bit: i32 = 0
        while bit < k {
            if (mask & (1 << bit)) != 0 {
                bits = bits + 1
                shift = shift + w[bit]
            }
            bit = bit + 1
        }
        let budget: i64 = n - shift
        if budget >= 0 {
            let N: i64 = budget + (k as i64)
            let mut prod: i64 = 1
            let mut j: i32 = 0
            while j < k {
                let mut termn: i64 = (N - (j as i64)) % MOD
                if termn < 0 { termn = termn + MOD }
                prod = ((prod as i128) * (termn as i128) % (MOD as i128)) as i64
                j = j + 1
            }
            let term: i64 = ((prod as i128) * (invk as i128) % (MOD as i128)) as i64
            if (bits & 1) == 1 {
                ans = ans - term
            } else {
                ans = ans + term
            }
            ans = ans % MOD
            if ans < 0 { ans = ans + MOD }
        }
        mask = mask + 1
    }
    return ans
}

function main() -> i32 {
    let w: ptr<i64> = calloc(16, 8)
    if w == null { return 1 }
    let mut total: i64 = 0
    let mut k: i32 = 10
    while k <= 15 {
        let mut n: i64 = 1
        let mut i: i32 = 0
        while i < k {
            n = n * 10
            i = i + 1
        }
        total = (total + constrained(n, k, k as i64, w)) % MOD
        k = k + 1
    }
    printf("%lld\n", total)
    free(w)
    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_i64(int64_t base0, int64_t exp0, int64_t mod);
int64_t constrained_i64_i32_i64_ptr_i64(int64_t n, int32_t k, int64_t b, int64_t* w);
int32_t main(void);

static const int64_t MOD = 1000000007;



int64_t modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((base0), (mod));
    int64_t e = exp0;
    while (e > 0) {
        if ((e & 1) == 1) {
            r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
        }
        b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t constrained_i64_i32_i64_ptr_i64(int64_t n, int32_t k, int64_t b, int64_t* w) {
    int64_t pw = 1;
    int32_t i = 0;
    while (i < k) {
        pw = (pw * b);
        w[i] = (pw + 1);
        i = (i + 1);
    }
    int64_t fact = 1;
    i = 2;
    while (i <= k) {
        fact = FLOW_CHECKED_MOD(((fact * ((int64_t)(i)))), (MOD));
        i = (i + 1);
    }
    int64_t invk = modpow_i64_i64_i64(fact, (MOD - 2), MOD);
    int32_t m = FLOW_CHECKED_SHL((1), (k));
    int64_t ans = 0;
    int32_t mask = 0;
    while (mask < m) {
        int64_t shift = 0;
        int32_t bits = 0;
        int32_t bit = 0;
        while (bit < k) {
            if ((mask & FLOW_CHECKED_SHL((1), (bit))) != 0) {
                bits = (bits + 1);
                shift = (shift + w[bit]);
            }
            bit = (bit + 1);
        }
        int64_t budget = (n - shift);
        if (budget >= 0) {
            int64_t N = (budget + ((int64_t)(k)));
            int64_t prod = 1;
            int32_t j = 0;
            while (j < k) {
                int64_t termn = FLOW_CHECKED_MOD(((N - ((int64_t)(j)))), (MOD));
                if (termn < 0) {
                    termn = (termn + MOD);
                }
                prod = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(prod)) * ((__int128)(termn)))), (((__int128)(MOD))))));
                j = (j + 1);
            }
            int64_t term = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(prod)) * ((__int128)(invk)))), (((__int128)(MOD))))));
            if ((bits & 1) == 1) {
                ans = (ans - term);
            } else {
                ans = (ans + term);
            }
            ans = FLOW_CHECKED_MOD((ans), (MOD));
            if (ans < 0) {
                ans = (ans + MOD);
            }
        }
        mask = (mask + 1);
    }
    return ans;
}

int32_t main(void) {
    int64_t* w = (int64_t*)(calloc(16, 8));
    if (w == NULL) {
        return 1;
    }
    int64_t total = 0;
    int32_t k = 10;
    while (k <= 15) {
        int64_t n = 1;
        int32_t i = 0;
        while (i < k) {
            n = (n * 10);
            i = (i + 1);
        }
        total = FLOW_CHECKED_MOD(((total + constrained_i64_i32_i64_ptr_i64(n, k, ((int64_t)(k)), w))), (MOD));
        k = (k + 1);
    }
    printf("%lld\n", total);
    free(w);
    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(1000000007 : i64) : i64
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: 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 = arith.remsi %arg0, %arg2 : i64
    %5 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %4, %6 : i64, !llvm.ptr
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %8 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %9 = llvm.load %8 : !llvm.ptr -> i64
    %10 = arith.constant 0 : i32
    %12 = arith.extsi %10 : i32 to i64
    %11 = arith.cmpi sgt, %9, %12 : i64
    cf.cond_br %11, ^bb1, ^bb2
    ^bb1:
      %13 = llvm.load %8 : !llvm.ptr -> i64
      %14 = arith.constant 1 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.andi %13, %16 : i64
      %17 = arith.constant 1 : i32
      %19 = arith.extsi %17 : i32 to i64
      %18 = arith.cmpi eq, %15, %19 : i64
      cf.cond_br %18, ^bb3, ^bb4
      ^bb3:
        %20 = llvm.load %3 : !llvm.ptr -> i64
        %21 = arith.extsi %20 : i64 to i128
        %22 = llvm.load %6 : !llvm.ptr -> i64
        %23 = arith.extsi %22 : i64 to i128
        %25 = arith.trunci %21 : i128 to i64
        %26 = arith.trunci %23 : i128 to i64
        %24 = arith.muli %25, %26 : i64
        %27 = arith.extsi %arg2 : i64 to i128
        %29 = arith.trunci %27 : i128 to i64
        %28 = arith.remsi %24, %29 : i64
        llvm.store %28, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %30 = llvm.load %6 : !llvm.ptr -> i64
      %31 = arith.extsi %30 : i64 to i128
      %32 = llvm.load %6 : !llvm.ptr -> i64
      %33 = arith.extsi %32 : i64 to i128
      %35 = arith.trunci %31 : i128 to i64
      %36 = arith.trunci %33 : i128 to i64
      %34 = arith.muli %35, %36 : i64
      %37 = arith.extsi %arg2 : i64 to i128
      %39 = arith.trunci %37 : i128 to i64
      %38 = arith.remsi %34, %39 : i64
      llvm.store %38, %6 : i64, !llvm.ptr
      %40 = llvm.load %8 : !llvm.ptr -> i64
      %41 = arith.constant 2 : i32
      %43 = arith.extsi %41 : i32 to i64
      %42 = arith.divsi %40, %43 : i64
      llvm.store %42, %8 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %44 = llvm.load %3 : !llvm.ptr -> i64
    func.return %44 : i64
  }
  func.func @constrained(%arg0: i64, %arg1: i32, %arg2: i64, %arg3: !llvm.ptr) -> i64 {
    %45 = arith.constant 1 : i32
    %46 = arith.extsi %45 : i32 to i64
    %47 = llvm.mlir.constant(1 : i64) : i64
    %48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
    llvm.store %46, %48 : i64, !llvm.ptr
    %49 = arith.constant 0 : i32
    %50 = llvm.mlir.constant(1 : i64) : i64
    %51 = llvm.alloca %50 x i32 : (i64) -> !llvm.ptr
    llvm.store %49, %51 : i32, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %52 = llvm.load %51 : !llvm.ptr -> i32
    %53 = arith.cmpi slt, %52, %arg1 : i32
    cf.cond_br %53, ^bb7, ^bb8
    ^bb7:
      %54 = llvm.load %48 : !llvm.ptr -> i64
      %55 = arith.muli %54, %arg2 : i64
      llvm.store %55, %48 : i64, !llvm.ptr
      %56 = llvm.load %48 : !llvm.ptr -> i64
      %57 = arith.constant 1 : i32
      %59 = arith.extsi %57 : i32 to i64
      %58 = arith.addi %56, %59 : i64
      %60 = llvm.load %51 : !llvm.ptr -> i32
      %61 = arith.extsi %60 : i32 to i64
      %62 = llvm.getelementptr %arg3[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %58, %62 : i64, !llvm.ptr
      %63 = llvm.load %51 : !llvm.ptr -> i32
      %64 = arith.constant 1 : i32
      %65 = arith.addi %63, %64 : i32
      llvm.store %65, %51 : i32, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %66 = arith.constant 1 : i32
    %67 = arith.extsi %66 : i32 to i64
    %68 = llvm.mlir.constant(1 : i64) : i64
    %69 = llvm.alloca %68 x i64 : (i64) -> !llvm.ptr
    llvm.store %67, %69 : i64, !llvm.ptr
    %70 = arith.constant 2 : i32
    llvm.store %70, %51 : i32, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %71 = llvm.load %51 : !llvm.ptr -> i32
    %72 = arith.cmpi sle, %71, %arg1 : i32
    cf.cond_br %72, ^bb10, ^bb11
    ^bb10:
      %73 = llvm.load %69 : !llvm.ptr -> i64
      %74 = llvm.load %51 : !llvm.ptr -> i32
      %75 = arith.extsi %74 : i32 to i64
      %76 = arith.muli %73, %75 : i64
      %77 = llvm.mlir.addressof @MOD : !llvm.ptr
      %78 = llvm.load %77 : !llvm.ptr -> i64
      %79 = arith.remsi %76, %78 : i64
      llvm.store %79, %69 : i64, !llvm.ptr
      %80 = llvm.load %51 : !llvm.ptr -> i32
      %81 = arith.constant 1 : i32
      %82 = arith.addi %80, %81 : i32
      llvm.store %82, %51 : i32, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %84 = llvm.load %69 : !llvm.ptr -> i64
    %85 = llvm.mlir.addressof @MOD : !llvm.ptr
    %86 = llvm.load %85 : !llvm.ptr -> i64
    %87 = arith.constant 2 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.subi %86, %89 : i64
    %90 = llvm.mlir.addressof @MOD : !llvm.ptr
    %91 = llvm.load %90 : !llvm.ptr -> i64
    %83 = func.call @modpow(%84, %88, %91) : (i64, i64, i64) -> i64
    %92 = arith.constant 1 : i32
    %93 = arith.shli %92, %arg1 : i32
    %94 = arith.constant 0 : i32
    %95 = arith.extsi %94 : i32 to i64
    %96 = llvm.mlir.constant(1 : i64) : i64
    %97 = llvm.alloca %96 x i64 : (i64) -> !llvm.ptr
    llvm.store %95, %97 : i64, !llvm.ptr
    %98 = arith.constant 0 : i32
    %99 = llvm.mlir.constant(1 : i64) : i64
    %100 = llvm.alloca %99 x i32 : (i64) -> !llvm.ptr
    llvm.store %98, %100 : i32, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %101 = llvm.load %100 : !llvm.ptr -> i32
    %102 = arith.cmpi slt, %101, %93 : i32
    cf.cond_br %102, ^bb13, ^bb14
    ^bb13:
      %103 = arith.constant 0 : i32
      %104 = arith.extsi %103 : i32 to i64
      %105 = llvm.mlir.constant(1 : i64) : i64
      %106 = llvm.alloca %105 x i64 : (i64) -> !llvm.ptr
      llvm.store %104, %106 : i64, !llvm.ptr
      %107 = arith.constant 0 : i32
      %108 = llvm.mlir.constant(1 : i64) : i64
      %109 = llvm.alloca %108 x i32 : (i64) -> !llvm.ptr
      llvm.store %107, %109 : i32, !llvm.ptr
      %110 = arith.constant 0 : i32
      %111 = llvm.mlir.constant(1 : i64) : i64
      %112 = llvm.alloca %111 x i32 : (i64) -> !llvm.ptr
      llvm.store %110, %112 : i32, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %113 = llvm.load %112 : !llvm.ptr -> i32
      %114 = arith.cmpi slt, %113, %arg1 : i32
      cf.cond_br %114, ^bb16, ^bb17
      ^bb16:
        %115 = llvm.load %100 : !llvm.ptr -> i32
        %116 = arith.constant 1 : i32
        %117 = llvm.load %112 : !llvm.ptr -> i32
        %118 = arith.shli %116, %117 : i32
        %119 = arith.andi %115, %118 : i32
        %120 = arith.constant 0 : i32
        %121 = arith.cmpi ne, %119, %120 : i32
        cf.cond_br %121, ^bb18, ^bb19
        ^bb18:
          %122 = llvm.load %109 : !llvm.ptr -> i32
          %123 = arith.constant 1 : i32
          %124 = arith.addi %122, %123 : i32
          llvm.store %124, %109 : i32, !llvm.ptr
          %125 = llvm.load %106 : !llvm.ptr -> i64
          %127 = llvm.load %112 : !llvm.ptr -> i32
          %128 = arith.extsi %127 : i32 to i64
          %129 = llvm.getelementptr %arg3[%128] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %126 = llvm.load %129 : !llvm.ptr -> i64
          %130 = arith.addi %125, %126 : i64
          llvm.store %130, %106 : i64, !llvm.ptr
          cf.br ^bb20
        ^bb19:
          cf.br ^bb20
        ^bb20:
        %131 = llvm.load %112 : !llvm.ptr -> i32
        %132 = arith.constant 1 : i32
        %133 = arith.addi %131, %132 : i32
        llvm.store %133, %112 : i32, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %134 = llvm.load %106 : !llvm.ptr -> i64
      %135 = arith.subi %arg0, %134 : i64
      %136 = arith.constant 0 : i32
      %138 = arith.extsi %136 : i32 to i64
      %137 = arith.cmpi sge, %135, %138 : i64
      cf.cond_br %137, ^bb21, ^bb22
      ^bb21:
        %139 = arith.extsi %arg1 : i32 to i64
        %140 = arith.addi %135, %139 : i64
        %141 = arith.constant 1 : i32
        %142 = arith.extsi %141 : i32 to i64
        %143 = llvm.mlir.constant(1 : i64) : i64
        %144 = llvm.alloca %143 x i64 : (i64) -> !llvm.ptr
        llvm.store %142, %144 : i64, !llvm.ptr
        %145 = arith.constant 0 : i32
        %146 = llvm.mlir.constant(1 : i64) : i64
        %147 = llvm.alloca %146 x i32 : (i64) -> !llvm.ptr
        llvm.store %145, %147 : i32, !llvm.ptr
        cf.br ^bb24
        ^bb24:
        %148 = llvm.load %147 : !llvm.ptr -> i32
        %149 = arith.cmpi slt, %148, %arg1 : i32
        cf.cond_br %149, ^bb25, ^bb26
        ^bb25:
          %150 = llvm.load %147 : !llvm.ptr -> i32
          %151 = arith.extsi %150 : i32 to i64
          %152 = arith.subi %140, %151 : i64
          %153 = llvm.mlir.addressof @MOD : !llvm.ptr
          %154 = llvm.load %153 : !llvm.ptr -> i64
          %155 = arith.remsi %152, %154 : i64
          %156 = llvm.mlir.constant(1 : i64) : i64
          %157 = llvm.alloca %156 x i64 : (i64) -> !llvm.ptr
          llvm.store %155, %157 : i64, !llvm.ptr
          %158 = llvm.load %157 : !llvm.ptr -> i64
          %159 = arith.constant 0 : i32
          %161 = arith.extsi %159 : i32 to i64
          %160 = arith.cmpi slt, %158, %161 : i64
          cf.cond_br %160, ^bb27, ^bb28
          ^bb27:
            %162 = llvm.load %157 : !llvm.ptr -> i64
            %163 = llvm.mlir.addressof @MOD : !llvm.ptr
            %164 = llvm.load %163 : !llvm.ptr -> i64
            %165 = arith.addi %162, %164 : i64
            llvm.store %165, %157 : i64, !llvm.ptr
            cf.br ^bb29
          ^bb28:
            cf.br ^bb29
          ^bb29:
          %166 = llvm.load %144 : !llvm.ptr -> i64
          %167 = arith.extsi %166 : i64 to i128
          %168 = llvm.load %157 : !llvm.ptr -> i64
          %169 = arith.extsi %168 : i64 to i128
          %171 = arith.trunci %167 : i128 to i64
          %172 = arith.trunci %169 : i128 to i64
          %170 = arith.muli %171, %172 : i64
          %173 = llvm.mlir.addressof @MOD : !llvm.ptr
          %174 = llvm.load %173 : !llvm.ptr -> i64
          %175 = arith.extsi %174 : i64 to i128
          %177 = arith.trunci %175 : i128 to i64
          %176 = arith.remsi %170, %177 : i64
          llvm.store %176, %144 : i64, !llvm.ptr
          %178 = llvm.load %147 : !llvm.ptr -> i32
          %179 = arith.constant 1 : i32
          %180 = arith.addi %178, %179 : i32
          llvm.store %180, %147 : i32, !llvm.ptr
          cf.br ^bb24
        ^bb26:
        %181 = llvm.load %144 : !llvm.ptr -> i64
        %182 = arith.extsi %181 : i64 to i128
        %183 = arith.extsi %83 : i64 to i128
        %185 = arith.trunci %182 : i128 to i64
        %186 = arith.trunci %183 : i128 to i64
        %184 = arith.muli %185, %186 : i64
        %187 = llvm.mlir.addressof @MOD : !llvm.ptr
        %188 = llvm.load %187 : !llvm.ptr -> i64
        %189 = arith.extsi %188 : i64 to i128
        %191 = arith.trunci %189 : i128 to i64
        %190 = arith.remsi %184, %191 : i64
        %192 = llvm.load %109 : !llvm.ptr -> i32
        %193 = arith.constant 1 : i32
        %194 = arith.andi %192, %193 : i32
        %195 = arith.constant 1 : i32
        %196 = arith.cmpi eq, %194, %195 : i32
        cf.cond_br %196, ^bb30, ^bb31
        ^bb30:
          %197 = llvm.load %97 : !llvm.ptr -> i64
          %198 = arith.subi %197, %190 : i64
          llvm.store %198, %97 : i64, !llvm.ptr
          cf.br ^bb32
        ^bb31:
          %199 = llvm.load %97 : !llvm.ptr -> i64
          %200 = arith.addi %199, %190 : i64
          llvm.store %200, %97 : i64, !llvm.ptr
          cf.br ^bb32
        ^bb32:
        %201 = llvm.load %97 : !llvm.ptr -> i64
        %202 = llvm.mlir.addressof @MOD : !llvm.ptr
        %203 = llvm.load %202 : !llvm.ptr -> i64
        %204 = arith.remsi %201, %203 : i64
        llvm.store %204, %97 : i64, !llvm.ptr
        %205 = llvm.load %97 : !llvm.ptr -> i64
        %206 = arith.constant 0 : i32
        %208 = arith.extsi %206 : i32 to i64
        %207 = arith.cmpi slt, %205, %208 : i64
        cf.cond_br %207, ^bb33, ^bb34
        ^bb33:
          %209 = llvm.load %97 : !llvm.ptr -> i64
          %210 = llvm.mlir.addressof @MOD : !llvm.ptr
          %211 = llvm.load %210 : !llvm.ptr -> i64
          %212 = arith.addi %209, %211 : i64
          llvm.store %212, %97 : i64, !llvm.ptr
          cf.br ^bb35
        ^bb34:
          cf.br ^bb35
        ^bb35:
        cf.br ^bb23
      ^bb22:
        cf.br ^bb23
      ^bb23:
      %213 = llvm.load %100 : !llvm.ptr -> i32
      %214 = arith.constant 1 : i32
      %215 = arith.addi %213, %214 : i32
      llvm.store %215, %100 : i32, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %216 = llvm.load %97 : !llvm.ptr -> i64
    func.return %216 : i64
  }
  func.func @main() -> i32 {
    %218 = arith.constant 16 : i32
    %219 = arith.constant 8 : i32
    %220 = arith.extsi %218 : i32 to i64
    %221 = arith.extsi %219 : i32 to i64
    %217 = func.call @calloc(%220, %221) : (i64, i64) -> !llvm.ptr
    %222 = llvm.mlir.zero : !llvm.ptr
    %223 = llvm.icmp "eq" %217, %222 : !llvm.ptr
    cf.cond_br %223, ^bb36, ^bb37
    ^bb36:
      %224 = arith.constant 1 : i32
      func.return %224 : i32
    ^bb37:
      cf.br ^bb38
    ^bb38:
    %225 = arith.constant 0 : i32
    %226 = arith.extsi %225 : i32 to i64
    %227 = llvm.mlir.constant(1 : i64) : i64
    %228 = llvm.alloca %227 x i64 : (i64) -> !llvm.ptr
    llvm.store %226, %228 : i64, !llvm.ptr
    %229 = arith.constant 10 : i32
    %230 = llvm.mlir.constant(1 : i64) : i64
    %231 = llvm.alloca %230 x i32 : (i64) -> !llvm.ptr
    llvm.store %229, %231 : i32, !llvm.ptr
    cf.br ^bb39
    ^bb39:
    %232 = llvm.load %231 : !llvm.ptr -> i32
    %233 = arith.constant 15 : i32
    %234 = arith.cmpi sle, %232, %233 : i32
    cf.cond_br %234, ^bb40, ^bb41
    ^bb40:
      %235 = arith.constant 1 : i32
      %236 = arith.extsi %235 : i32 to i64
      %237 = llvm.mlir.constant(1 : i64) : i64
      %238 = llvm.alloca %237 x i64 : (i64) -> !llvm.ptr
      llvm.store %236, %238 : i64, !llvm.ptr
      %239 = arith.constant 0 : i32
      %240 = llvm.mlir.constant(1 : i64) : i64
      %241 = llvm.alloca %240 x i32 : (i64) -> !llvm.ptr
      llvm.store %239, %241 : i32, !llvm.ptr
      cf.br ^bb42
      ^bb42:
      %242 = llvm.load %241 : !llvm.ptr -> i32
      %243 = llvm.load %231 : !llvm.ptr -> i32
      %244 = arith.cmpi slt, %242, %243 : i32
      cf.cond_br %244, ^bb43, ^bb44
      ^bb43:
        %245 = llvm.load %238 : !llvm.ptr -> i64
        %246 = arith.constant 10 : i32
        %248 = arith.extsi %246 : i32 to i64
        %247 = arith.muli %245, %248 : i64
        llvm.store %247, %238 : i64, !llvm.ptr
        %249 = llvm.load %241 : !llvm.ptr -> i32
        %250 = arith.constant 1 : i32
        %251 = arith.addi %249, %250 : i32
        llvm.store %251, %241 : i32, !llvm.ptr
        cf.br ^bb42
      ^bb44:
      %252 = llvm.load %228 : !llvm.ptr -> i64
      %254 = llvm.load %238 : !llvm.ptr -> i64
      %255 = llvm.load %231 : !llvm.ptr -> i32
      %256 = llvm.load %231 : !llvm.ptr -> i32
      %257 = arith.extsi %256 : i32 to i64
      %253 = func.call @constrained(%254, %255, %257, %217) : (i64, i32, i64, !llvm.ptr) -> i64
      %258 = arith.addi %252, %253 : i64
      %259 = llvm.mlir.addressof @MOD : !llvm.ptr
      %260 = llvm.load %259 : !llvm.ptr -> i64
      %261 = arith.remsi %258, %260 : i64
      llvm.store %261, %228 : i64, !llvm.ptr
      %262 = llvm.load %231 : !llvm.ptr -> i32
      %263 = arith.constant 1 : i32
      %264 = arith.addi %262, %263 : i32
      llvm.store %264, %231 : i32, !llvm.ptr
      cf.br ^bb39
    ^bb41:
    %265 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %266 = llvm.load %228 : !llvm.ptr -> i64
    %267 = llvm.call @printf(%265, %266) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%217) : (!llvm.ptr) -> ()
    %269 = arith.constant 0 : i32
    func.return %269 : i32
  }
}