Problem 330

(A(10^9)+B(10^9)) mod 77777777 via CRT over prime factors.

Answer15955822
Output15955822
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^2) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(sqrt(n))
Space complexityO(n^2)O(1)
ApproachFlow solutionTrial division or Pollard rho
VerdictSuboptimal

Flow source

# Project Euler 330
# (A(10^9)+B(10^9)) mod 77777777 via CRT over prime factors.

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

function modpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = ((base % mod) + mod) % mod
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 { r = (r * b) % mod }
        b = (b * b) % mod
        e = e / 2
    }
    return r
}

function comb_small(n: i64, k0: i64, p: i64) -> i64 {
    if k0 < 0 || k0 > n { return 0 }
    if k0 == 0 || k0 == n { return 1 }
    let mut k: i64 = k0
    if k > n - k { k = n - k }
    let mut num: i64 = 1
    let mut den: i64 = 1
    let mut i: i64 = 1
    while i <= k {
        num = (num * (n - (k - i))) % p
        den = (den * i) % p
        i = i + 1
    }
    return (num * modpow(den, p - 2, p)) % p
}

function s_mod_prime(n: i64, p: i64) -> i64 {
    let n0: i64 = n % p
    let inv2: i64 = modpow(2, p - 2, p)
    let r: ptr<i64> = calloc(p, 8)
    let r_exp: ptr<i64> = calloc(p, 8)
    let fact: ptr<i64> = calloc(n0 + 1, 8)
    let mut inv2_pow: i64 = 1
    let mut prev: i64 = 1
    let mut i: i64 = 1
    while i < p {
        inv2_pow = (inv2_pow * inv2) % p
        r[i] = (inv2_pow - prev + p) % p
        prev = inv2_pow
        i = i + 1
    }
    let mut e: i64 = 0
    while e < p - 1 {
        let mut s: i64 = 0
        i = 1
        while i < p {
            s = (s + r[i] * modpow(i, e, p)) % p
            i = i + 1
        }
        r_exp[e] = s
        e = e + 1
    }
    fact[0] = 1
    i = 1
    while i <= n0 {
        fact[i] = (fact[i - 1] * i) % p
        i = i + 1
    }
    let mut res: i64 = 0
    let mut m: i64 = 0
    while m <= n0 {
        let c_nm: i64 = comb_small(n0, m, p)
        let ee: i64 = (n - m) % (p - 1)
        res = (res + fact[m] * c_nm % p * r_exp[ee]) % p
        m = m + 1
    }
    free(r); free(r_exp); free(fact)
    return res
}

function main() -> i32 {
    let n: i64 = 1000000000
    let residues: ptr<i64> = calloc(5, 8)
    let mods: ptr<i64> = calloc(5, 8)
    mods[0] = 7; mods[1] = 11; mods[2] = 73; mods[3] = 101; mods[4] = 137
    let mut i: i64 = 0
    while i < 5 {
        residues[i] = s_mod_prime(n, mods[i])
        i = i + 1
    }
    let mut x: i64 = 0
    let mut m: i64 = 1
    i = 0
    while i < 5 {
        let mi: i64 = mods[i]
        let ai: i64 = residues[i]
        let t: i64 = ((ai - x) % mi + mi) % mi * modpow(m % mi, mi - 2, mi) % mi
        x = x + m * t
        m = m * mi
        i = i + 1
    }
    printf("%lld\n", x % m)
    free(residues); free(mods)
    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 base, int64_t exp, int64_t mod);
int64_t comb_small_i64_i64_i64(int64_t n, int64_t k0, int64_t p);
int64_t s_mod_prime_i64_i64(int64_t n, int64_t p);
int32_t main(void);



int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((base), (mod)) + mod)), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            r = FLOW_CHECKED_MOD(((r * b)), (mod));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (mod));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t comb_small_i64_i64_i64(int64_t n, int64_t k0, int64_t p) {
    if ((k0 < 0 || k0 > n)) {
        return 0;
    }
    if ((k0 == 0 || k0 == n)) {
        return 1;
    }
    int64_t k = k0;
    if (k > (n - k)) {
        k = (n - k);
    }
    int64_t num = 1;
    int64_t den = 1;
    int64_t i = 1;
    while (i <= k) {
        num = FLOW_CHECKED_MOD(((num * (n - (k - i)))), (p));
        den = FLOW_CHECKED_MOD(((den * i)), (p));
        i = (i + 1);
    }
    return FLOW_CHECKED_MOD(((num * modpow_i64_i64_i64(den, (p - 2), p))), (p));
}

int64_t s_mod_prime_i64_i64(int64_t n, int64_t p) {
    int64_t n0 = FLOW_CHECKED_MOD((n), (p));
    int64_t inv2 = modpow_i64_i64_i64(2, (p - 2), p);
    int64_t* r = (int64_t*)(calloc(p, 8));
    int64_t* r_exp = (int64_t*)(calloc(p, 8));
    int64_t* fact = (int64_t*)(calloc((n0 + 1), 8));
    int64_t inv2_pow = 1;
    int64_t prev = 1;
    int64_t i = 1;
    while (i < p) {
        inv2_pow = FLOW_CHECKED_MOD(((inv2_pow * inv2)), (p));
        r[i] = FLOW_CHECKED_MOD((((inv2_pow - prev) + p)), (p));
        prev = inv2_pow;
        i = (i + 1);
    }
    int64_t e = 0;
    while (e < (p - 1)) {
        int64_t s = 0;
        i = 1;
        while (i < p) {
            s = FLOW_CHECKED_MOD(((s + (r[i] * modpow_i64_i64_i64(i, e, p)))), (p));
            i = (i + 1);
        }
        r_exp[e] = s;
        e = (e + 1);
    }
    fact[0] = 1;
    i = 1;
    while (i <= n0) {
        fact[i] = FLOW_CHECKED_MOD(((fact[(i - 1)] * i)), (p));
        i = (i + 1);
    }
    int64_t res = 0;
    int64_t m = 0;
    while (m <= n0) {
        int64_t c_nm = comb_small_i64_i64_i64(n0, m, p);
        int64_t ee = FLOW_CHECKED_MOD(((n - m)), ((p - 1)));
        res = FLOW_CHECKED_MOD(((res + (FLOW_CHECKED_MOD(((fact[m] * c_nm)), (p)) * r_exp[ee]))), (p));
        m = (m + 1);
    }
    free(r);
    free(r_exp);
    free(fact);
    return res;
}

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