Problem 358

Cyclic number from full reptend prime near 137...56789; sum of digits.

Answer3284144505
Output3284144505
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n)
Space complexityO(1)O(n)
ApproachFlow solutionBig-integer arithmetic
VerdictOptimal

Flow source

# Project Euler 358
# Cyclic number from full reptend prime near 137...56789; sum of digits.

function isqrt(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let mut x: i64 = n
    let mut y: i64 = (x + 1) / 2
    while y < x {
        x = y
        y = (x + n / x) / 2
    }
    return x
}

function is_prime(n: i64) -> i32 {
    if n < 2 { return 0 }
    if n % 2 == 0 {
        if n == 2 { return 1 }
        return 0
    }
    if n % 3 == 0 {
        if n == 3 { return 1 }
        return 0
    }
    let lim: i64 = isqrt(n)
    let mut d: i64 = 5
    let mut step: i64 = 2
    while d <= lim {
        if n % d == 0 { return 0 }
        d = d + step
        step = 6 - step
    }
    return 1
}

function modpow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base % 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 is_full_reptend(p: i64) -> i32 {
    if is_prime(p) == 0 { return 0 }
    if p == 2 { return 0 }
    if p == 5 { return 0 }
    let mut m: i64 = p - 1
    if m % 2 == 0 {
        if modpow(10, (p - 1) / 2, p) == 1 { return 0 }
        while m % 2 == 0 { m = m / 2 }
    }
    let mut d: i64 = 3
    while d * d <= m {
        if m % d == 0 {
            if modpow(10, (p - 1) / d, p) == 1 { return 0 }
            while m % d == 0 { m = m / d }
        }
        d = d + 2
    }
    if m > 1 {
        if modpow(10, (p - 1) / m, p) == 1 { return 0 }
    }
    return 1
}

function modinv(a0: i64, m: i64) -> i64 {
    let mut t: i64 = 0
    let mut newt: i64 = 1
    let mut r: i64 = m
    let mut newr: i64 = a0 % m
    while newr != 0 {
        let q: i64 = r / newr
        let tmp: i64 = t - q * newt
        t = newt
        newt = tmp
        let tmp2: i64 = r - q * newr
        r = newr
        newr = tmp2
    }
    if r > 1 { return 0 }
    if t < 0 { t = t + m }
    return t
}

function main() -> i32 {
    let PREFIX_SCALE: i64 = 100000000000
    let PREFIX_VALUE: i64 = 137
    let SUFFIX_VALUE: i64 = 56789
    let SUFFIX_MODULUS: i64 = 100000

    let low: i64 = PREFIX_SCALE / (PREFIX_VALUE + 1) + 1
    let high: i64 = PREFIX_SCALE / PREFIX_VALUE

    let inv: i64 = modinv(SUFFIX_VALUE, SUFFIX_MODULUS)
    let residue: i64 = ((0 - inv) % SUFFIX_MODULUS + SUFFIX_MODULUS) % SUFFIX_MODULUS
    let mut p: i64 = low + ((residue - low) % SUFFIX_MODULUS + SUFFIX_MODULUS) % SUFFIX_MODULUS

    let mut found: i64 = 0
    while p <= high {
        if PREFIX_SCALE / p == PREFIX_VALUE {
            if is_full_reptend(p) == 1 {
                found = p
                break
            }
        }
        p = p + SUFFIX_MODULUS
    }

    let ans: i64 = 9 * (found - 1) / 2
    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 isqrt_i64(int64_t n);
int32_t is_prime_i64(int64_t n);
int64_t modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int32_t is_full_reptend_i64(int64_t p);
int64_t modinv_i64_i64(int64_t a0, int64_t m);
int32_t main(void);

int64_t isqrt_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int64_t x = n;
    int64_t y = FLOW_CHECKED_DIV(((x + 1)), (2));
    while (y < x) {
        x = y;
        y = FLOW_CHECKED_DIV(((x + FLOW_CHECKED_DIV((n), (x)))), (2));
    }
    return x;
}

int32_t is_prime_i64(int64_t n) {
    if (n < 2) {
        return 0;
    }
    if (FLOW_CHECKED_MOD((n), (2)) == 0) {
        if (n == 2) {
            return 1;
        }
        return 0;
    }
    if (FLOW_CHECKED_MOD((n), (3)) == 0) {
        if (n == 3) {
            return 1;
        }
        return 0;
    }
    int64_t lim = isqrt_i64(n);
    int64_t d = 5;
    int64_t step = 2;
    while (d <= lim) {
        if (FLOW_CHECKED_MOD((n), (d)) == 0) {
            return 0;
        }
        d = (d + step);
        step = (6 - step);
    }
    return 1;
}

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((base), (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;
}

int32_t is_full_reptend_i64(int64_t p) {
    if (is_prime_i64(p) == 0) {
        return 0;
    }
    if (p == 2) {
        return 0;
    }
    if (p == 5) {
        return 0;
    }
    int64_t m = (p - 1);
    if (FLOW_CHECKED_MOD((m), (2)) == 0) {
        if (modpow_i64_i64_i64(10, FLOW_CHECKED_DIV(((p - 1)), (2)), p) == 1) {
            return 0;
        }
        while (FLOW_CHECKED_MOD((m), (2)) == 0) {
            m = FLOW_CHECKED_DIV((m), (2));
        }
    }
    int64_t d = 3;
    while ((d * d) <= m) {
        if (FLOW_CHECKED_MOD((m), (d)) == 0) {
            if (modpow_i64_i64_i64(10, FLOW_CHECKED_DIV(((p - 1)), (d)), p) == 1) {
                return 0;
            }
            while (FLOW_CHECKED_MOD((m), (d)) == 0) {
                m = FLOW_CHECKED_DIV((m), (d));
            }
        }
        d = (d + 2);
    }
    if (m > 1) {
        if (modpow_i64_i64_i64(10, FLOW_CHECKED_DIV(((p - 1)), (m)), p) == 1) {
            return 0;
        }
    }
    return 1;
}

int64_t modinv_i64_i64(int64_t a0, int64_t m) {
    int64_t t = 0;
    int64_t newt = 1;
    int64_t r = m;
    int64_t newr = FLOW_CHECKED_MOD((a0), (m));
    while (newr != 0) {
        int64_t q = FLOW_CHECKED_DIV((r), (newr));
        int64_t tmp = (t - (q * newt));
        t = newt;
        newt = tmp;
        int64_t tmp2 = (r - (q * newr));
        r = newr;
        newr = tmp2;
    }
    if (r > 1) {
        return 0;
    }
    if (t < 0) {
        t = (t + m);
    }
    return t;
}

int32_t main(void) {
    int64_t PREFIX_SCALE = 100000000000;
    int64_t PREFIX_VALUE = 137;
    int64_t SUFFIX_VALUE = 56789;
    int64_t SUFFIX_MODULUS = 100000;
    int64_t low = (FLOW_CHECKED_DIV((PREFIX_SCALE), ((PREFIX_VALUE + 1))) + 1);
    int64_t high = FLOW_CHECKED_DIV((PREFIX_SCALE), (PREFIX_VALUE));
    int64_t inv = modinv_i64_i64(SUFFIX_VALUE, SUFFIX_MODULUS);
    int64_t residue = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((0 - inv)), (SUFFIX_MODULUS)) + SUFFIX_MODULUS)), (SUFFIX_MODULUS));
    int64_t p = (low + FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((residue - low)), (SUFFIX_MODULUS)) + SUFFIX_MODULUS)), (SUFFIX_MODULUS)));
    int64_t found = 0;
    while (p <= high) {
        if (FLOW_CHECKED_DIV((PREFIX_SCALE), (p)) == PREFIX_VALUE) {
            if (is_full_reptend_i64(p) == 1) {
                found = p;
                break;
            }
        }
        p = (p + SUFFIX_MODULUS);
    }
    int64_t ans = FLOW_CHECKED_DIV(((9 * (found - 1))), (2));
    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 @isqrt(%arg0: i64) -> i64 {
    %0 = arith.constant 0 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg0, %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 = llvm.mlir.constant(1 : i64) : i64
    %6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %6 : i64, !llvm.ptr
    %7 = llvm.load %6 : !llvm.ptr -> i64
    %8 = arith.constant 1 : i32
    %10 = arith.extsi %8 : i32 to i64
    %9 = arith.addi %7, %10 : i64
    %11 = arith.constant 2 : i32
    %13 = arith.extsi %11 : i32 to i64
    %12 = arith.divsi %9, %13 : i64
    %14 = llvm.mlir.constant(1 : i64) : i64
    %15 = llvm.alloca %14 x i64 : (i64) -> !llvm.ptr
    llvm.store %12, %15 : i64, !llvm.ptr
    cf.br ^bb3
    ^bb3:
    %16 = llvm.load %15 : !llvm.ptr -> i64
    %17 = llvm.load %6 : !llvm.ptr -> i64
    %18 = arith.cmpi slt, %16, %17 : i64
    cf.cond_br %18, ^bb4, ^bb5
    ^bb4:
      %19 = llvm.load %15 : !llvm.ptr -> i64
      llvm.store %19, %6 : i64, !llvm.ptr
      %20 = llvm.load %6 : !llvm.ptr -> i64
      %21 = llvm.load %6 : !llvm.ptr -> i64
      %22 = arith.divsi %arg0, %21 : i64
      %23 = arith.addi %20, %22 : i64
      %24 = arith.constant 2 : i32
      %26 = arith.extsi %24 : i32 to i64
      %25 = arith.divsi %23, %26 : i64
      llvm.store %25, %15 : i64, !llvm.ptr
      cf.br ^bb3
    ^bb5:
    %27 = llvm.load %6 : !llvm.ptr -> i64
    func.return %27 : i64
  }
  func.func @is_prime(%arg0: i64) -> i32 {
    %28 = arith.constant 2 : i32
    %30 = arith.extsi %28 : i32 to i64
    %29 = arith.cmpi slt, %arg0, %30 : i64
    cf.cond_br %29, ^bb6, ^bb7
    ^bb6:
      %31 = arith.constant 0 : i32
      func.return %31 : i32
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %32 = arith.constant 2 : i32
    %34 = arith.extsi %32 : i32 to i64
    %33 = arith.remsi %arg0, %34 : i64
    %35 = arith.constant 0 : i32
    %37 = arith.extsi %35 : i32 to i64
    %36 = arith.cmpi eq, %33, %37 : i64
    cf.cond_br %36, ^bb9, ^bb10
    ^bb9:
      %38 = arith.constant 2 : i32
      %40 = arith.extsi %38 : i32 to i64
      %39 = arith.cmpi eq, %arg0, %40 : i64
      cf.cond_br %39, ^bb12, ^bb13
      ^bb12:
        %41 = arith.constant 1 : i32
        func.return %41 : i32
      ^bb13:
        cf.br ^bb14
      ^bb14:
      %42 = arith.constant 0 : i32
      func.return %42 : i32
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %43 = arith.constant 3 : i32
    %45 = arith.extsi %43 : i32 to i64
    %44 = arith.remsi %arg0, %45 : i64
    %46 = arith.constant 0 : i32
    %48 = arith.extsi %46 : i32 to i64
    %47 = arith.cmpi eq, %44, %48 : i64
    cf.cond_br %47, ^bb15, ^bb16
    ^bb15:
      %49 = arith.constant 3 : i32
      %51 = arith.extsi %49 : i32 to i64
      %50 = arith.cmpi eq, %arg0, %51 : i64
      cf.cond_br %50, ^bb18, ^bb19
      ^bb18:
        %52 = arith.constant 1 : i32
        func.return %52 : i32
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %53 = arith.constant 0 : i32
      func.return %53 : i32
    ^bb16:
      cf.br ^bb17
    ^bb17:
    %54 = func.call @isqrt(%arg0) : (i64) -> i64
    %55 = arith.constant 5 : i32
    %56 = arith.extsi %55 : i32 to i64
    %57 = llvm.mlir.constant(1 : i64) : i64
    %58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
    llvm.store %56, %58 : i64, !llvm.ptr
    %59 = arith.constant 2 : 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
    cf.br ^bb21
    ^bb21:
    %63 = llvm.load %58 : !llvm.ptr -> i64
    %64 = arith.cmpi sle, %63, %54 : i64
    cf.cond_br %64, ^bb22, ^bb23
    ^bb22:
      %65 = llvm.load %58 : !llvm.ptr -> i64
      %66 = arith.remsi %arg0, %65 : i64
      %67 = arith.constant 0 : i32
      %69 = arith.extsi %67 : i32 to i64
      %68 = arith.cmpi eq, %66, %69 : i64
      cf.cond_br %68, ^bb24, ^bb25
      ^bb24:
        %70 = arith.constant 0 : i32
        func.return %70 : i32
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %71 = llvm.load %58 : !llvm.ptr -> i64
      %72 = llvm.load %62 : !llvm.ptr -> i64
      %73 = arith.addi %71, %72 : i64
      llvm.store %73, %58 : i64, !llvm.ptr
      %74 = arith.constant 6 : i32
      %75 = llvm.load %62 : !llvm.ptr -> i64
      %77 = arith.extsi %74 : i32 to i64
      %76 = arith.subi %77, %75 : i64
      llvm.store %76, %62 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %78 = arith.constant 1 : i32
    func.return %78 : i32
  }
  func.func @modpow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %79 = arith.constant 1 : i32
    %80 = arith.extsi %79 : i32 to i64
    %81 = llvm.mlir.constant(1 : i64) : i64
    %82 = llvm.alloca %81 x i64 : (i64) -> !llvm.ptr
    llvm.store %80, %82 : i64, !llvm.ptr
    %83 = arith.remsi %arg0, %arg2 : i64
    %84 = llvm.mlir.constant(1 : i64) : i64
    %85 = llvm.alloca %84 x i64 : (i64) -> !llvm.ptr
    llvm.store %83, %85 : i64, !llvm.ptr
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %87 : i64, !llvm.ptr
    cf.br ^bb27
    ^bb27:
    %88 = llvm.load %87 : !llvm.ptr -> i64
    %89 = arith.constant 0 : i32
    %91 = arith.extsi %89 : i32 to i64
    %90 = arith.cmpi sgt, %88, %91 : i64
    cf.cond_br %90, ^bb28, ^bb29
    ^bb28:
      %92 = llvm.load %87 : !llvm.ptr -> i64
      %93 = arith.constant 2 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.remsi %92, %95 : i64
      %96 = arith.constant 1 : i32
      %98 = arith.extsi %96 : i32 to i64
      %97 = arith.cmpi eq, %94, %98 : i64
      cf.cond_br %97, ^bb30, ^bb31
      ^bb30:
        %99 = llvm.load %82 : !llvm.ptr -> i64
        %100 = llvm.load %85 : !llvm.ptr -> i64
        %101 = arith.muli %99, %100 : i64
        %102 = arith.remsi %101, %arg2 : i64
        llvm.store %102, %82 : i64, !llvm.ptr
        cf.br ^bb32
      ^bb31:
        cf.br ^bb32
      ^bb32:
      %103 = llvm.load %85 : !llvm.ptr -> i64
      %104 = llvm.load %85 : !llvm.ptr -> i64
      %105 = arith.muli %103, %104 : i64
      %106 = arith.remsi %105, %arg2 : i64
      llvm.store %106, %85 : i64, !llvm.ptr
      %107 = llvm.load %87 : !llvm.ptr -> i64
      %108 = arith.constant 2 : i32
      %110 = arith.extsi %108 : i32 to i64
      %109 = arith.divsi %107, %110 : i64
      llvm.store %109, %87 : i64, !llvm.ptr
      cf.br ^bb27
    ^bb29:
    %111 = llvm.load %82 : !llvm.ptr -> i64
    func.return %111 : i64
  }
  func.func @is_full_reptend(%arg0: i64) -> i32 {
    %112 = func.call @is_prime(%arg0) : (i64) -> i32
    %113 = arith.constant 0 : i32
    %114 = arith.cmpi eq, %112, %113 : i32
    cf.cond_br %114, ^bb33, ^bb34
    ^bb33:
      %115 = arith.constant 0 : i32
      func.return %115 : i32
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %116 = arith.constant 2 : i32
    %118 = arith.extsi %116 : i32 to i64
    %117 = arith.cmpi eq, %arg0, %118 : i64
    cf.cond_br %117, ^bb36, ^bb37
    ^bb36:
      %119 = arith.constant 0 : i32
      func.return %119 : i32
    ^bb37:
      cf.br ^bb38
    ^bb38:
    %120 = arith.constant 5 : i32
    %122 = arith.extsi %120 : i32 to i64
    %121 = arith.cmpi eq, %arg0, %122 : i64
    cf.cond_br %121, ^bb39, ^bb40
    ^bb39:
      %123 = arith.constant 0 : i32
      func.return %123 : i32
    ^bb40:
      cf.br ^bb41
    ^bb41:
    %124 = arith.constant 1 : i32
    %126 = arith.extsi %124 : i32 to i64
    %125 = arith.subi %arg0, %126 : i64
    %127 = llvm.mlir.constant(1 : i64) : i64
    %128 = llvm.alloca %127 x i64 : (i64) -> !llvm.ptr
    llvm.store %125, %128 : i64, !llvm.ptr
    %129 = llvm.load %128 : !llvm.ptr -> i64
    %130 = arith.constant 2 : i32
    %132 = arith.extsi %130 : i32 to i64
    %131 = arith.remsi %129, %132 : i64
    %133 = arith.constant 0 : i32
    %135 = arith.extsi %133 : i32 to i64
    %134 = arith.cmpi eq, %131, %135 : i64
    cf.cond_br %134, ^bb42, ^bb43
    ^bb42:
      %137 = arith.constant 10 : i32
      %138 = arith.constant 1 : i32
      %140 = arith.extsi %138 : i32 to i64
      %139 = arith.subi %arg0, %140 : i64
      %141 = arith.constant 2 : i32
      %143 = arith.extsi %141 : i32 to i64
      %142 = arith.divsi %139, %143 : i64
      %144 = arith.extsi %137 : i32 to i64
      %136 = func.call @modpow(%144, %142, %arg0) : (i64, i64, i64) -> i64
      %145 = arith.constant 1 : i32
      %147 = arith.extsi %145 : i32 to i64
      %146 = arith.cmpi eq, %136, %147 : i64
      cf.cond_br %146, ^bb45, ^bb46
      ^bb45:
        %148 = arith.constant 0 : i32
        func.return %148 : i32
      ^bb46:
        cf.br ^bb47
      ^bb47:
      cf.br ^bb48
      ^bb48:
      %149 = llvm.load %128 : !llvm.ptr -> i64
      %150 = arith.constant 2 : i32
      %152 = arith.extsi %150 : i32 to i64
      %151 = arith.remsi %149, %152 : i64
      %153 = arith.constant 0 : i32
      %155 = arith.extsi %153 : i32 to i64
      %154 = arith.cmpi eq, %151, %155 : i64
      cf.cond_br %154, ^bb49, ^bb50
      ^bb49:
        %156 = llvm.load %128 : !llvm.ptr -> i64
        %157 = arith.constant 2 : i32
        %159 = arith.extsi %157 : i32 to i64
        %158 = arith.divsi %156, %159 : i64
        llvm.store %158, %128 : i64, !llvm.ptr
        cf.br ^bb48
      ^bb50:
      cf.br ^bb44
    ^bb43:
      cf.br ^bb44
    ^bb44:
    %160 = arith.constant 3 : i32
    %161 = arith.extsi %160 : i32 to i64
    %162 = llvm.mlir.constant(1 : i64) : i64
    %163 = llvm.alloca %162 x i64 : (i64) -> !llvm.ptr
    llvm.store %161, %163 : i64, !llvm.ptr
    cf.br ^bb51
    ^bb51:
    %164 = llvm.load %163 : !llvm.ptr -> i64
    %165 = llvm.load %163 : !llvm.ptr -> i64
    %166 = arith.muli %164, %165 : i64
    %167 = llvm.load %128 : !llvm.ptr -> i64
    %168 = arith.cmpi sle, %166, %167 : i64
    cf.cond_br %168, ^bb52, ^bb53
    ^bb52:
      %169 = llvm.load %128 : !llvm.ptr -> i64
      %170 = llvm.load %163 : !llvm.ptr -> i64
      %171 = arith.remsi %169, %170 : i64
      %172 = arith.constant 0 : i32
      %174 = arith.extsi %172 : i32 to i64
      %173 = arith.cmpi eq, %171, %174 : i64
      cf.cond_br %173, ^bb54, ^bb55
      ^bb54:
        %176 = arith.constant 10 : i32
        %177 = arith.constant 1 : i32
        %179 = arith.extsi %177 : i32 to i64
        %178 = arith.subi %arg0, %179 : i64
        %180 = llvm.load %163 : !llvm.ptr -> i64
        %181 = arith.divsi %178, %180 : i64
        %182 = arith.extsi %176 : i32 to i64
        %175 = func.call @modpow(%182, %181, %arg0) : (i64, i64, i64) -> i64
        %183 = arith.constant 1 : i32
        %185 = arith.extsi %183 : i32 to i64
        %184 = arith.cmpi eq, %175, %185 : i64
        cf.cond_br %184, ^bb57, ^bb58
        ^bb57:
          %186 = arith.constant 0 : i32
          func.return %186 : i32
        ^bb58:
          cf.br ^bb59
        ^bb59:
        cf.br ^bb60
        ^bb60:
        %187 = llvm.load %128 : !llvm.ptr -> i64
        %188 = llvm.load %163 : !llvm.ptr -> i64
        %189 = arith.remsi %187, %188 : i64
        %190 = arith.constant 0 : i32
        %192 = arith.extsi %190 : i32 to i64
        %191 = arith.cmpi eq, %189, %192 : i64
        cf.cond_br %191, ^bb61, ^bb62
        ^bb61:
          %193 = llvm.load %128 : !llvm.ptr -> i64
          %194 = llvm.load %163 : !llvm.ptr -> i64
          %195 = arith.divsi %193, %194 : i64
          llvm.store %195, %128 : i64, !llvm.ptr
          cf.br ^bb60
        ^bb62:
        cf.br ^bb56
      ^bb55:
        cf.br ^bb56
      ^bb56:
      %196 = llvm.load %163 : !llvm.ptr -> i64
      %197 = arith.constant 2 : i32
      %199 = arith.extsi %197 : i32 to i64
      %198 = arith.addi %196, %199 : i64
      llvm.store %198, %163 : i64, !llvm.ptr
      cf.br ^bb51
    ^bb53:
    %200 = llvm.load %128 : !llvm.ptr -> i64
    %201 = arith.constant 1 : i32
    %203 = arith.extsi %201 : i32 to i64
    %202 = arith.cmpi sgt, %200, %203 : i64
    cf.cond_br %202, ^bb63, ^bb64
    ^bb63:
      %205 = arith.constant 10 : i32
      %206 = arith.constant 1 : i32
      %208 = arith.extsi %206 : i32 to i64
      %207 = arith.subi %arg0, %208 : i64
      %209 = llvm.load %128 : !llvm.ptr -> i64
      %210 = arith.divsi %207, %209 : i64
      %211 = arith.extsi %205 : i32 to i64
      %204 = func.call @modpow(%211, %210, %arg0) : (i64, i64, i64) -> i64
      %212 = arith.constant 1 : i32
      %214 = arith.extsi %212 : i32 to i64
      %213 = arith.cmpi eq, %204, %214 : i64
      cf.cond_br %213, ^bb66, ^bb67
      ^bb66:
        %215 = arith.constant 0 : i32
        func.return %215 : i32
      ^bb67:
        cf.br ^bb68
      ^bb68:
      cf.br ^bb65
    ^bb64:
      cf.br ^bb65
    ^bb65:
    %216 = arith.constant 1 : i32
    func.return %216 : i32
  }
  func.func @modinv(%arg0: i64, %arg1: i64) -> i64 {
    %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
    %225 = llvm.mlir.constant(1 : i64) : i64
    %226 = llvm.alloca %225 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %226 : i64, !llvm.ptr
    %227 = arith.remsi %arg0, %arg1 : i64
    %228 = llvm.mlir.constant(1 : i64) : i64
    %229 = llvm.alloca %228 x i64 : (i64) -> !llvm.ptr
    llvm.store %227, %229 : i64, !llvm.ptr
    cf.br ^bb69
    ^bb69:
    %230 = llvm.load %229 : !llvm.ptr -> i64
    %231 = arith.constant 0 : i32
    %233 = arith.extsi %231 : i32 to i64
    %232 = arith.cmpi ne, %230, %233 : i64
    cf.cond_br %232, ^bb70, ^bb71
    ^bb70:
      %234 = llvm.load %226 : !llvm.ptr -> i64
      %235 = llvm.load %229 : !llvm.ptr -> i64
      %236 = arith.divsi %234, %235 : i64
      %237 = llvm.load %220 : !llvm.ptr -> i64
      %238 = llvm.load %224 : !llvm.ptr -> i64
      %239 = arith.muli %236, %238 : i64
      %240 = arith.subi %237, %239 : i64
      %241 = llvm.load %224 : !llvm.ptr -> i64
      llvm.store %241, %220 : i64, !llvm.ptr
      llvm.store %240, %224 : i64, !llvm.ptr
      %242 = llvm.load %226 : !llvm.ptr -> i64
      %243 = llvm.load %229 : !llvm.ptr -> i64
      %244 = arith.muli %236, %243 : i64
      %245 = arith.subi %242, %244 : i64
      %246 = llvm.load %229 : !llvm.ptr -> i64
      llvm.store %246, %226 : i64, !llvm.ptr
      llvm.store %245, %229 : i64, !llvm.ptr
      cf.br ^bb69
    ^bb71:
    %247 = llvm.load %226 : !llvm.ptr -> i64
    %248 = arith.constant 1 : i32
    %250 = arith.extsi %248 : i32 to i64
    %249 = arith.cmpi sgt, %247, %250 : i64
    cf.cond_br %249, ^bb72, ^bb73
    ^bb72:
      %251 = arith.constant 0 : i32
      %252 = arith.extsi %251 : i32 to i64
      func.return %252 : i64
    ^bb73:
      cf.br ^bb74
    ^bb74:
    %253 = llvm.load %220 : !llvm.ptr -> i64
    %254 = arith.constant 0 : i32
    %256 = arith.extsi %254 : i32 to i64
    %255 = arith.cmpi slt, %253, %256 : i64
    cf.cond_br %255, ^bb75, ^bb76
    ^bb75:
      %257 = llvm.load %220 : !llvm.ptr -> i64
      %258 = arith.addi %257, %arg1 : i64
      llvm.store %258, %220 : i64, !llvm.ptr
      cf.br ^bb77
    ^bb76:
      cf.br ^bb77
    ^bb77:
    %259 = llvm.load %220 : !llvm.ptr -> i64
    func.return %259 : i64
  }
  func.func @main() -> i32 {
    %260 = arith.constant 95705032704 : i32
    %261 = arith.extsi %260 : i32 to i64
    %262 = arith.constant 137 : i32
    %263 = arith.extsi %262 : i32 to i64
    %264 = arith.constant 56789 : i32
    %265 = arith.extsi %264 : i32 to i64
    %266 = arith.constant 100000 : i32
    %267 = arith.extsi %266 : i32 to i64
    %268 = arith.constant 1 : i32
    %270 = arith.extsi %268 : i32 to i64
    %269 = arith.addi %263, %270 : i64
    %271 = arith.divsi %261, %269 : i64
    %272 = arith.constant 1 : i32
    %274 = arith.extsi %272 : i32 to i64
    %273 = arith.addi %271, %274 : i64
    %275 = arith.divsi %261, %263 : i64
    %276 = func.call @modinv(%265, %267) : (i64, i64) -> i64
    %277 = arith.constant 0 : i32
    %279 = arith.extsi %277 : i32 to i64
    %278 = arith.subi %279, %276 : i64
    %280 = arith.remsi %278, %267 : i64
    %281 = arith.addi %280, %267 : i64
    %282 = arith.remsi %281, %267 : i64
    %283 = arith.subi %282, %273 : i64
    %284 = arith.remsi %283, %267 : i64
    %285 = arith.addi %284, %267 : i64
    %286 = arith.remsi %285, %267 : i64
    %287 = arith.addi %273, %286 : i64
    %288 = llvm.mlir.constant(1 : i64) : i64
    %289 = llvm.alloca %288 x i64 : (i64) -> !llvm.ptr
    llvm.store %287, %289 : i64, !llvm.ptr
    %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
    cf.br ^bb78
    ^bb78:
    %294 = llvm.load %289 : !llvm.ptr -> i64
    %295 = arith.cmpi sle, %294, %275 : i64
    cf.cond_br %295, ^bb79, ^bb80
    ^bb79:
      %296 = llvm.load %289 : !llvm.ptr -> i64
      %297 = arith.divsi %261, %296 : i64
      %298 = arith.cmpi eq, %297, %263 : i64
      cf.cond_br %298, ^bb81, ^bb82
      ^bb81:
        %300 = llvm.load %289 : !llvm.ptr -> i64
        %299 = func.call @is_full_reptend(%300) : (i64) -> i32
        %301 = arith.constant 1 : i32
        %302 = arith.cmpi eq, %299, %301 : i32
        cf.cond_br %302, ^bb84, ^bb85
        ^bb84:
          %303 = llvm.load %289 : !llvm.ptr -> i64
          llvm.store %303, %293 : i64, !llvm.ptr
          cf.br ^bb80
        ^bb85:
          cf.br ^bb86
        ^bb86:
        cf.br ^bb83
      ^bb82:
        cf.br ^bb83
      ^bb83:
      %304 = llvm.load %289 : !llvm.ptr -> i64
      %305 = arith.addi %304, %267 : i64
      llvm.store %305, %289 : i64, !llvm.ptr
      cf.br ^bb78
    ^bb80:
    %306 = arith.constant 9 : i32
    %307 = llvm.load %293 : !llvm.ptr -> i64
    %308 = arith.constant 1 : i32
    %310 = arith.extsi %308 : i32 to i64
    %309 = arith.subi %307, %310 : i64
    %312 = arith.extsi %306 : i32 to i64
    %311 = arith.muli %312, %309 : i64
    %313 = arith.constant 2 : i32
    %315 = arith.extsi %313 : i32 to i64
    %314 = arith.divsi %311, %315 : i64
    %316 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %317 = llvm.call @printf(%316, %314) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %318 = arith.constant 0 : i32
    func.return %318 : i32
  }
}