Problem 973

Computed via S_k_value and X(n) with modular arithmetic mod 10^9+7.

Answer427278142
Output427278142
StatusPASS
Native helperno
Runtime160 ms
Peak memory1536 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(log n)
Space complexityO(n)O(1)
ApproachFlow solutionModular exponentiation
VerdictSuboptimal

Flow source

# Project Euler 973
# Computed via S_k_value and X(n) with modular arithmetic mod 10^9+7.

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

const MOD: i64 = 1000000007
const INV2: i64 = 500000004
const N: i64 = 10000

function mod(x: i64) -> i64 {
    let mut r: i64 = x % MOD
    if r < 0 { r = r + MOD }
    return r
}

function powmod(base0: i64, exp0: i64, m: i64) -> i64 {
    let mut result: i64 = 1 % m
    let mut base: i64 = base0 % m
    if base < 0 { base = base + m }
    let mut exp: i64 = exp0
    while exp > 0 {
        if (exp & 1) != 0 {
            result = ((result as i128) * (base as i128) % (m as i128)) as i64
        }
        base = ((base as i128) * (base as i128) % (m as i128)) as i64
        exp = exp >> 1
    }
    return result
}

function S_k_value(n: i64, k: i64) -> i64 {
    if k == 0 {
        if n == 0 { return 1 }
        return mod(0 - powmod(MOD - 2, n - 1, MOD))
    }
    let B: i64 = 1 << k
    let period: i64 = B << 1
    let per_mask: i64 = period - 1
    let S: ptr<i64> = calloc(n + 1, 8)
    let P: ptr<i64> = calloc(n + 1, 8)
    S[0] = 1
    P[0] = 1
    let mut nn: i64 = 1
    while nn <= n {
        let mut val: i64 = 0
        let mut s: i64 = 1
        while s <= nn {
            let p: i64 = s & per_mask
            let mut sign: i64 = 1
            let mut e: i64 = 0
            if p < B {
                sign = 1
                e = s + (B - p) - 1
            } else {
                sign = -1
                e = s + (period - p) - 1
            }
            if e > nn { e = nn }
            let t_lo: i64 = nn - e
            let t_hi: i64 = nn - s
            let mut sum_range: i64 = 0
            if t_lo == 0 {
                sum_range = P[t_hi]
            } else {
                sum_range = P[t_hi] - P[t_lo - 1]
            }
            val = val + sign * sum_range
            s = e + 1
        }
        S[nn] = mod(val)
        P[nn] = mod(P[nn - 1] + S[nn])
        nn = nn + 1
    }
    let result: i64 = S[n]
    free(S)
    free(P)
    return result
}

function X(n: i64) -> i64 {
    if n <= 0 { return 0 }
    let P: i64 = powmod(2, n - 1, MOD)
    let mut total: i64 = 0
    let mut k: i64 = 0
    while k < 14 {
        let Sk: i64 = S_k_value(n, k)
        let Ak: i64 = mod(((P - Sk) % MOD) * INV2 % MOD)
        total = (total + powmod(2, k, MOD) * Ak) % MOD
        k = k + 1
    }
    total = mod(total - (n & 1))
    return total
}

function main() -> i32 {
    printf("%lld\n", X(N))
    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 mod_i64(int64_t x);
int64_t powmod_i64_i64_i64(int64_t base0, int64_t exp0, int64_t m);
int64_t S_k_value_i64_i64(int64_t n, int64_t k);
int64_t X_i64(int64_t n);
int32_t main(void);

static const int64_t MOD = 1000000007;
static const int64_t INV2 = 500000004;
static const int64_t N = 10000;



int64_t mod_i64(int64_t x) {
    int64_t r = FLOW_CHECKED_MOD((x), (MOD));
    if (r < 0) {
        r = (r + MOD);
    }
    return r;
}

int64_t powmod_i64_i64_i64(int64_t base0, int64_t exp0, int64_t m) {
    int64_t result = FLOW_CHECKED_MOD((1), (m));
    int64_t base = FLOW_CHECKED_MOD((base0), (m));
    if (base < 0) {
        base = (base + m);
    }
    int64_t exp = exp0;
    while (exp > 0) {
        if ((exp & 1) != 0) {
            result = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(result)) * ((__int128)(base)))), (((__int128)(m))))));
        }
        base = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(base)) * ((__int128)(base)))), (((__int128)(m))))));
        exp = FLOW_CHECKED_SHR((exp), (1));
    }
    return result;
}

int64_t S_k_value_i64_i64(int64_t n, int64_t k) {
    if (k == 0) {
        if (n == 0) {
            return 1;
        }
        return mod_i64((0 - powmod_i64_i64_i64((MOD - 2), (n - 1), MOD)));
    }
    int64_t B = FLOW_CHECKED_SHL((1), (k));
    int64_t period = FLOW_CHECKED_SHL((B), (1));
    int64_t per_mask = (period - 1);
    int64_t* S = (int64_t*)(calloc((n + 1), 8));
    int64_t* P = (int64_t*)(calloc((n + 1), 8));
    S[0] = 1;
    P[0] = 1;
    int64_t nn = 1;
    while (nn <= n) {
        int64_t val = 0;
        int64_t s = 1;
        while (s <= nn) {
            int64_t p = (s & per_mask);
            int64_t sign = 1;
            int64_t e = 0;
            if (p < B) {
                sign = 1;
                e = ((s + (B - p)) - 1);
            } else {
                sign = (-1);
                e = ((s + (period - p)) - 1);
            }
            if (e > nn) {
                e = nn;
            }
            int64_t t_lo = (nn - e);
            int64_t t_hi = (nn - s);
            int64_t sum_range = 0;
            if (t_lo == 0) {
                sum_range = P[t_hi];
            } else {
                sum_range = (P[t_hi] - P[(t_lo - 1)]);
            }
            val = (val + (sign * sum_range));
            s = (e + 1);
        }
        S[nn] = mod_i64(val);
        P[nn] = mod_i64((P[(nn - 1)] + S[nn]));
        nn = (nn + 1);
    }
    int64_t result = S[n];
    free(S);
    free(P);
    return result;
}

int64_t X_i64(int64_t n) {
    if (n <= 0) {
        return 0;
    }
    int64_t P = powmod_i64_i64_i64(2, (n - 1), MOD);
    int64_t total = 0;
    int64_t k = 0;
    while (k < 14) {
        int64_t Sk = S_k_value_i64_i64(n, k);
        int64_t Ak = mod_i64(FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((P - Sk)), (MOD)) * INV2)), (MOD)));
        total = FLOW_CHECKED_MOD(((total + (powmod_i64_i64_i64(2, k, MOD) * Ak))), (MOD));
        k = (k + 1);
    }
    total = mod_i64((total - (n & 1)));
    return total;
}

int32_t main(void) {
    printf("%lld\n", X_i64(N));
    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) -> ()
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
  // Constant: INV2
  llvm.mlir.global internal constant @INV2(500000004 : i64) : i64
  // Constant: N
  llvm.mlir.global internal constant @N(10000 : i64) : i64
  func.func @mod(%arg0: i64) -> i64 {
    %0 = llvm.mlir.addressof @MOD : !llvm.ptr
    %1 = llvm.load %0 : !llvm.ptr -> i64
    %2 = arith.remsi %arg0, %1 : i64
    %3 = llvm.mlir.constant(1 : i64) : i64
    %4 = llvm.alloca %3 x i64 : (i64) -> !llvm.ptr
    llvm.store %2, %4 : i64, !llvm.ptr
    %5 = llvm.load %4 : !llvm.ptr -> i64
    %6 = arith.constant 0 : i32
    %8 = arith.extsi %6 : i32 to i64
    %7 = arith.cmpi slt, %5, %8 : i64
    cf.cond_br %7, ^bb0, ^bb1
    ^bb0:
      %9 = llvm.load %4 : !llvm.ptr -> i64
      %10 = llvm.mlir.addressof @MOD : !llvm.ptr
      %11 = llvm.load %10 : !llvm.ptr -> i64
      %12 = arith.addi %9, %11 : i64
      llvm.store %12, %4 : i64, !llvm.ptr
      cf.br ^bb2
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %13 = llvm.load %4 : !llvm.ptr -> i64
    func.return %13 : i64
  }
  func.func @powmod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %14 = arith.constant 1 : i32
    %16 = arith.extsi %14 : i32 to i64
    %15 = arith.remsi %16, %arg2 : i64
    %17 = llvm.mlir.constant(1 : i64) : i64
    %18 = llvm.alloca %17 x i64 : (i64) -> !llvm.ptr
    llvm.store %15, %18 : i64, !llvm.ptr
    %19 = arith.remsi %arg0, %arg2 : i64
    %20 = llvm.mlir.constant(1 : i64) : i64
    %21 = llvm.alloca %20 x i64 : (i64) -> !llvm.ptr
    llvm.store %19, %21 : i64, !llvm.ptr
    %22 = llvm.load %21 : !llvm.ptr -> i64
    %23 = arith.constant 0 : i32
    %25 = arith.extsi %23 : i32 to i64
    %24 = arith.cmpi slt, %22, %25 : i64
    cf.cond_br %24, ^bb3, ^bb4
    ^bb3:
      %26 = llvm.load %21 : !llvm.ptr -> i64
      %27 = arith.addi %26, %arg2 : i64
      llvm.store %27, %21 : i64, !llvm.ptr
      cf.br ^bb5
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %28 = llvm.mlir.constant(1 : i64) : i64
    %29 = llvm.alloca %28 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %29 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %30 = llvm.load %29 : !llvm.ptr -> i64
    %31 = arith.constant 0 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.cmpi sgt, %30, %33 : i64
    cf.cond_br %32, ^bb7, ^bb8
    ^bb7:
      %34 = llvm.load %29 : !llvm.ptr -> i64
      %35 = arith.constant 1 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.andi %34, %37 : i64
      %38 = arith.constant 0 : i32
      %40 = arith.extsi %38 : i32 to i64
      %39 = arith.cmpi ne, %36, %40 : i64
      cf.cond_br %39, ^bb9, ^bb10
      ^bb9:
        %41 = llvm.load %18 : !llvm.ptr -> i64
        %42 = arith.extsi %41 : i64 to i128
        %43 = llvm.load %21 : !llvm.ptr -> i64
        %44 = arith.extsi %43 : i64 to i128
        %46 = arith.trunci %42 : i128 to i64
        %47 = arith.trunci %44 : i128 to i64
        %45 = arith.muli %46, %47 : i64
        %48 = arith.extsi %arg2 : i64 to i128
        %50 = arith.trunci %48 : i128 to i64
        %49 = arith.remsi %45, %50 : i64
        llvm.store %49, %18 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %51 = llvm.load %21 : !llvm.ptr -> i64
      %52 = arith.extsi %51 : i64 to i128
      %53 = llvm.load %21 : !llvm.ptr -> i64
      %54 = arith.extsi %53 : i64 to i128
      %56 = arith.trunci %52 : i128 to i64
      %57 = arith.trunci %54 : i128 to i64
      %55 = arith.muli %56, %57 : i64
      %58 = arith.extsi %arg2 : i64 to i128
      %60 = arith.trunci %58 : i128 to i64
      %59 = arith.remsi %55, %60 : i64
      llvm.store %59, %21 : i64, !llvm.ptr
      %61 = llvm.load %29 : !llvm.ptr -> i64
      %62 = arith.constant 1 : i32
      %64 = arith.extsi %62 : i32 to i64
      %63 = arith.shrsi %61, %64 : i64
      llvm.store %63, %29 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %65 = llvm.load %18 : !llvm.ptr -> i64
    func.return %65 : i64
  }
  func.func @S_k_value(%arg0: i64, %arg1: i64) -> i64 {
    %66 = arith.constant 0 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.cmpi eq, %arg1, %68 : i64
    cf.cond_br %67, ^bb12, ^bb13
    ^bb12:
      %69 = arith.constant 0 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.cmpi eq, %arg0, %71 : i64
      cf.cond_br %70, ^bb15, ^bb16
      ^bb15:
        %72 = arith.constant 1 : i32
        %73 = arith.extsi %72 : i32 to i64
        func.return %73 : i64
      ^bb16:
        cf.br ^bb17
      ^bb17:
      %75 = arith.constant 0 : i32
      %77 = llvm.mlir.addressof @MOD : !llvm.ptr
      %78 = llvm.load %77 : !llvm.ptr -> i64
      %79 = arith.constant 2 : i32
      %81 = arith.extsi %79 : i32 to i64
      %80 = arith.subi %78, %81 : i64
      %82 = arith.constant 1 : i32
      %84 = arith.extsi %82 : i32 to i64
      %83 = arith.subi %arg0, %84 : i64
      %85 = llvm.mlir.addressof @MOD : !llvm.ptr
      %86 = llvm.load %85 : !llvm.ptr -> i64
      %76 = func.call @powmod(%80, %83, %86) : (i64, i64, i64) -> i64
      %88 = arith.extsi %75 : i32 to i64
      %87 = arith.subi %88, %76 : i64
      %74 = func.call @mod(%87) : (i64) -> i64
      func.return %74 : i64
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %89 = arith.constant 1 : i32
    %91 = arith.extsi %89 : i32 to i64
    %90 = arith.shli %91, %arg1 : i64
    %92 = arith.constant 1 : i32
    %94 = arith.extsi %92 : i32 to i64
    %93 = arith.shli %90, %94 : i64
    %95 = arith.constant 1 : i32
    %97 = arith.extsi %95 : i32 to i64
    %96 = arith.subi %93, %97 : i64
    %99 = arith.constant 1 : i32
    %101 = arith.extsi %99 : i32 to i64
    %100 = arith.addi %arg0, %101 : i64
    %102 = arith.constant 8 : i32
    %103 = arith.extsi %102 : i32 to i64
    %98 = func.call @calloc(%100, %103) : (i64, i64) -> !llvm.ptr
    %105 = arith.constant 1 : i32
    %107 = arith.extsi %105 : i32 to i64
    %106 = arith.addi %arg0, %107 : i64
    %108 = arith.constant 8 : i32
    %109 = arith.extsi %108 : i32 to i64
    %104 = func.call @calloc(%106, %109) : (i64, i64) -> !llvm.ptr
    %110 = arith.constant 1 : i32
    %111 = arith.constant 0 : i32
    %112 = arith.extsi %110 : i32 to i64
    %113 = arith.extsi %111 : i32 to i64
    %114 = llvm.getelementptr %98[%113] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %112, %114 : i64, !llvm.ptr
    %115 = arith.constant 1 : i32
    %116 = arith.constant 0 : i32
    %117 = arith.extsi %115 : i32 to i64
    %118 = arith.extsi %116 : i32 to i64
    %119 = llvm.getelementptr %104[%118] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %117, %119 : i64, !llvm.ptr
    %120 = arith.constant 1 : i32
    %121 = arith.extsi %120 : i32 to i64
    %122 = llvm.mlir.constant(1 : i64) : i64
    %123 = llvm.alloca %122 x i64 : (i64) -> !llvm.ptr
    llvm.store %121, %123 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %124 = llvm.load %123 : !llvm.ptr -> i64
    %125 = arith.cmpi sle, %124, %arg0 : i64
    cf.cond_br %125, ^bb19, ^bb20
    ^bb19:
      %126 = arith.constant 0 : i32
      %127 = arith.extsi %126 : i32 to i64
      %128 = llvm.mlir.constant(1 : i64) : i64
      %129 = llvm.alloca %128 x i64 : (i64) -> !llvm.ptr
      llvm.store %127, %129 : i64, !llvm.ptr
      %130 = arith.constant 1 : i32
      %131 = arith.extsi %130 : i32 to i64
      %132 = llvm.mlir.constant(1 : i64) : i64
      %133 = llvm.alloca %132 x i64 : (i64) -> !llvm.ptr
      llvm.store %131, %133 : i64, !llvm.ptr
      cf.br ^bb21
      ^bb21:
      %134 = llvm.load %133 : !llvm.ptr -> i64
      %135 = llvm.load %123 : !llvm.ptr -> i64
      %136 = arith.cmpi sle, %134, %135 : i64
      cf.cond_br %136, ^bb22, ^bb23
      ^bb22:
        %137 = llvm.load %133 : !llvm.ptr -> i64
        %138 = arith.andi %137, %96 : i64
        %139 = arith.constant 1 : i32
        %140 = arith.extsi %139 : i32 to i64
        %141 = llvm.mlir.constant(1 : i64) : i64
        %142 = llvm.alloca %141 x i64 : (i64) -> !llvm.ptr
        llvm.store %140, %142 : i64, !llvm.ptr
        %143 = arith.constant 0 : i32
        %144 = arith.extsi %143 : i32 to i64
        %145 = llvm.mlir.constant(1 : i64) : i64
        %146 = llvm.alloca %145 x i64 : (i64) -> !llvm.ptr
        llvm.store %144, %146 : i64, !llvm.ptr
        %147 = arith.cmpi slt, %138, %90 : i64
        cf.cond_br %147, ^bb24, ^bb25
        ^bb24:
          %148 = arith.constant 1 : i32
          %149 = arith.extsi %148 : i32 to i64
          llvm.store %149, %142 : i64, !llvm.ptr
          %150 = llvm.load %133 : !llvm.ptr -> i64
          %151 = arith.subi %90, %138 : i64
          %152 = arith.addi %150, %151 : i64
          %153 = arith.constant 1 : i32
          %155 = arith.extsi %153 : i32 to i64
          %154 = arith.subi %152, %155 : i64
          llvm.store %154, %146 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb25:
          %156 = arith.constant 1 : i32
          %158 = arith.constant 0 : i32
          %157 = arith.subi %158, %156 : i32
          %159 = arith.extsi %157 : i32 to i64
          llvm.store %159, %142 : i64, !llvm.ptr
          %160 = llvm.load %133 : !llvm.ptr -> i64
          %161 = arith.subi %93, %138 : i64
          %162 = arith.addi %160, %161 : i64
          %163 = arith.constant 1 : i32
          %165 = arith.extsi %163 : i32 to i64
          %164 = arith.subi %162, %165 : i64
          llvm.store %164, %146 : i64, !llvm.ptr
          cf.br ^bb26
        ^bb26:
        %166 = llvm.load %146 : !llvm.ptr -> i64
        %167 = llvm.load %123 : !llvm.ptr -> i64
        %168 = arith.cmpi sgt, %166, %167 : i64
        cf.cond_br %168, ^bb27, ^bb28
        ^bb27:
          %169 = llvm.load %123 : !llvm.ptr -> i64
          llvm.store %169, %146 : i64, !llvm.ptr
          cf.br ^bb29
        ^bb28:
          cf.br ^bb29
        ^bb29:
        %170 = llvm.load %123 : !llvm.ptr -> i64
        %171 = llvm.load %146 : !llvm.ptr -> i64
        %172 = arith.subi %170, %171 : i64
        %173 = llvm.load %123 : !llvm.ptr -> i64
        %174 = llvm.load %133 : !llvm.ptr -> i64
        %175 = arith.subi %173, %174 : i64
        %176 = arith.constant 0 : i32
        %177 = arith.extsi %176 : i32 to i64
        %178 = llvm.mlir.constant(1 : i64) : i64
        %179 = llvm.alloca %178 x i64 : (i64) -> !llvm.ptr
        llvm.store %177, %179 : i64, !llvm.ptr
        %180 = arith.constant 0 : i32
        %182 = arith.extsi %180 : i32 to i64
        %181 = arith.cmpi eq, %172, %182 : i64
        cf.cond_br %181, ^bb30, ^bb31
        ^bb30:
          %184 = llvm.getelementptr %104[%175] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %183 = llvm.load %184 : !llvm.ptr -> i64
          llvm.store %183, %179 : i64, !llvm.ptr
          cf.br ^bb32
        ^bb31:
          %186 = llvm.getelementptr %104[%175] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %185 = llvm.load %186 : !llvm.ptr -> i64
          %188 = arith.constant 1 : i32
          %190 = arith.extsi %188 : i32 to i64
          %189 = arith.subi %172, %190 : i64
          %191 = llvm.getelementptr %104[%189] : (!llvm.ptr, i64) -> !llvm.ptr, i64
          %187 = llvm.load %191 : !llvm.ptr -> i64
          %192 = arith.subi %185, %187 : i64
          llvm.store %192, %179 : i64, !llvm.ptr
          cf.br ^bb32
        ^bb32:
        %193 = llvm.load %129 : !llvm.ptr -> i64
        %194 = llvm.load %142 : !llvm.ptr -> i64
        %195 = llvm.load %179 : !llvm.ptr -> i64
        %196 = arith.muli %194, %195 : i64
        %197 = arith.addi %193, %196 : i64
        llvm.store %197, %129 : i64, !llvm.ptr
        %198 = llvm.load %146 : !llvm.ptr -> i64
        %199 = arith.constant 1 : i32
        %201 = arith.extsi %199 : i32 to i64
        %200 = arith.addi %198, %201 : i64
        llvm.store %200, %133 : i64, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      %203 = llvm.load %129 : !llvm.ptr -> i64
      %202 = func.call @mod(%203) : (i64) -> i64
      %204 = llvm.load %123 : !llvm.ptr -> i64
      %205 = llvm.getelementptr %98[%204] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %202, %205 : i64, !llvm.ptr
      %208 = llvm.load %123 : !llvm.ptr -> i64
      %209 = arith.constant 1 : i32
      %211 = arith.extsi %209 : i32 to i64
      %210 = arith.subi %208, %211 : i64
      %212 = llvm.getelementptr %104[%210] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %207 = llvm.load %212 : !llvm.ptr -> i64
      %214 = llvm.load %123 : !llvm.ptr -> i64
      %215 = llvm.getelementptr %98[%214] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %213 = llvm.load %215 : !llvm.ptr -> i64
      %216 = arith.addi %207, %213 : i64
      %206 = func.call @mod(%216) : (i64) -> i64
      %217 = llvm.load %123 : !llvm.ptr -> i64
      %218 = llvm.getelementptr %104[%217] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %206, %218 : i64, !llvm.ptr
      %219 = llvm.load %123 : !llvm.ptr -> i64
      %220 = arith.constant 1 : i32
      %222 = arith.extsi %220 : i32 to i64
      %221 = arith.addi %219, %222 : i64
      llvm.store %221, %123 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %224 = llvm.getelementptr %98[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %223 = llvm.load %224 : !llvm.ptr -> i64
    func.call @free(%98) : (!llvm.ptr) -> ()
    func.call @free(%104) : (!llvm.ptr) -> ()
    func.return %223 : i64
  }
  func.func @X(%arg0: i64) -> i64 {
    %227 = arith.constant 0 : i32
    %229 = arith.extsi %227 : i32 to i64
    %228 = arith.cmpi sle, %arg0, %229 : i64
    cf.cond_br %228, ^bb33, ^bb34
    ^bb33:
      %230 = arith.constant 0 : i32
      %231 = arith.extsi %230 : i32 to i64
      func.return %231 : i64
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %233 = arith.constant 2 : i32
    %234 = arith.constant 1 : i32
    %236 = arith.extsi %234 : i32 to i64
    %235 = arith.subi %arg0, %236 : i64
    %237 = llvm.mlir.addressof @MOD : !llvm.ptr
    %238 = llvm.load %237 : !llvm.ptr -> i64
    %239 = arith.extsi %233 : i32 to i64
    %232 = func.call @powmod(%239, %235, %238) : (i64, i64, i64) -> i64
    %240 = arith.constant 0 : i32
    %241 = arith.extsi %240 : i32 to i64
    %242 = llvm.mlir.constant(1 : i64) : i64
    %243 = llvm.alloca %242 x i64 : (i64) -> !llvm.ptr
    llvm.store %241, %243 : i64, !llvm.ptr
    %244 = arith.constant 0 : i32
    %245 = arith.extsi %244 : i32 to i64
    %246 = llvm.mlir.constant(1 : i64) : i64
    %247 = llvm.alloca %246 x i64 : (i64) -> !llvm.ptr
    llvm.store %245, %247 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %248 = llvm.load %247 : !llvm.ptr -> i64
    %249 = arith.constant 14 : i32
    %251 = arith.extsi %249 : i32 to i64
    %250 = arith.cmpi slt, %248, %251 : i64
    cf.cond_br %250, ^bb37, ^bb38
    ^bb37:
      %253 = llvm.load %247 : !llvm.ptr -> i64
      %252 = func.call @S_k_value(%arg0, %253) : (i64, i64) -> i64
      %255 = arith.subi %232, %252 : i64
      %256 = llvm.mlir.addressof @MOD : !llvm.ptr
      %257 = llvm.load %256 : !llvm.ptr -> i64
      %258 = arith.remsi %255, %257 : i64
      %259 = llvm.mlir.addressof @INV2 : !llvm.ptr
      %260 = llvm.load %259 : !llvm.ptr -> i64
      %261 = arith.muli %258, %260 : i64
      %262 = llvm.mlir.addressof @MOD : !llvm.ptr
      %263 = llvm.load %262 : !llvm.ptr -> i64
      %264 = arith.remsi %261, %263 : i64
      %254 = func.call @mod(%264) : (i64) -> i64
      %265 = llvm.load %243 : !llvm.ptr -> i64
      %267 = arith.constant 2 : i32
      %268 = llvm.load %247 : !llvm.ptr -> i64
      %269 = llvm.mlir.addressof @MOD : !llvm.ptr
      %270 = llvm.load %269 : !llvm.ptr -> i64
      %271 = arith.extsi %267 : i32 to i64
      %266 = func.call @powmod(%271, %268, %270) : (i64, i64, i64) -> i64
      %272 = arith.muli %266, %254 : i64
      %273 = arith.addi %265, %272 : i64
      %274 = llvm.mlir.addressof @MOD : !llvm.ptr
      %275 = llvm.load %274 : !llvm.ptr -> i64
      %276 = arith.remsi %273, %275 : i64
      llvm.store %276, %243 : i64, !llvm.ptr
      %277 = llvm.load %247 : !llvm.ptr -> i64
      %278 = arith.constant 1 : i32
      %280 = arith.extsi %278 : i32 to i64
      %279 = arith.addi %277, %280 : i64
      llvm.store %279, %247 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %282 = llvm.load %243 : !llvm.ptr -> i64
    %283 = arith.constant 1 : i32
    %285 = arith.extsi %283 : i32 to i64
    %284 = arith.andi %arg0, %285 : i64
    %286 = arith.subi %282, %284 : i64
    %281 = func.call @mod(%286) : (i64) -> i64
    llvm.store %281, %243 : i64, !llvm.ptr
    %287 = llvm.load %243 : !llvm.ptr -> i64
    func.return %287 : i64
  }
  func.func @main() -> i32 {
    %288 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %290 = llvm.mlir.addressof @N : !llvm.ptr
    %291 = llvm.load %290 : !llvm.ptr -> i64
    %289 = func.call @X(%291) : (i64) -> i64
    %292 = llvm.call @printf(%288, %289) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %293 = arith.constant 0 : i32
    func.return %293 : i32
  }
}