Problem 698

123 Numbers: F(111111111111222333) mod 123123123. 123-numbers: digits only 1,2,3, and each digit count is itself a 123-number. Enumerate by length, count valid strings, unrank within the target length. All counts fit in i64 (verified: L=38, max count ~8.5e16).

Answer57808202
Output57808202
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(n^2)
Space complexityO(1)O(n)
ApproachFlow solutionPolynomial interpolation
VerdictOptimal

Flow source

# Project Euler 698
# 123 Numbers: F(111111111111222333) mod 123123123.
# 123-numbers: digits only 1,2,3, and each digit count is itself a 123-number.
# Enumerate by length, count valid strings, unrank within the target length.
# All counts fit in i64 (verified: L=38, max count ~8.5e16).

const MOD: i64 = 123123123
const TARGET: i64 = 111111111111222333

# Allowed digit counts: 0,1,2,3,11,12,13,21,22,23,31,32,33
const N_ALLOWED: i64 = 13

function binom(n: i64, k: i64) -> i64 {
    if k < 0 { return 0 }
    if k > n { return 0 }
    if k == 0 { return 1 }
    if k > n - k { k = n - k }
    let mut result: i64 = 1
    let mut i: i64 = 1
    while i <= k {
        result = result * (n - k + i) / i
        i = i + 1
    }
    return result
}

# Count valid strings of length L with given prefix usage (u1,u2,u3).
# A string is valid if total counts (a,b,c) with a+b+c=L, each in allowed set.
function count_completions(L: i64, u1: i64, u2: i64, u3: i64) -> i64 {
    let used: i64 = u1 + u2 + u3
    let r: i64 = L - used
    let mut total: i64 = 0

    let allowed: array<i64, 13> = [0, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33]

    let mut ai: i64 = 0
    while ai < N_ALLOWED {
        let a: i64 = allowed[ai]
        if a < u1 {
            ai = ai + 1
        } else {
            let ra: i64 = a - u1
            if ra > r {
                ai = ai + 1
            } else {
                let mut bi: i64 = 0
                while bi < N_ALLOWED {
                    let b: i64 = allowed[bi]
                    if b < u2 {
                        bi = bi + 1
                    } else {
                        let rb: i64 = b - u2
                        if ra + rb > r {
                            bi = bi + 1
                        } else {
                            let rc: i64 = r - ra - rb
                            let c: i64 = u3 + rc
                            # Check if c is in allowed set
                            let mut ok: i32 = 0
                            let mut ci: i64 = 0
                            while ci < N_ALLOWED {
                                if allowed[ci] == c { ok = 1 }
                                ci = ci + 1
                            }
                            if ok == 0 {
                                bi = bi + 1
                            } else {
                                if a == 0 && b == 0 && c == 0 {
                                    bi = bi + 1
                                } else {
                                    let m: i64 = binom(r, ra) * binom(r - ra, rb)
                                    total = total + m
                                    bi = bi + 1
                                }
                            }
                        }
                    }
                }
                ai = ai + 1
            }
        }
    }
    return total
}

function main() -> i32 {
    let mut cumulative: i64 = 0
    let mut found_L: i64 = 0
    let mut rank_in_length: i64 = 0

    let mut L: i64 = 1
    while L < 5000 {
        let cnt_L: i64 = count_completions(L, 0, 0, 0)
        if cumulative + cnt_L >= TARGET {
            rank_in_length = TARGET - cumulative
            found_L = L
            L = 5000
        } else {
            cumulative = cumulative + cnt_L
            L = L + 1
        }
    }

    let mut u1: i64 = 0
    let mut u2: i64 = 0
    let mut u3: i64 = 0
    let mut answer: i64 = 0

    let mut pos: i64 = 0
    while pos < found_L {
        let mut digit: i64 = 1
        while digit <= 3 {
            let nu1: i64 = u1
            let nu2: i64 = u2
            let nu3: i64 = u3
            if digit == 1 {
                let nu1b: i64 = u1 + 1
                let cnt: i64 = count_completions(found_L, nu1b, nu2, nu3)
                if rank_in_length > cnt {
                    rank_in_length = rank_in_length - cnt
                    digit = digit + 1
                } else {
                    u1 = nu1b
                    answer = (answer * 10 + digit) % MOD
                    digit = 4
                }
            } else {
                if digit == 2 {
                    let nu2b: i64 = u2 + 1
                    let cnt: i64 = count_completions(found_L, nu1, nu2b, nu3)
                    if rank_in_length > cnt {
                        rank_in_length = rank_in_length - cnt
                        digit = digit + 1
                    } else {
                        u2 = nu2b
                        answer = (answer * 10 + digit) % MOD
                        digit = 4
                    }
                } else {
                    let nu3b: i64 = u3 + 1
                    let cnt: i64 = count_completions(found_L, nu1, nu2, nu3b)
                    if rank_in_length > cnt {
                        rank_in_length = rank_in_length - cnt
                        digit = digit + 1
                    } else {
                        u3 = nu3b
                        answer = (answer * 10 + digit) % MOD
                        digit = 4
                    }
                }
            }
        }
        pos = pos + 1
    }

    printf("%lld\n", answer)
    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 binom_i64_i64(int64_t n, int64_t k);
int64_t count_completions_i64_i64_i64_i64(int64_t L, int64_t u1, int64_t u2, int64_t u3);
int32_t main(void);

static const int64_t MOD = 123123123;
static const int64_t TARGET = 111111111111222333;
static const int64_t N_ALLOWED = 13;

int64_t binom_i64_i64(int64_t n, int64_t k) {
    if (k < 0) {
        return 0;
    }
    if (k > n) {
        return 0;
    }
    if (k == 0) {
        return 1;
    }
    if (k > (n - k)) {
        k = (n - k);
    }
    int64_t result = 1;
    int64_t i = 1;
    while (i <= k) {
        result = FLOW_CHECKED_DIV(((result * ((n - k) + i))), (i));
        i = (i + 1);
    }
    return result;
}

int64_t count_completions_i64_i64_i64_i64(int64_t L, int64_t u1, int64_t u2, int64_t u3) {
    int64_t used = ((u1 + u2) + u3);
    int64_t r = (L - used);
    int64_t total = 0;
    int64_t allowed[13] = { 0, 1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33 };
    int64_t ai = 0;
    while (ai < N_ALLOWED) {
        int64_t a = (((unsigned)(ai) < 13) ? allowed[ai] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ai), 13), flow_fault_handler("array index out of bounds"), allowed[0]));
        if (a < u1) {
            ai = (ai + 1);
        } else {
            int64_t ra = (a - u1);
            if (ra > r) {
                ai = (ai + 1);
            } else {
                int64_t bi = 0;
                while (bi < N_ALLOWED) {
                    int64_t b = (((unsigned)(bi) < 13) ? allowed[bi] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(bi), 13), flow_fault_handler("array index out of bounds"), allowed[0]));
                    if (b < u2) {
                        bi = (bi + 1);
                    } else {
                        int64_t rb = (b - u2);
                        if ((ra + rb) > r) {
                            bi = (bi + 1);
                        } else {
                            int64_t rc = ((r - ra) - rb);
                            int64_t c = (u3 + rc);
                            int32_t ok = 0;
                            int64_t ci = 0;
                            while (ci < N_ALLOWED) {
                                if ((((unsigned)(ci) < 13) ? allowed[ci] : (fprintf(stderr, "array index %d out of bounds (size %d)\n", (int)(ci), 13), flow_fault_handler("array index out of bounds"), allowed[0])) == c) {
                                    ok = 1;
                                }
                                ci = (ci + 1);
                            }
                            if (ok == 0) {
                                bi = (bi + 1);
                            } else {
                                if (((a == 0 && b == 0) && c == 0)) {
                                    bi = (bi + 1);
                                } else {
                                    int64_t m = (binom_i64_i64(r, ra) * binom_i64_i64((r - ra), rb));
                                    total = (total + m);
                                    bi = (bi + 1);
                                }
                            }
                        }
                    }
                }
                ai = (ai + 1);
            }
        }
    }
    return total;
}

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