Problem 622

Riffle shuffles: sum of deck sizes with order dividing 60 exactly.

Answer3010983666182123972
Output3010983666182123972
StatusPASS
Native helperno
Runtime0 ms
Peak memory1152 KB
Time complexityO(n^3) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionSieve or enumeration
VerdictSuboptimal

Flow source

# Project Euler 622
# Riffle shuffles: sum of deck sizes with order dividing 60 exactly.

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

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 {
            let t: i128 = (r as i128) * (b as i128) % (mod as i128)
            r = t as i64
        }
        let t2: i128 = (b as i128) * (b as i128) % (mod as i128)
        b = t2 as i64
        e = e / 2
    }
    return r
}

function main() -> i32 {
    let target: i64 = (1 as i64 << 60) - 1
    # factor target and enumerate divisors
    let pf: ptr<i64> = calloc(64, 8)
    let pe: ptr<i64> = calloc(64, 8)
    let mut pn: i64 = 0
    let mut n: i64 = target
    let mut p: i64 = 2
    while p * p <= n {
        if n % p == 0 {
            let mut e: i64 = 0
            while n % p == 0 {
                n = n / p
                e = e + 1
            }
            pf[pn] = p
            pe[pn] = e
            pn = pn + 1
        }
        if p == 2 { p = 3 } else { p = p + 2 }
    }
    if n > 1 {
        pf[pn] = n
        pe[pn] = 1
        pn = pn + 1
    }
    # generate all divisors
    let mut dcount: i64 = 1
    let mut i: i64 = 0
    while i < pn {
        dcount = dcount * (pe[i] + 1)
        i = i + 1
    }
    let divs: ptr<i64> = calloc(dcount, 8)
    let mut nd: i64 = 1
    divs[0] = 1
    i = 0
    while i < pn {
        let prime: i64 = pf[i]
        let exp: i64 = pe[i]
        let prev: i64 = nd
        let mut e: i64 = 1
        let mut pp: i64 = prime
        while e <= exp {
            let mut j: i64 = 0
            while j < prev {
                divs[nd] = divs[j] * pp
                nd = nd + 1
                j = j + 1
            }
            pp = pp * prime
            e = e + 1
        }
        i = i + 1
    }
    # order primes of 60
    let op: ptr<i64> = calloc(8, 8)
    let mut on: i64 = 0
    let mut oo: i64 = 60
    p = 2
    while p * p <= oo {
        if oo % p == 0 {
            op[on] = p
            on = on + 1
            while oo % p == 0 { oo = oo / p }
        }
        if p == 2 { p = 3 } else { p = p + 2 }
    }
    if oo > 1 {
        op[on] = oo
        on = on + 1
    }
    let mut total: i64 = 0
    i = 0
    while i < nd {
        let d: i64 = divs[i]
        if d > 1 {
            let mut ok: i32 = 1
            let mut t: i64 = 0
            while t < on {
                if modpow(2, 60 / op[t], d) == 1 {
                    ok = 0
                    break
                }
                t = t + 1
            }
            if ok == 1 {
                total = total + d + 1
            }
        }
        i = i + 1
    }
    free(pf)
    free(pe)
    free(divs)
    free(op)
    printf("%lld\n", total)
    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);
int32_t main(void);



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) {
            __int128 t = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))));
            r = ((int64_t)(t));
        }
        __int128 t2 = FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))));
        b = ((int64_t)(t2));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

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