Problem 356

Last 8 digits of sum_{n=1..30} floor(a_n^987654321), a_n largest root of x^3-2^n x^2+n.

Answer28010159
Output28010159
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n^3) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 356
# Last 8 digits of sum_{n=1..30} floor(a_n^987654321), a_n largest root of x^3-2^n x^2+n.

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

function mat_mul(a: ptr<i64>, b: ptr<i64>, out: ptr<i64>, mod: i64) -> void {
    let mut i: i32 = 0
    while i < 3 {
        let mut j: i32 = 0
        while j < 3 {
            let mut s: i64 = 0
            let mut k: i32 = 0
            while k < 3 {
                s = (s + a[i * 3 + k] * b[k * 3 + j]) % mod
                k = k + 1
            }
            out[i * 3 + j] = s
            j = j + 1
        }
        i = i + 1
    }
}

function mat_copy(src: ptr<i64>, dst: ptr<i64>) -> void {
    let mut i: i32 = 0
    while i < 9 {
        dst[i] = src[i]
        i = i + 1
    }
}

function power_sum_mod(n: i64, k: i64, mod: i64) -> i64 {
    if k == 0 { return 3 % mod }
    let A: i64 = modpow2(n, mod)
    if k == 1 { return A }
    if k == 2 { return (A * A) % mod }

    # M = [[A, 0, -n], [1, 0, 0], [0, 1, 0]]
    let M: ptr<i64> = calloc(9, 8)
    let base: ptr<i64> = calloc(9, 8)
    let res: ptr<i64> = calloc(9, 8)
    let tmp: ptr<i64> = calloc(9, 8)
    if M == null || base == null || res == null || tmp == null { return 0 }

    M[0] = A
    M[1] = 0
    M[2] = ((0 - n) % mod + mod) % mod
    M[3] = 1
    M[4] = 0
    M[5] = 0
    M[6] = 0
    M[7] = 1
    M[8] = 0

    # identity
    res[0] = 1; res[1] = 0; res[2] = 0
    res[3] = 0; res[4] = 1; res[5] = 0
    res[6] = 0; res[7] = 0; res[8] = 1

    mat_copy(M, base)
    let mut e: i64 = k - 2
    while e > 0 {
        if e % 2 == 1 {
            mat_mul(res, base, tmp, mod)
            mat_copy(tmp, res)
        }
        mat_mul(base, base, tmp, mod)
        mat_copy(tmp, base)
        e = e / 2
    }

    let v0: i64 = (A * A) % mod
    let v1: i64 = A
    let v2: i64 = 3 % mod
    let Sk: i64 = (res[0] * v0 + res[1] * v1 + res[2] * v2) % mod

    free(tmp)
    free(res)
    free(base)
    free(M)
    return Sk
}

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

function main() -> i32 {
    let MOD: i64 = 100000000
    let EXP: i64 = 987654321
    let mut total: i64 = 0
    let mut n: i64 = 1
    while n <= 30 {
        let Sk: i64 = power_sum_mod(n, EXP, MOD)
        total = (total + Sk - 1) % MOD
        n = n + 1
    }
    if total < 0 { total = total + MOD }
    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 modpow2_i64_i64(int64_t n, int64_t mod);
void mat_mul_ptr_i64_ptr_i64_ptr_i64_i64(int64_t* a, int64_t* b, int64_t* out, int64_t mod);
void mat_copy_ptr_i64_ptr_i64(int64_t* src, int64_t* dst);
int64_t power_sum_mod_i64_i64_i64(int64_t n, int64_t k, int64_t mod);
int32_t main(void);

int64_t modpow2_i64_i64(int64_t n, int64_t mod) {
    int64_t r = 1;
    int64_t b = FLOW_CHECKED_MOD((2), (mod));
    int64_t e = n;
    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;
}

void mat_mul_ptr_i64_ptr_i64_ptr_i64_i64(int64_t* a, int64_t* b, int64_t* out, int64_t mod) {
    int32_t i = 0;
    while (i < 3) {
        int32_t j = 0;
        while (j < 3) {
            int64_t s = 0;
            int32_t k = 0;
            while (k < 3) {
                s = FLOW_CHECKED_MOD(((s + (a[((i * 3) + k)] * b[((k * 3) + j)]))), (mod));
                k = (k + 1);
            }
            out[((i * 3) + j)] = s;
            j = (j + 1);
        }
        i = (i + 1);
    }
}

void mat_copy_ptr_i64_ptr_i64(int64_t* src, int64_t* dst) {
    int32_t i = 0;
    while (i < 9) {
        dst[i] = src[i];
        i = (i + 1);
    }
}

int64_t power_sum_mod_i64_i64_i64(int64_t n, int64_t k, int64_t mod) {
    if (k == 0) {
        return FLOW_CHECKED_MOD((3), (mod));
    }
    int64_t A = modpow2_i64_i64(n, mod);
    if (k == 1) {
        return A;
    }
    if (k == 2) {
        return FLOW_CHECKED_MOD(((A * A)), (mod));
    }
    int64_t* M = (int64_t*)(calloc(9, 8));
    int64_t* base = (int64_t*)(calloc(9, 8));
    int64_t* res = (int64_t*)(calloc(9, 8));
    int64_t* tmp = (int64_t*)(calloc(9, 8));
    if ((((M == NULL || base == NULL) || res == NULL) || tmp == NULL)) {
        return 0;
    }
    M[0] = A;
    M[1] = 0;
    M[2] = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((0 - n)), (mod)) + mod)), (mod));
    M[3] = 1;
    M[4] = 0;
    M[5] = 0;
    M[6] = 0;
    M[7] = 1;
    M[8] = 0;
    res[0] = 1;
    res[1] = 0;
    res[2] = 0;
    res[3] = 0;
    res[4] = 1;
    res[5] = 0;
    res[6] = 0;
    res[7] = 0;
    res[8] = 1;
    mat_copy_ptr_i64_ptr_i64(M, base);
    int64_t e = (k - 2);
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            mat_mul_ptr_i64_ptr_i64_ptr_i64_i64(res, base, tmp, mod);
            mat_copy_ptr_i64_ptr_i64(tmp, res);
        }
        mat_mul_ptr_i64_ptr_i64_ptr_i64_i64(base, base, tmp, mod);
        mat_copy_ptr_i64_ptr_i64(tmp, base);
        e = FLOW_CHECKED_DIV((e), (2));
    }
    int64_t v0 = FLOW_CHECKED_MOD(((A * A)), (mod));
    int64_t v1 = A;
    int64_t v2 = FLOW_CHECKED_MOD((3), (mod));
    int64_t Sk = FLOW_CHECKED_MOD(((((res[0] * v0) + (res[1] * v1)) + (res[2] * v2))), (mod));
    free(tmp);
    free(res);
    free(base);
    free(M);
    return Sk;
}



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