Problem 409

W(10^7) mod 10^9+7: winning nim positions with distinct pile sizes.

Answer253223948
Output253223948
StatusPASS
Native helperno
Runtime410 ms
Peak memory40192 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log n)
Space complexityO(n)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictOptimal

Flow source

# Project Euler 409
# W(10^7) mod 10^9+7: winning nim positions with distinct pile sizes.

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 % 2 == 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 N: i64 = 10000000
    let MOD: i64 = 1000000007
    let q: i64 = modpow(2, N, MOD)
    let q_minus_1: i64 = q - 1
    if q_minus_1 < 0 { q_minus_1 = q_minus_1 + MOD }

    let mut total: i64 = 1
    let mut fact: i64 = 1
    let mut term: i64 = q_minus_1
    let mut i: i64 = 1
    while i <= N {
        let t: i128 = (total as i128) * (term as i128) % (MOD as i128)
        total = t as i64
        term = term - 1
        if term < 0 { term = term + MOD }
        let t2: i128 = (fact as i128) * (i as i128) % (MOD as i128)
        fact = t2 as i64
        i = i + 1
    }
    let inv_fact: i64 = modpow(fact, MOD - 2, MOD)
    let comb: i64 = ((total as i128) * (inv_fact as i128) % (MOD as i128)) as i64

    let m: i64 = N / 2
    let mut s: i64 = 1
    if m > 0 {
        let inv: ptr<i64> = calloc(m + 1, 8)
        if inv == null { return 1 }
        inv[1] = 1
        i = 2
        while i <= m {
            inv[i] = MOD - ((MOD / i) * inv[MOD % i]) % MOD
            i = i + 1
        }
        let n_half: i64 = modpow(2, N - 1, MOD)
        let mut binom: i64 = 1
        let mut r: i64 = 1
        while r <= m {
            let tr: i64 = (n_half - r + 1) % MOD
            if tr < 0 { tr = tr + MOD }
            binom = ((binom as i128) * (tr as i128) % (MOD as i128)) as i64
            binom = ((binom as i128) * (inv[r] as i128) % (MOD as i128)) as i64
            if r % 2 == 1 {
                s = s - binom
            } else {
                s = s + binom
            }
            s = s % MOD
            if s < 0 { s = s + MOD }
            r = r + 1
        }
        free(inv)
    }

    let mut e_n: i64 = s
    if N % 2 == 1 {
        e_n = (0 - s) % MOD
        if e_n < 0 { e_n = e_n + MOD }
    }

    let inv_q: i64 = modpow(q, MOD - 2, MOD)
    let mut losing: i64 = ((fact as i128) * (inv_q as i128) % (MOD as i128)) as i64
    let inner: i64 = (comb + ((q_minus_1 as i128) * (e_n as i128) % (MOD as i128)) as i64) % MOD
    losing = ((losing as i128) * (inner as i128) % (MOD as i128)) as i64
    let ans: i64 = (total - losing) % MOD
    if ans < 0 { ans = ans + MOD }
    printf("%lld\n", ans)
    return 0
}

Generated C

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Flow runtime helpers */
typedef struct flow_temp_node { struct flow_temp_node* next; } flow_temp_node;
static flow_temp_node* flow_temp_head = NULL;
static int flow_temp_atexit_set = 0;
__attribute__((unused)) static void flow_temp_free_all(void) {
    while (flow_temp_head) {
        flow_temp_node* n = flow_temp_head;
        flow_temp_head = n->next;
        free(n);
    }
}
__attribute__((unused)) static void* flow_temp_alloc(size_t nbytes) {
    flow_temp_node* node = (flow_temp_node*)malloc(sizeof(flow_temp_node) + nbytes);
    if (!node) return NULL;
    node->next = flow_temp_head;
    flow_temp_head = node;
    if (!flow_temp_atexit_set) {
        flow_temp_atexit_set = 1;
        atexit(flow_temp_free_all);
    }
    return (void*)(node + 1);
}
#ifndef FLOW_DIAG
#define FLOW_DIAG(msg) fprintf(stderr, "%s", (msg))
#endif
#ifndef FLOW_LOG
#define FLOW_LOG(fmt, ...) printf(fmt, __VA_ARGS__)
#endif
#ifndef FLOW_LOG_EMPTY
#define FLOW_LOG_EMPTY(fmt) printf(fmt)
#endif
static char* flow_strcat(const char* a, const char* b) {
    size_t la = strlen(a ? a : ""), lb = strlen(b ? b : "");
    char* r = (char*)flow_temp_alloc(la + lb + 1);
    if (!r) return NULL;
    if (la) memcpy(r, a, la);
    if (lb) memcpy(r + la, b, lb);
    r[la + lb] = '\0';
    return r;
}

#define __flow_in_arr(arr, val) __extension__ ({ \
    int _found = 0; \
    size_t _n = sizeof(arr)/sizeof((arr)[0]); \
    for (size_t _i = 0; _i < _n; _i++) { \
        if ((arr)[_i] == (val)) { _found = 1; break; } \
    } _found; })

/* Unified fault handler (MISRA #279) — override with -DFLOW_FAULT_HANDLER=fn */
#ifndef FLOW_FAULT_HANDLER
__attribute__((unused)) static inline void flow_fault_handler(const char* msg) {
    fprintf(stderr, "flow: %s\n", msg ? msg : "fault");
    abort();
#if defined(__GNUC__) || defined(__clang__)
    __builtin_unreachable();
#endif
}
#else
#define flow_fault_handler FLOW_FAULT_HANDLER
#endif
#define flow_div_by_zero_handler() flow_fault_handler("division by zero")
#define flow_shift_ub_handler() flow_fault_handler("invalid shift (amount out of range or left-shift of negative)")

#ifndef FLOW_CHECKED_DIV
#define FLOW_CHECKED_DIV(L, R) (((R) != 0) ? ((L) / (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_MOD
#define FLOW_CHECKED_MOD(L, R) (((R) != 0) ? ((L) % (R)) : (flow_div_by_zero_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHL
#define FLOW_CHECKED_SHL(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull)) && ((L) >= 0)) ? ((L) << (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif
#ifndef FLOW_CHECKED_SHR
#define FLOW_CHECKED_SHR(L, R) ((((R) >= 0) && ((unsigned long long)(R) < (sizeof(L) * 8ull))) ? ((L) >> (R)) : (flow_shift_ub_handler(), (L) * 0))
#endif

#include <math.h>

void* _ui_state = NULL;

static inline float i32_to_f32(int32_t v) { return (float)v; }

/* Host stub for @gpu kernels (device codegen replaces this). */
static inline int32_t gpu_thread_id(void) { return 0; }

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 (FLOW_CHECKED_MOD((e), (2)) == 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 N = 10000000;
    int64_t MOD = 1000000007;
    int64_t q = modpow_i64_i64_i64(2, N, MOD);
    int64_t q_minus_1 = (q - 1);
    if (q_minus_1 < 0) {
        q_minus_1 = (q_minus_1 + MOD);
    }
    int64_t total = 1;
    int64_t fact = 1;
    int64_t term = q_minus_1;
    int64_t i = 1;
    while (i <= N) {
        __int128 t = FLOW_CHECKED_MOD(((((__int128)(total)) * ((__int128)(term)))), (((__int128)(MOD))));
        total = ((int64_t)(t));
        term = (term - 1);
        if (term < 0) {
            term = (term + MOD);
        }
        __int128 t2 = FLOW_CHECKED_MOD(((((__int128)(fact)) * ((__int128)(i)))), (((__int128)(MOD))));
        fact = ((int64_t)(t2));
        i = (i + 1);
    }
    int64_t inv_fact = modpow_i64_i64_i64(fact, (MOD - 2), MOD);
    int64_t comb = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(total)) * ((__int128)(inv_fact)))), (((__int128)(MOD))))));
    int64_t m = FLOW_CHECKED_DIV((N), (2));
    int64_t s = 1;
    if (m > 0) {
        int64_t* inv = (int64_t*)(calloc((m + 1), 8));
        if (inv == NULL) {
            return 1;
        }
        inv[1] = 1;
        i = 2;
        while (i <= m) {
            inv[i] = (MOD - FLOW_CHECKED_MOD(((FLOW_CHECKED_DIV((MOD), (i)) * inv[FLOW_CHECKED_MOD((MOD), (i))])), (MOD)));
            i = (i + 1);
        }
        int64_t n_half = modpow_i64_i64_i64(2, (N - 1), MOD);
        int64_t binom = 1;
        int64_t r = 1;
        while (r <= m) {
            int64_t tr = FLOW_CHECKED_MOD((((n_half - r) + 1)), (MOD));
            if (tr < 0) {
                tr = (tr + MOD);
            }
            binom = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(binom)) * ((__int128)(tr)))), (((__int128)(MOD))))));
            binom = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(binom)) * ((__int128)(inv[r])))), (((__int128)(MOD))))));
            if (FLOW_CHECKED_MOD((r), (2)) == 1) {
                s = (s - binom);
            } else {
                s = (s + binom);
            }
            s = FLOW_CHECKED_MOD((s), (MOD));
            if (s < 0) {
                s = (s + MOD);
            }
            r = (r + 1);
        }
        free(inv);
    }
    int64_t e_n = s;
    if (FLOW_CHECKED_MOD((N), (2)) == 1) {
        e_n = FLOW_CHECKED_MOD(((0 - s)), (MOD));
        if (e_n < 0) {
            e_n = (e_n + MOD);
        }
    }
    int64_t inv_q = modpow_i64_i64_i64(q, (MOD - 2), MOD);
    int64_t losing = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(fact)) * ((__int128)(inv_q)))), (((__int128)(MOD))))));
    int64_t inner = FLOW_CHECKED_MOD(((comb + ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(q_minus_1)) * ((__int128)(e_n)))), (((__int128)(MOD)))))))), (MOD));
    losing = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(losing)) * ((__int128)(inner)))), (((__int128)(MOD))))));
    int64_t ans = FLOW_CHECKED_MOD(((total - losing)), (MOD));
    if (ans < 0) {
        ans = (ans + MOD);
    }
    printf("%lld\n", ans);
    return 0;
}

Generated MLIR

module {
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.mlir.global internal constant @str_0("%lld\n\00") {addr_space = 0 : i32} : !llvm.array<6 x i8>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @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 2 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.remsi %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 10000000 : i32
    %50 = arith.extsi %49 : i32 to i64
    %51 = arith.constant 1000000007 : i32
    %52 = arith.extsi %51 : i32 to i64
    %54 = arith.constant 2 : i32
    %55 = arith.extsi %54 : i32 to i64
    %53 = func.call @modpow(%55, %50, %52) : (i64, i64, i64) -> i64
    %56 = arith.constant 1 : i32
    %58 = arith.extsi %56 : i32 to i64
    %57 = arith.subi %53, %58 : i64
    %59 = arith.constant 0 : i32
    %61 = arith.extsi %59 : i32 to i64
    %60 = arith.cmpi slt, %57, %61 : i64
    %62 = scf.if %60 -> (i64) {
      %63 = arith.addi %57, %52 : i64
      scf.yield %63 : i64
    } else {
      scf.yield %57 : i64
    }
    %64 = arith.constant 1 : i32
    %65 = arith.extsi %64 : i32 to i64
    %66 = llvm.mlir.constant(1 : i64) : i64
    %67 = llvm.alloca %66 x i64 : (i64) -> !llvm.ptr
    llvm.store %65, %67 : i64, !llvm.ptr
    %68 = arith.constant 1 : i32
    %69 = arith.extsi %68 : i32 to i64
    %70 = llvm.mlir.constant(1 : i64) : i64
    %71 = llvm.alloca %70 x i64 : (i64) -> !llvm.ptr
    llvm.store %69, %71 : i64, !llvm.ptr
    %72 = llvm.mlir.constant(1 : i64) : i64
    %73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
    llvm.store %62, %73 : i64, !llvm.ptr
    %74 = arith.constant 1 : i32
    %75 = arith.extsi %74 : i32 to i64
    %76 = llvm.mlir.constant(1 : i64) : i64
    %77 = llvm.alloca %76 x i64 : (i64) -> !llvm.ptr
    llvm.store %75, %77 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %78 = llvm.load %77 : !llvm.ptr -> i64
    %79 = arith.cmpi sle, %78, %50 : i64
    cf.cond_br %79, ^bb7, ^bb8
    ^bb7:
      %80 = llvm.load %67 : !llvm.ptr -> i64
      %81 = arith.extsi %80 : i64 to i128
      %82 = llvm.load %73 : !llvm.ptr -> i64
      %83 = arith.extsi %82 : i64 to i128
      %85 = arith.trunci %81 : i128 to i64
      %86 = arith.trunci %83 : i128 to i64
      %84 = arith.muli %85, %86 : i64
      %87 = arith.extsi %52 : i64 to i128
      %89 = arith.trunci %87 : i128 to i64
      %88 = arith.remsi %84, %89 : i64
      %90 = arith.extsi %88 : i64 to i128
      %91 = arith.trunci %90 : i128 to i64
      llvm.store %91, %67 : i64, !llvm.ptr
      %92 = llvm.load %73 : !llvm.ptr -> i64
      %93 = arith.constant 1 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.subi %92, %95 : i64
      llvm.store %94, %73 : i64, !llvm.ptr
      %96 = llvm.load %73 : !llvm.ptr -> i64
      %97 = arith.constant 0 : i32
      %99 = arith.extsi %97 : i32 to i64
      %98 = arith.cmpi slt, %96, %99 : i64
      cf.cond_br %98, ^bb9, ^bb10
      ^bb9:
        %100 = llvm.load %73 : !llvm.ptr -> i64
        %101 = arith.addi %100, %52 : i64
        llvm.store %101, %73 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %102 = llvm.load %71 : !llvm.ptr -> i64
      %103 = arith.extsi %102 : i64 to i128
      %104 = llvm.load %77 : !llvm.ptr -> i64
      %105 = arith.extsi %104 : i64 to i128
      %107 = arith.trunci %103 : i128 to i64
      %108 = arith.trunci %105 : i128 to i64
      %106 = arith.muli %107, %108 : i64
      %109 = arith.extsi %52 : i64 to i128
      %111 = arith.trunci %109 : i128 to i64
      %110 = arith.remsi %106, %111 : i64
      %112 = arith.extsi %110 : i64 to i128
      %113 = arith.trunci %112 : i128 to i64
      llvm.store %113, %71 : i64, !llvm.ptr
      %114 = llvm.load %77 : !llvm.ptr -> i64
      %115 = arith.constant 1 : i32
      %117 = arith.extsi %115 : i32 to i64
      %116 = arith.addi %114, %117 : i64
      llvm.store %116, %77 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %119 = llvm.load %71 : !llvm.ptr -> i64
    %120 = arith.constant 2 : i32
    %122 = arith.extsi %120 : i32 to i64
    %121 = arith.subi %52, %122 : i64
    %118 = func.call @modpow(%119, %121, %52) : (i64, i64, i64) -> i64
    %123 = llvm.load %67 : !llvm.ptr -> i64
    %124 = arith.extsi %123 : i64 to i128
    %125 = arith.extsi %118 : i64 to i128
    %127 = arith.trunci %124 : i128 to i64
    %128 = arith.trunci %125 : i128 to i64
    %126 = arith.muli %127, %128 : i64
    %129 = arith.extsi %52 : i64 to i128
    %131 = arith.trunci %129 : i128 to i64
    %130 = arith.remsi %126, %131 : i64
    %132 = arith.constant 2 : i32
    %134 = arith.extsi %132 : i32 to i64
    %133 = arith.divsi %50, %134 : i64
    %135 = arith.constant 1 : i32
    %136 = arith.extsi %135 : i32 to i64
    %137 = llvm.mlir.constant(1 : i64) : i64
    %138 = llvm.alloca %137 x i64 : (i64) -> !llvm.ptr
    llvm.store %136, %138 : i64, !llvm.ptr
    %139 = arith.constant 0 : i32
    %141 = arith.extsi %139 : i32 to i64
    %140 = arith.cmpi sgt, %133, %141 : i64
    cf.cond_br %140, ^bb12, ^bb13
    ^bb12:
      %143 = arith.constant 1 : i32
      %145 = arith.extsi %143 : i32 to i64
      %144 = arith.addi %133, %145 : i64
      %146 = arith.constant 8 : i32
      %147 = arith.extsi %146 : i32 to i64
      %142 = func.call @calloc(%144, %147) : (i64, i64) -> !llvm.ptr
      %148 = llvm.mlir.zero : !llvm.ptr
      %149 = llvm.icmp "eq" %142, %148 : !llvm.ptr
      cf.cond_br %149, ^bb15, ^bb16
      ^bb15:
        %150 = arith.constant 1 : i32
        func.return %150 : i32
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %151 = arith.constant 1 : i32
      %152 = arith.constant 1 : i32
      %153 = arith.extsi %151 : i32 to i64
      %154 = arith.extsi %152 : i32 to i64
      %155 = llvm.getelementptr %142[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %153, %155 : i64, !llvm.ptr
      %156 = arith.constant 2 : i32
      %157 = arith.extsi %156 : i32 to i64
      llvm.store %157, %77 : i64, !llvm.ptr
      cf.br ^bb18
      ^bb18:
      %158 = llvm.load %77 : !llvm.ptr -> i64
      %159 = arith.cmpi sle, %158, %133 : i64
      cf.cond_br %159, ^bb19, ^bb20
      ^bb19:
        %160 = llvm.load %77 : !llvm.ptr -> i64
        %161 = arith.divsi %52, %160 : i64
        %163 = llvm.load %77 : !llvm.ptr -> i64
        %164 = arith.remsi %52, %163 : i64
        %165 = llvm.getelementptr %142[%164] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %162 = llvm.load %165 : !llvm.ptr -> i64
        %166 = arith.muli %161, %162 : i64
        %167 = arith.remsi %166, %52 : i64
        %168 = arith.subi %52, %167 : i64
        %169 = llvm.load %77 : !llvm.ptr -> i64
        %170 = llvm.getelementptr %142[%169] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %168, %170 : i64, !llvm.ptr
        %171 = llvm.load %77 : !llvm.ptr -> i64
        %172 = arith.constant 1 : i32
        %174 = arith.extsi %172 : i32 to i64
        %173 = arith.addi %171, %174 : i64
        llvm.store %173, %77 : i64, !llvm.ptr
        cf.br ^bb18
      ^bb20:
      %176 = arith.constant 2 : i32
      %177 = arith.constant 1 : i32
      %179 = arith.extsi %177 : i32 to i64
      %178 = arith.subi %50, %179 : i64
      %180 = arith.extsi %176 : i32 to i64
      %175 = func.call @modpow(%180, %178, %52) : (i64, i64, i64) -> i64
      %181 = arith.constant 1 : i32
      %182 = arith.extsi %181 : i32 to i64
      %183 = llvm.mlir.constant(1 : i64) : i64
      %184 = llvm.alloca %183 x i64 : (i64) -> !llvm.ptr
      llvm.store %182, %184 : i64, !llvm.ptr
      %185 = arith.constant 1 : i32
      %186 = arith.extsi %185 : i32 to i64
      %187 = llvm.mlir.constant(1 : i64) : i64
      %188 = llvm.alloca %187 x i64 : (i64) -> !llvm.ptr
      llvm.store %186, %188 : i64, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %189 = llvm.load %188 : !llvm.ptr -> i64
      %190 = arith.cmpi sle, %189, %133 : i64
      cf.cond_br %190, ^bb22, ^bb23
      ^bb22:
        %191 = llvm.load %188 : !llvm.ptr -> i64
        %192 = arith.subi %175, %191 : i64
        %193 = arith.constant 1 : i32
        %195 = arith.extsi %193 : i32 to i64
        %194 = arith.addi %192, %195 : i64
        %196 = arith.remsi %194, %52 : i64
        %197 = arith.constant 0 : i32
        %199 = arith.extsi %197 : i32 to i64
        %198 = arith.cmpi slt, %196, %199 : i64
        %200 = scf.if %198 -> (i64) {
          %201 = arith.addi %196, %52 : i64
          scf.yield %201 : i64
        } else {
          scf.yield %196 : i64
        }
        %202 = llvm.load %184 : !llvm.ptr -> i64
        %203 = arith.extsi %202 : i64 to i128
        %204 = arith.extsi %200 : i64 to i128
        %206 = arith.trunci %203 : i128 to i64
        %207 = arith.trunci %204 : i128 to i64
        %205 = arith.muli %206, %207 : i64
        %208 = arith.extsi %52 : i64 to i128
        %210 = arith.trunci %208 : i128 to i64
        %209 = arith.remsi %205, %210 : i64
        llvm.store %209, %184 : i64, !llvm.ptr
        %211 = llvm.load %184 : !llvm.ptr -> i64
        %212 = arith.extsi %211 : i64 to i128
        %214 = llvm.load %188 : !llvm.ptr -> i64
        %215 = llvm.getelementptr %142[%214] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %213 = llvm.load %215 : !llvm.ptr -> i64
        %216 = arith.extsi %213 : i64 to i128
        %218 = arith.trunci %212 : i128 to i64
        %219 = arith.trunci %216 : i128 to i64
        %217 = arith.muli %218, %219 : i64
        %220 = arith.extsi %52 : i64 to i128
        %222 = arith.trunci %220 : i128 to i64
        %221 = arith.remsi %217, %222 : i64
        llvm.store %221, %184 : i64, !llvm.ptr
        %223 = llvm.load %188 : !llvm.ptr -> i64
        %224 = arith.constant 2 : i32
        %226 = arith.extsi %224 : i32 to i64
        %225 = arith.remsi %223, %226 : i64
        %227 = arith.constant 1 : i32
        %229 = arith.extsi %227 : i32 to i64
        %228 = arith.cmpi eq, %225, %229 : i64
        cf.cond_br %228, ^bb24, ^bb25
        ^bb24:
          %230 = llvm.load %138 : !llvm.ptr -> i64
          %231 = llvm.load %184 : !llvm.ptr -> i64
          %232 = arith.subi %230, %231 : i64
          llvm.store %232, %138 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          %233 = llvm.load %138 : !llvm.ptr -> i64
          %234 = llvm.load %184 : !llvm.ptr -> i64
          %235 = arith.addi %233, %234 : i64
          llvm.store %235, %138 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb26:
        %236 = llvm.load %138 : !llvm.ptr -> i64
        %237 = arith.remsi %236, %52 : i64
        llvm.store %237, %138 : i64, !llvm.ptr
        %238 = llvm.load %138 : !llvm.ptr -> i64
        %239 = arith.constant 0 : i32
        %241 = arith.extsi %239 : i32 to i64
        %240 = arith.cmpi slt, %238, %241 : i64
        cf.cond_br %240, ^bb27, ^bb28
        ^bb27:
          %242 = llvm.load %138 : !llvm.ptr -> i64
          %243 = arith.addi %242, %52 : i64
          llvm.store %243, %138 : i64, !llvm.ptr
          cf.br ^bb29
        ^bb28:
          cf.br ^bb29
        ^bb29:
        %244 = llvm.load %188 : !llvm.ptr -> i64
        %245 = arith.constant 1 : i32
        %247 = arith.extsi %245 : i32 to i64
        %246 = arith.addi %244, %247 : i64
        llvm.store %246, %188 : i64, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      func.call @free(%142) : (!llvm.ptr) -> ()
      cf.br ^bb14
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %249 = llvm.load %138 : !llvm.ptr -> i64
    %250 = llvm.mlir.constant(1 : i64) : i64
    %251 = llvm.alloca %250 x i64 : (i64) -> !llvm.ptr
    llvm.store %249, %251 : i64, !llvm.ptr
    %252 = arith.constant 2 : i32
    %254 = arith.extsi %252 : i32 to i64
    %253 = arith.remsi %50, %254 : i64
    %255 = arith.constant 1 : i32
    %257 = arith.extsi %255 : i32 to i64
    %256 = arith.cmpi eq, %253, %257 : i64
    cf.cond_br %256, ^bb30, ^bb31
    ^bb30:
      %258 = arith.constant 0 : i32
      %259 = llvm.load %138 : !llvm.ptr -> i64
      %261 = arith.extsi %258 : i32 to i64
      %260 = arith.subi %261, %259 : i64
      %262 = arith.remsi %260, %52 : i64
      llvm.store %262, %251 : i64, !llvm.ptr
      %263 = llvm.load %251 : !llvm.ptr -> i64
      %264 = arith.constant 0 : i32
      %266 = arith.extsi %264 : i32 to i64
      %265 = arith.cmpi slt, %263, %266 : i64
      cf.cond_br %265, ^bb33, ^bb34
      ^bb33:
        %267 = llvm.load %251 : !llvm.ptr -> i64
        %268 = arith.addi %267, %52 : i64
        llvm.store %268, %251 : i64, !llvm.ptr
        cf.br ^bb35
      ^bb34:
        cf.br ^bb35
      ^bb35:
      cf.br ^bb32
    ^bb31:
      cf.br ^bb32
    ^bb32:
    %270 = arith.constant 2 : i32
    %272 = arith.extsi %270 : i32 to i64
    %271 = arith.subi %52, %272 : i64
    %269 = func.call @modpow(%53, %271, %52) : (i64, i64, i64) -> i64
    %273 = llvm.load %71 : !llvm.ptr -> i64
    %274 = arith.extsi %273 : i64 to i128
    %275 = arith.extsi %269 : i64 to i128
    %277 = arith.trunci %274 : i128 to i64
    %278 = arith.trunci %275 : i128 to i64
    %276 = arith.muli %277, %278 : i64
    %279 = arith.extsi %52 : i64 to i128
    %281 = arith.trunci %279 : i128 to i64
    %280 = arith.remsi %276, %281 : i64
    %282 = llvm.mlir.constant(1 : i64) : i64
    %283 = llvm.alloca %282 x i64 : (i64) -> !llvm.ptr
    llvm.store %280, %283 : i64, !llvm.ptr
    %284 = arith.extsi %62 : i64 to i128
    %285 = llvm.load %251 : !llvm.ptr -> i64
    %286 = arith.extsi %285 : i64 to i128
    %288 = arith.trunci %284 : i128 to i64
    %289 = arith.trunci %286 : i128 to i64
    %287 = arith.muli %288, %289 : i64
    %290 = arith.extsi %52 : i64 to i128
    %292 = arith.trunci %290 : i128 to i64
    %291 = arith.remsi %287, %292 : i64
    %293 = arith.addi %130, %291 : i64
    %294 = arith.remsi %293, %52 : i64
    %295 = llvm.load %283 : !llvm.ptr -> i64
    %296 = arith.extsi %295 : i64 to i128
    %297 = arith.extsi %294 : i64 to i128
    %299 = arith.trunci %296 : i128 to i64
    %300 = arith.trunci %297 : i128 to i64
    %298 = arith.muli %299, %300 : i64
    %301 = arith.extsi %52 : i64 to i128
    %303 = arith.trunci %301 : i128 to i64
    %302 = arith.remsi %298, %303 : i64
    llvm.store %302, %283 : i64, !llvm.ptr
    %304 = llvm.load %67 : !llvm.ptr -> i64
    %305 = llvm.load %283 : !llvm.ptr -> i64
    %306 = arith.subi %304, %305 : i64
    %307 = arith.remsi %306, %52 : i64
    %308 = arith.constant 0 : i32
    %310 = arith.extsi %308 : i32 to i64
    %309 = arith.cmpi slt, %307, %310 : i64
    %311 = scf.if %309 -> (i64) {
      %312 = arith.addi %307, %52 : i64
      scf.yield %312 : i64
    } else {
      scf.yield %307 : i64
    }
    %313 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %314 = llvm.call @printf(%313, %311) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %315 = arith.constant 0 : i32
    func.return %315 : i32
  }
}