← All problems
Problem 422
(a+b+c+d) mod 10^9+7 for P_{11^14} on the hyperbola.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n)O(n log n)
Space complexity O(1)O(n)
Approach Flow solution Geometric enumeration
Verdict Optimal
Flow source
# Project Euler 422
# (a+b+c+d) mod 10^9+7 for P_{11^14} on the hyperbola.
function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
let mut r: i64 = 1
let mut b: i64 = base0 % mod
if b < 0 { b = b + mod }
let mut e: i64 = exp0
while e > 0 {
if e % 2 == 1 {
r = ((r as i128) * (b as i128) % (mod as i128)) as i64
}
b = ((b as i128) * (b as i128) % (mod as i128)) as i64
e = e / 2
}
return r
}
# Return (F_n, F_{n+1}) mod mod via fast doubling.
function fib_mod(n: i64, mod: i64, out_fn: ptr<i64>, out_fn1: ptr<i64>) -> void {
if n == 0 {
out_fn[0] = 0
out_fn1[0] = 1
return
}
let mut stack_n: i64 = n
let mut depth: i64 = 0
# unwind via iterative doubling from MSB
let mut bits: i64 = 0
let mut t: i64 = n
while t > 0 {
bits = bits + 1
t = t / 2
}
let mut a: i64 = 0
let mut b: i64 = 1
let mut bit: i64 = bits - 1
while bit >= 0 {
# double: (F_k, F_{k+1}) -> (F_{2k}, F_{2k+1})
let two_b_minus_a: i64 = (2 * b - a) % mod
if two_b_minus_a < 0 { two_b_minus_a = two_b_minus_a + mod }
let c: i64 = ((a as i128) * (two_b_minus_a as i128) % (mod as i128)) as i64
let d: i64 = (((a as i128) * (a as i128) + (b as i128) * (b as i128)) % (mod as i128)) as i64
a = c
b = d
if ((n >> bit) & 1) == 1 {
let e: i64 = (a + b) % mod
a = b
b = e
}
bit = bit - 1
}
out_fn[0] = a
out_fn1[0] = b
}
function solve_mod(n: i64) -> i64 {
let MOD: i64 = 1000000007
let PHI: i64 = MOD - 1
if n == 1 { return (13 + 1 + 61 + 4) % MOD }
if n == 2 {
# (-43)/6 + (-4)/1 -> 43+6+4+1
return (43 + 6 + 4 + 1) % MOD
}
let fn_buf: i64 = 0
let fn1_buf: i64 = 0
fib_mod(n, PHI, &fn_buf, &fn1_buf)
let Fn: i64 = fn_buf
let Fn1: i64 = fn1_buf
let Fnm1: i64 = (Fn1 - Fn) % PHI
if Fnm1 < 0 { Fnm1 = Fnm1 + PHI }
let Fnm2: i64 = (Fn - Fnm1) % PHI
if Fnm2 < 0 { Fnm2 = Fnm2 + PHI }
fib_mod(n - 1, 2, &fn_buf, &fn1_buf)
let sign: i64 = 1
if fn_buf == 1 { sign = -1 }
let E: i64 = (Fn + Fnm2) % PHI
let F: i64 = Fnm1
let mut N_abs: i64 = 0
let mut D: i64 = 0
let mut gx: i64 = 0
let mut gy: i64 = 0
if n % 2 == 1 {
N_abs = modpow(2, E, MOD)
D = modpow(3, F, MOD)
gx = 12
gy = 1
} else {
N_abs = modpow(3, F, MOD)
D = modpow(2, E, MOD)
gx = 1
gy = 12
}
let mut N: i64 = N_abs
if sign == -1 { N = (MOD - N_abs) % MOD }
let N2: i64 = ((N_abs as i128) * (N_abs as i128) % (MOD as i128)) as i64
let D2: i64 = ((D as i128) * (D as i128) % (MOD as i128)) as i64
let mut num_x: i64 = (3 * N2 + 4 * D2) % MOD
let mut num_y: i64 = (4 * N2 - 3 * D2) % MOD
if num_y < 0 { num_y = num_y + MOD }
let mut den: i64 = ((N as i128) * (D as i128) % (MOD as i128)) as i64
if sign == -1 {
num_x = (MOD - num_x) % MOD
num_y = (MOD - num_y) % MOD
den = (MOD - den) % MOD
}
let inv_gx: i64 = modpow(gx, MOD - 2, MOD)
let inv_gy: i64 = modpow(gy, MOD - 2, MOD)
let a: i64 = ((num_x as i128) * (inv_gx as i128) % (MOD as i128)) as i64
let b: i64 = ((den as i128) * (inv_gx as i128) % (MOD as i128)) as i64
let c: i64 = ((num_y as i128) * (inv_gy as i128) % (MOD as i128)) as i64
let d: i64 = ((den as i128) * (inv_gy as i128) % (MOD as i128)) as i64
return (a + b + c + d) % MOD
}
function main() -> i32 {
# n = 11^14
let mut n: i64 = 1
let mut i: i64 = 0
while i < 14 {
n = n * 11
i = i + 1
}
printf("%lld\n", solve_mod(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 modpow_i64_i64_i64(int64_t base0, int64_t exp0, int64_t mod);
void fib_mod_i64_i64_ptr_i64_ptr_i64(int64_t n, int64_t mod, int64_t* out_fn, int64_t* out_fn1);
int64_t solve_mod_i64(int64_t n);
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));
if (b < 0) {
b = (b + mod);
}
int64_t e = exp0;
while (e > 0) {
if (FLOW_CHECKED_MOD((e), (2)) == 1) {
r = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))))));
}
b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))))));
e = FLOW_CHECKED_DIV((e), (2));
}
return r;
}
void fib_mod_i64_i64_ptr_i64_ptr_i64(int64_t n, int64_t mod, int64_t* out_fn, int64_t* out_fn1) {
if (n == 0) {
out_fn[0] = 0;
out_fn1[0] = 1;
return;
}
int64_t stack_n = n;
int64_t depth = 0;
int64_t bits = 0;
int64_t t = n;
while (t > 0) {
bits = (bits + 1);
t = FLOW_CHECKED_DIV((t), (2));
}
int64_t a = 0;
int64_t b = 1;
int64_t bit = (bits - 1);
while (bit >= 0) {
int64_t two_b_minus_a = FLOW_CHECKED_MOD((((2 * b) - a)), (mod));
if (two_b_minus_a < 0) {
two_b_minus_a = (two_b_minus_a + mod);
}
int64_t c = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(a)) * ((__int128)(two_b_minus_a)))), (((__int128)(mod))))));
int64_t d = ((int64_t)(FLOW_CHECKED_MOD((((((__int128)(a)) * ((__int128)(a))) + (((__int128)(b)) * ((__int128)(b))))), (((__int128)(mod))))));
a = c;
b = d;
if ((FLOW_CHECKED_SHR((n), (bit)) & 1) == 1) {
int64_t e = FLOW_CHECKED_MOD(((a + b)), (mod));
a = b;
b = e;
}
bit = (bit - 1);
}
out_fn[0] = a;
out_fn1[0] = b;
}
int64_t solve_mod_i64(int64_t n) {
int64_t MOD = 1000000007;
int64_t PHI = (MOD - 1);
if (n == 1) {
return FLOW_CHECKED_MOD(((((13 + 1) + 61) + 4)), (MOD));
}
if (n == 2) {
return FLOW_CHECKED_MOD(((((43 + 6) + 4) + 1)), (MOD));
}
int64_t fn_buf = 0;
int64_t fn1_buf = 0;
fib_mod_i64_i64_ptr_i64_ptr_i64(n, PHI, (&(fn_buf)), (&(fn1_buf)));
int64_t Fn = fn_buf;
int64_t Fn1 = fn1_buf;
int64_t Fnm1 = FLOW_CHECKED_MOD(((Fn1 - Fn)), (PHI));
if (Fnm1 < 0) {
Fnm1 = (Fnm1 + PHI);
}
int64_t Fnm2 = FLOW_CHECKED_MOD(((Fn - Fnm1)), (PHI));
if (Fnm2 < 0) {
Fnm2 = (Fnm2 + PHI);
}
fib_mod_i64_i64_ptr_i64_ptr_i64((n - 1), 2, (&(fn_buf)), (&(fn1_buf)));
int64_t sign = 1;
if (fn_buf == 1) {
sign = (-1);
}
int64_t E = FLOW_CHECKED_MOD(((Fn + Fnm2)), (PHI));
int64_t F = Fnm1;
int64_t N_abs = 0;
int64_t D = 0;
int64_t gx = 0;
int64_t gy = 0;
if (FLOW_CHECKED_MOD((n), (2)) == 1) {
N_abs = modpow_i64_i64_i64(2, E, MOD);
D = modpow_i64_i64_i64(3, F, MOD);
gx = 12;
gy = 1;
} else {
N_abs = modpow_i64_i64_i64(3, F, MOD);
D = modpow_i64_i64_i64(2, E, MOD);
gx = 1;
gy = 12;
}
int64_t N = N_abs;
if (sign == (-1)) {
N = FLOW_CHECKED_MOD(((MOD - N_abs)), (MOD));
}
int64_t N2 = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(N_abs)) * ((__int128)(N_abs)))), (((__int128)(MOD))))));
int64_t D2 = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(D)) * ((__int128)(D)))), (((__int128)(MOD))))));
int64_t num_x = FLOW_CHECKED_MOD((((3 * N2) + (4 * D2))), (MOD));
int64_t num_y = FLOW_CHECKED_MOD((((4 * N2) - (3 * D2))), (MOD));
if (num_y < 0) {
num_y = (num_y + MOD);
}
int64_t den = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(N)) * ((__int128)(D)))), (((__int128)(MOD))))));
if (sign == (-1)) {
num_x = FLOW_CHECKED_MOD(((MOD - num_x)), (MOD));
num_y = FLOW_CHECKED_MOD(((MOD - num_y)), (MOD));
den = FLOW_CHECKED_MOD(((MOD - den)), (MOD));
}
int64_t inv_gx = modpow_i64_i64_i64(gx, (MOD - 2), MOD);
int64_t inv_gy = modpow_i64_i64_i64(gy, (MOD - 2), MOD);
int64_t a = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(num_x)) * ((__int128)(inv_gx)))), (((__int128)(MOD))))));
int64_t b = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(den)) * ((__int128)(inv_gx)))), (((__int128)(MOD))))));
int64_t c = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(num_y)) * ((__int128)(inv_gy)))), (((__int128)(MOD))))));
int64_t d = ((int64_t)(FLOW_CHECKED_MOD(((((__int128)(den)) * ((__int128)(inv_gy)))), (((__int128)(MOD))))));
return FLOW_CHECKED_MOD(((((a + b) + c) + d)), (MOD));
}
int32_t main(void) {
int64_t n = 1;
int64_t i = 0;
while (i < 14) {
n = (n * 11);
i = (i + 1);
}
printf("%lld\n", solve_mod_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 @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.load %6 : !llvm.ptr -> i64
%8 = arith.constant 0 : i32
%10 = arith.extsi %8 : i32 to i64
%9 = arith.cmpi slt, %7, %10 : i64
cf.cond_br %9, ^bb0, ^bb1
^bb0:
%11 = llvm.load %6 : !llvm.ptr -> i64
%12 = arith.addi %11, %arg2 : i64
llvm.store %12, %6 : i64, !llvm.ptr
cf.br ^bb2
^bb1:
cf.br ^bb2
^bb2:
%13 = llvm.mlir.constant(1 : i64) : i64
%14 = llvm.alloca %13 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %14 : i64, !llvm.ptr
cf.br ^bb3
^bb3:
%15 = llvm.load %14 : !llvm.ptr -> i64
%16 = arith.constant 0 : i32
%18 = arith.extsi %16 : i32 to i64
%17 = arith.cmpi sgt, %15, %18 : i64
cf.cond_br %17, ^bb4, ^bb5
^bb4:
%19 = llvm.load %14 : !llvm.ptr -> i64
%20 = arith.constant 2 : i32
%22 = arith.extsi %20 : i32 to i64
%21 = arith.remsi %19, %22 : i64
%23 = arith.constant 1 : i32
%25 = arith.extsi %23 : i32 to i64
%24 = arith.cmpi eq, %21, %25 : i64
cf.cond_br %24, ^bb6, ^bb7
^bb6:
%26 = llvm.load %3 : !llvm.ptr -> i64
%27 = arith.extsi %26 : i64 to i128
%28 = llvm.load %6 : !llvm.ptr -> i64
%29 = arith.extsi %28 : i64 to i128
%31 = arith.trunci %27 : i128 to i64
%32 = arith.trunci %29 : i128 to i64
%30 = arith.muli %31, %32 : i64
%33 = arith.extsi %arg2 : i64 to i128
%35 = arith.trunci %33 : i128 to i64
%34 = arith.remsi %30, %35 : i64
llvm.store %34, %3 : i64, !llvm.ptr
cf.br ^bb8
^bb7:
cf.br ^bb8
^bb8:
%36 = llvm.load %6 : !llvm.ptr -> i64
%37 = arith.extsi %36 : i64 to i128
%38 = llvm.load %6 : !llvm.ptr -> i64
%39 = arith.extsi %38 : i64 to i128
%41 = arith.trunci %37 : i128 to i64
%42 = arith.trunci %39 : i128 to i64
%40 = arith.muli %41, %42 : i64
%43 = arith.extsi %arg2 : i64 to i128
%45 = arith.trunci %43 : i128 to i64
%44 = arith.remsi %40, %45 : i64
llvm.store %44, %6 : i64, !llvm.ptr
%46 = llvm.load %14 : !llvm.ptr -> i64
%47 = arith.constant 2 : i32
%49 = arith.extsi %47 : i32 to i64
%48 = arith.divsi %46, %49 : i64
llvm.store %48, %14 : i64, !llvm.ptr
cf.br ^bb3
^bb5:
%50 = llvm.load %3 : !llvm.ptr -> i64
func.return %50 : i64
}
func.func @fib_mod(%arg0: i64, %arg1: i64, %arg2: !llvm.ptr, %arg3: !llvm.ptr) -> () {
%51 = arith.constant 0 : i32
%53 = arith.extsi %51 : i32 to i64
%52 = arith.cmpi eq, %arg0, %53 : i64
cf.cond_br %52, ^bb9, ^bb10
^bb9:
%54 = arith.constant 0 : i32
%55 = arith.constant 0 : i32
%56 = arith.extsi %54 : i32 to i64
%57 = arith.extsi %55 : i32 to i64
%58 = llvm.getelementptr %arg2[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %56, %58 : i64, !llvm.ptr
%59 = arith.constant 1 : i32
%60 = arith.constant 0 : i32
%61 = arith.extsi %59 : i32 to i64
%62 = arith.extsi %60 : i32 to i64
%63 = llvm.getelementptr %arg3[%62] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %61, %63 : i64, !llvm.ptr
func.return
^bb10:
cf.br ^bb11
^bb11:
%64 = llvm.mlir.constant(1 : i64) : i64
%65 = llvm.alloca %64 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %65 : i64, !llvm.ptr
%66 = arith.constant 0 : i32
%67 = arith.extsi %66 : i32 to i64
%68 = llvm.mlir.constant(1 : i64) : i64
%69 = llvm.alloca %68 x i64 : (i64) -> !llvm.ptr
llvm.store %67, %69 : i64, !llvm.ptr
%70 = arith.constant 0 : i32
%71 = arith.extsi %70 : i32 to i64
%72 = llvm.mlir.constant(1 : i64) : i64
%73 = llvm.alloca %72 x i64 : (i64) -> !llvm.ptr
llvm.store %71, %73 : i64, !llvm.ptr
%74 = llvm.mlir.constant(1 : i64) : i64
%75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
llvm.store %arg0, %75 : i64, !llvm.ptr
cf.br ^bb12
^bb12:
%76 = llvm.load %75 : !llvm.ptr -> i64
%77 = arith.constant 0 : i32
%79 = arith.extsi %77 : i32 to i64
%78 = arith.cmpi sgt, %76, %79 : i64
cf.cond_br %78, ^bb13, ^bb14
^bb13:
%80 = llvm.load %73 : !llvm.ptr -> i64
%81 = arith.constant 1 : i32
%83 = arith.extsi %81 : i32 to i64
%82 = arith.addi %80, %83 : i64
llvm.store %82, %73 : i64, !llvm.ptr
%84 = llvm.load %75 : !llvm.ptr -> i64
%85 = arith.constant 2 : i32
%87 = arith.extsi %85 : i32 to i64
%86 = arith.divsi %84, %87 : i64
llvm.store %86, %75 : i64, !llvm.ptr
cf.br ^bb12
^bb14:
%88 = arith.constant 0 : i32
%89 = arith.extsi %88 : i32 to i64
%90 = llvm.mlir.constant(1 : i64) : i64
%91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
llvm.store %89, %91 : i64, !llvm.ptr
%92 = arith.constant 1 : i32
%93 = arith.extsi %92 : i32 to i64
%94 = llvm.mlir.constant(1 : i64) : i64
%95 = llvm.alloca %94 x i64 : (i64) -> !llvm.ptr
llvm.store %93, %95 : i64, !llvm.ptr
%96 = llvm.load %73 : !llvm.ptr -> i64
%97 = arith.constant 1 : i32
%99 = arith.extsi %97 : i32 to i64
%98 = arith.subi %96, %99 : i64
%100 = llvm.mlir.constant(1 : i64) : i64
%101 = llvm.alloca %100 x i64 : (i64) -> !llvm.ptr
llvm.store %98, %101 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%102 = llvm.load %101 : !llvm.ptr -> i64
%103 = arith.constant 0 : i32
%105 = arith.extsi %103 : i32 to i64
%104 = arith.cmpi sge, %102, %105 : i64
cf.cond_br %104, ^bb16, ^bb17
^bb16:
%106 = arith.constant 2 : i32
%107 = llvm.load %95 : !llvm.ptr -> i64
%109 = arith.extsi %106 : i32 to i64
%108 = arith.muli %109, %107 : i64
%110 = llvm.load %91 : !llvm.ptr -> i64
%111 = arith.subi %108, %110 : i64
%112 = arith.remsi %111, %arg1 : i64
%113 = arith.constant 0 : i32
%115 = arith.extsi %113 : i32 to i64
%114 = arith.cmpi slt, %112, %115 : i64
%116 = scf.if %114 -> (i64) {
%117 = arith.addi %112, %arg1 : i64
scf.yield %117 : i64
} else {
scf.yield %112 : i64
}
%118 = llvm.load %91 : !llvm.ptr -> i64
%119 = arith.extsi %118 : i64 to i128
%120 = arith.extsi %116 : i64 to i128
%122 = arith.trunci %119 : i128 to i64
%123 = arith.trunci %120 : i128 to i64
%121 = arith.muli %122, %123 : i64
%124 = arith.extsi %arg1 : i64 to i128
%126 = arith.trunci %124 : i128 to i64
%125 = arith.remsi %121, %126 : i64
%127 = llvm.load %91 : !llvm.ptr -> i64
%128 = arith.extsi %127 : i64 to i128
%129 = llvm.load %91 : !llvm.ptr -> i64
%130 = arith.extsi %129 : i64 to i128
%132 = arith.trunci %128 : i128 to i64
%133 = arith.trunci %130 : i128 to i64
%131 = arith.muli %132, %133 : i64
%134 = llvm.load %95 : !llvm.ptr -> i64
%135 = arith.extsi %134 : i64 to i128
%136 = llvm.load %95 : !llvm.ptr -> i64
%137 = arith.extsi %136 : i64 to i128
%139 = arith.trunci %135 : i128 to i64
%140 = arith.trunci %137 : i128 to i64
%138 = arith.muli %139, %140 : i64
%141 = arith.addi %131, %138 : i64
%142 = arith.extsi %arg1 : i64 to i128
%144 = arith.trunci %142 : i128 to i64
%143 = arith.remsi %141, %144 : i64
llvm.store %125, %91 : i64, !llvm.ptr
llvm.store %143, %95 : i64, !llvm.ptr
%145 = llvm.load %101 : !llvm.ptr -> i64
%146 = arith.shrsi %arg0, %145 : i64
%147 = arith.constant 1 : i32
%149 = arith.extsi %147 : i32 to i64
%148 = arith.andi %146, %149 : i64
%150 = arith.constant 1 : i32
%152 = arith.extsi %150 : i32 to i64
%151 = arith.cmpi eq, %148, %152 : i64
cf.cond_br %151, ^bb18, ^bb19
^bb18:
%153 = llvm.load %91 : !llvm.ptr -> i64
%154 = llvm.load %95 : !llvm.ptr -> i64
%155 = arith.addi %153, %154 : i64
%156 = arith.remsi %155, %arg1 : i64
%157 = llvm.load %95 : !llvm.ptr -> i64
llvm.store %157, %91 : i64, !llvm.ptr
llvm.store %156, %95 : i64, !llvm.ptr
cf.br ^bb20
^bb19:
cf.br ^bb20
^bb20:
%158 = llvm.load %101 : !llvm.ptr -> i64
%159 = arith.constant 1 : i32
%161 = arith.extsi %159 : i32 to i64
%160 = arith.subi %158, %161 : i64
llvm.store %160, %101 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%162 = llvm.load %91 : !llvm.ptr -> i64
%163 = arith.constant 0 : i32
%164 = arith.extsi %163 : i32 to i64
%165 = llvm.getelementptr %arg2[%164] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %162, %165 : i64, !llvm.ptr
%166 = llvm.load %95 : !llvm.ptr -> i64
%167 = arith.constant 0 : i32
%168 = arith.extsi %167 : i32 to i64
%169 = llvm.getelementptr %arg3[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %166, %169 : i64, !llvm.ptr
func.return
}
func.func @solve_mod(%arg0: i64) -> i64 {
%170 = arith.constant 1000000007 : i32
%171 = arith.extsi %170 : i32 to i64
%172 = arith.constant 1 : i32
%174 = arith.extsi %172 : i32 to i64
%173 = arith.subi %171, %174 : i64
%175 = arith.constant 1 : i32
%177 = arith.extsi %175 : i32 to i64
%176 = arith.cmpi eq, %arg0, %177 : i64
cf.cond_br %176, ^bb21, ^bb22
^bb21:
%178 = arith.constant 13 : i32
%179 = arith.constant 1 : i32
%180 = arith.addi %178, %179 : i32
%181 = arith.constant 61 : i32
%182 = arith.addi %180, %181 : i32
%183 = arith.constant 4 : i32
%184 = arith.addi %182, %183 : i32
%186 = arith.extsi %184 : i32 to i64
%185 = arith.remsi %186, %171 : i64
func.return %185 : i64
^bb22:
cf.br ^bb23
^bb23:
%187 = arith.constant 2 : i32
%189 = arith.extsi %187 : i32 to i64
%188 = arith.cmpi eq, %arg0, %189 : i64
cf.cond_br %188, ^bb24, ^bb25
^bb24:
%190 = arith.constant 43 : i32
%191 = arith.constant 6 : i32
%192 = arith.addi %190, %191 : i32
%193 = arith.constant 4 : i32
%194 = arith.addi %192, %193 : i32
%195 = arith.constant 1 : i32
%196 = arith.addi %194, %195 : i32
%198 = arith.extsi %196 : i32 to i64
%197 = arith.remsi %198, %171 : i64
func.return %197 : i64
^bb25:
cf.br ^bb26
^bb26:
%199 = arith.constant 0 : i32
%200 = arith.extsi %199 : i32 to i64
%201 = arith.constant 0 : i32
%202 = arith.extsi %201 : i32 to i64
%204 = llvm.mlir.constant(1 : i64) : i64
%205 = llvm.alloca %204 x i64 : (i64) -> !llvm.ptr
llvm.store %200, %205 : i64, !llvm.ptr
%206 = llvm.mlir.constant(1 : i64) : i64
%207 = llvm.alloca %206 x i64 : (i64) -> !llvm.ptr
llvm.store %202, %207 : i64, !llvm.ptr
func.call @fib_mod(%arg0, %173, %205, %207) : (i64, i64, !llvm.ptr, !llvm.ptr) -> ()
%208 = llvm.load %205 : !llvm.ptr -> i64
%209 = llvm.load %207 : !llvm.ptr -> i64
%210 = arith.subi %209, %208 : i64
%211 = arith.remsi %210, %173 : i64
%212 = arith.constant 0 : i32
%214 = arith.extsi %212 : i32 to i64
%213 = arith.cmpi slt, %211, %214 : i64
%215 = scf.if %213 -> (i64) {
%216 = arith.addi %211, %173 : i64
scf.yield %216 : i64
} else {
scf.yield %211 : i64
}
%217 = arith.subi %208, %215 : i64
%218 = arith.remsi %217, %173 : i64
%219 = arith.constant 0 : i32
%221 = arith.extsi %219 : i32 to i64
%220 = arith.cmpi slt, %218, %221 : i64
%222 = scf.if %220 -> (i64) {
%223 = arith.addi %218, %173 : i64
scf.yield %223 : i64
} else {
scf.yield %218 : i64
}
%225 = arith.constant 1 : i32
%227 = arith.extsi %225 : i32 to i64
%226 = arith.subi %arg0, %227 : i64
%228 = arith.constant 2 : i32
%229 = arith.extsi %228 : i32 to i64
func.call @fib_mod(%226, %229, %205, %207) : (i64, i64, !llvm.ptr, !llvm.ptr) -> ()
%230 = arith.constant 1 : i32
%231 = arith.extsi %230 : i32 to i64
%232 = llvm.load %205 : !llvm.ptr -> i64
%233 = arith.constant 1 : i32
%235 = arith.extsi %233 : i32 to i64
%234 = arith.cmpi eq, %232, %235 : i64
%236 = scf.if %234 -> (i64) {
%237 = arith.constant 1 : i32
%239 = arith.constant 0 : i32
%238 = arith.subi %239, %237 : i32
%240 = arith.extsi %238 : i32 to i64
scf.yield %240 : i64
} else {
scf.yield %231 : i64
}
%241 = arith.addi %208, %222 : i64
%242 = arith.remsi %241, %173 : i64
%243 = arith.constant 0 : i32
%244 = arith.extsi %243 : i32 to i64
%245 = llvm.mlir.constant(1 : i64) : i64
%246 = llvm.alloca %245 x i64 : (i64) -> !llvm.ptr
llvm.store %244, %246 : i64, !llvm.ptr
%247 = arith.constant 0 : i32
%248 = arith.extsi %247 : i32 to i64
%249 = llvm.mlir.constant(1 : i64) : i64
%250 = llvm.alloca %249 x i64 : (i64) -> !llvm.ptr
llvm.store %248, %250 : i64, !llvm.ptr
%251 = arith.constant 0 : i32
%252 = arith.extsi %251 : i32 to i64
%253 = llvm.mlir.constant(1 : i64) : i64
%254 = llvm.alloca %253 x i64 : (i64) -> !llvm.ptr
llvm.store %252, %254 : i64, !llvm.ptr
%255 = arith.constant 0 : i32
%256 = arith.extsi %255 : i32 to i64
%257 = llvm.mlir.constant(1 : i64) : i64
%258 = llvm.alloca %257 x i64 : (i64) -> !llvm.ptr
llvm.store %256, %258 : i64, !llvm.ptr
%259 = arith.constant 2 : i32
%261 = arith.extsi %259 : i32 to i64
%260 = arith.remsi %arg0, %261 : i64
%262 = arith.constant 1 : i32
%264 = arith.extsi %262 : i32 to i64
%263 = arith.cmpi eq, %260, %264 : i64
cf.cond_br %263, ^bb27, ^bb28
^bb27:
%266 = arith.constant 2 : i32
%267 = arith.extsi %266 : i32 to i64
%265 = func.call @modpow(%267, %242, %171) : (i64, i64, i64) -> i64
llvm.store %265, %246 : i64, !llvm.ptr
%269 = arith.constant 3 : i32
%270 = arith.extsi %269 : i32 to i64
%268 = func.call @modpow(%270, %215, %171) : (i64, i64, i64) -> i64
llvm.store %268, %250 : i64, !llvm.ptr
%271 = arith.constant 12 : i32
%272 = arith.extsi %271 : i32 to i64
llvm.store %272, %254 : i64, !llvm.ptr
%273 = arith.constant 1 : i32
%274 = arith.extsi %273 : i32 to i64
llvm.store %274, %258 : i64, !llvm.ptr
cf.br ^bb29
^bb28:
%276 = arith.constant 3 : i32
%277 = arith.extsi %276 : i32 to i64
%275 = func.call @modpow(%277, %215, %171) : (i64, i64, i64) -> i64
llvm.store %275, %246 : i64, !llvm.ptr
%279 = arith.constant 2 : i32
%280 = arith.extsi %279 : i32 to i64
%278 = func.call @modpow(%280, %242, %171) : (i64, i64, i64) -> i64
llvm.store %278, %250 : i64, !llvm.ptr
%281 = arith.constant 1 : i32
%282 = arith.extsi %281 : i32 to i64
llvm.store %282, %254 : i64, !llvm.ptr
%283 = arith.constant 12 : i32
%284 = arith.extsi %283 : i32 to i64
llvm.store %284, %258 : i64, !llvm.ptr
cf.br ^bb29
^bb29:
%285 = llvm.load %246 : !llvm.ptr -> i64
%286 = llvm.mlir.constant(1 : i64) : i64
%287 = llvm.alloca %286 x i64 : (i64) -> !llvm.ptr
llvm.store %285, %287 : i64, !llvm.ptr
%288 = arith.constant 1 : i32
%290 = arith.constant 0 : i32
%289 = arith.subi %290, %288 : i32
%292 = arith.extsi %289 : i32 to i64
%291 = arith.cmpi eq, %236, %292 : i64
cf.cond_br %291, ^bb30, ^bb31
^bb30:
%293 = llvm.load %246 : !llvm.ptr -> i64
%294 = arith.subi %171, %293 : i64
%295 = arith.remsi %294, %171 : i64
llvm.store %295, %287 : i64, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
%296 = llvm.load %246 : !llvm.ptr -> i64
%297 = arith.extsi %296 : i64 to i128
%298 = llvm.load %246 : !llvm.ptr -> i64
%299 = arith.extsi %298 : i64 to i128
%301 = arith.trunci %297 : i128 to i64
%302 = arith.trunci %299 : i128 to i64
%300 = arith.muli %301, %302 : i64
%303 = arith.extsi %171 : i64 to i128
%305 = arith.trunci %303 : i128 to i64
%304 = arith.remsi %300, %305 : i64
%306 = llvm.load %250 : !llvm.ptr -> i64
%307 = arith.extsi %306 : i64 to i128
%308 = llvm.load %250 : !llvm.ptr -> i64
%309 = arith.extsi %308 : i64 to i128
%311 = arith.trunci %307 : i128 to i64
%312 = arith.trunci %309 : i128 to i64
%310 = arith.muli %311, %312 : i64
%313 = arith.extsi %171 : i64 to i128
%315 = arith.trunci %313 : i128 to i64
%314 = arith.remsi %310, %315 : i64
%316 = arith.constant 3 : i32
%318 = arith.extsi %316 : i32 to i64
%317 = arith.muli %318, %304 : i64
%319 = arith.constant 4 : i32
%321 = arith.extsi %319 : i32 to i64
%320 = arith.muli %321, %314 : i64
%322 = arith.addi %317, %320 : i64
%323 = arith.remsi %322, %171 : i64
%324 = llvm.mlir.constant(1 : i64) : i64
%325 = llvm.alloca %324 x i64 : (i64) -> !llvm.ptr
llvm.store %323, %325 : i64, !llvm.ptr
%326 = arith.constant 4 : i32
%328 = arith.extsi %326 : i32 to i64
%327 = arith.muli %328, %304 : i64
%329 = arith.constant 3 : i32
%331 = arith.extsi %329 : i32 to i64
%330 = arith.muli %331, %314 : i64
%332 = arith.subi %327, %330 : i64
%333 = arith.remsi %332, %171 : i64
%334 = llvm.mlir.constant(1 : i64) : i64
%335 = llvm.alloca %334 x i64 : (i64) -> !llvm.ptr
llvm.store %333, %335 : i64, !llvm.ptr
%336 = llvm.load %335 : !llvm.ptr -> i64
%337 = arith.constant 0 : i32
%339 = arith.extsi %337 : i32 to i64
%338 = arith.cmpi slt, %336, %339 : i64
cf.cond_br %338, ^bb33, ^bb34
^bb33:
%340 = llvm.load %335 : !llvm.ptr -> i64
%341 = arith.addi %340, %171 : i64
llvm.store %341, %335 : i64, !llvm.ptr
cf.br ^bb35
^bb34:
cf.br ^bb35
^bb35:
%342 = llvm.load %287 : !llvm.ptr -> i64
%343 = arith.extsi %342 : i64 to i128
%344 = llvm.load %250 : !llvm.ptr -> i64
%345 = arith.extsi %344 : i64 to i128
%347 = arith.trunci %343 : i128 to i64
%348 = arith.trunci %345 : i128 to i64
%346 = arith.muli %347, %348 : i64
%349 = arith.extsi %171 : i64 to i128
%351 = arith.trunci %349 : i128 to i64
%350 = arith.remsi %346, %351 : i64
%352 = llvm.mlir.constant(1 : i64) : i64
%353 = llvm.alloca %352 x i64 : (i64) -> !llvm.ptr
llvm.store %350, %353 : i64, !llvm.ptr
%354 = arith.constant 1 : i32
%356 = arith.constant 0 : i32
%355 = arith.subi %356, %354 : i32
%358 = arith.extsi %355 : i32 to i64
%357 = arith.cmpi eq, %236, %358 : i64
cf.cond_br %357, ^bb36, ^bb37
^bb36:
%359 = llvm.load %325 : !llvm.ptr -> i64
%360 = arith.subi %171, %359 : i64
%361 = arith.remsi %360, %171 : i64
llvm.store %361, %325 : i64, !llvm.ptr
%362 = llvm.load %335 : !llvm.ptr -> i64
%363 = arith.subi %171, %362 : i64
%364 = arith.remsi %363, %171 : i64
llvm.store %364, %335 : i64, !llvm.ptr
%365 = llvm.load %353 : !llvm.ptr -> i64
%366 = arith.subi %171, %365 : i64
%367 = arith.remsi %366, %171 : i64
llvm.store %367, %353 : i64, !llvm.ptr
cf.br ^bb38
^bb37:
cf.br ^bb38
^bb38:
%369 = llvm.load %254 : !llvm.ptr -> i64
%370 = arith.constant 2 : i32
%372 = arith.extsi %370 : i32 to i64
%371 = arith.subi %171, %372 : i64
%368 = func.call @modpow(%369, %371, %171) : (i64, i64, i64) -> i64
%374 = llvm.load %258 : !llvm.ptr -> i64
%375 = arith.constant 2 : i32
%377 = arith.extsi %375 : i32 to i64
%376 = arith.subi %171, %377 : i64
%373 = func.call @modpow(%374, %376, %171) : (i64, i64, i64) -> i64
%378 = llvm.load %325 : !llvm.ptr -> i64
%379 = arith.extsi %378 : i64 to i128
%380 = arith.extsi %368 : i64 to i128
%382 = arith.trunci %379 : i128 to i64
%383 = arith.trunci %380 : i128 to i64
%381 = arith.muli %382, %383 : i64
%384 = arith.extsi %171 : i64 to i128
%386 = arith.trunci %384 : i128 to i64
%385 = arith.remsi %381, %386 : i64
%387 = llvm.load %353 : !llvm.ptr -> i64
%388 = arith.extsi %387 : i64 to i128
%389 = arith.extsi %368 : i64 to i128
%391 = arith.trunci %388 : i128 to i64
%392 = arith.trunci %389 : i128 to i64
%390 = arith.muli %391, %392 : i64
%393 = arith.extsi %171 : i64 to i128
%395 = arith.trunci %393 : i128 to i64
%394 = arith.remsi %390, %395 : i64
%396 = llvm.load %335 : !llvm.ptr -> i64
%397 = arith.extsi %396 : i64 to i128
%398 = arith.extsi %373 : i64 to i128
%400 = arith.trunci %397 : i128 to i64
%401 = arith.trunci %398 : i128 to i64
%399 = arith.muli %400, %401 : i64
%402 = arith.extsi %171 : i64 to i128
%404 = arith.trunci %402 : i128 to i64
%403 = arith.remsi %399, %404 : i64
%405 = llvm.load %353 : !llvm.ptr -> i64
%406 = arith.extsi %405 : i64 to i128
%407 = arith.extsi %373 : i64 to i128
%409 = arith.trunci %406 : i128 to i64
%410 = arith.trunci %407 : i128 to i64
%408 = arith.muli %409, %410 : i64
%411 = arith.extsi %171 : i64 to i128
%413 = arith.trunci %411 : i128 to i64
%412 = arith.remsi %408, %413 : i64
%414 = arith.addi %385, %394 : i64
%415 = arith.addi %414, %403 : i64
%416 = arith.addi %415, %412 : i64
%417 = arith.remsi %416, %171 : i64
func.return %417 : i64
}
func.func @main() -> i32 {
%418 = arith.constant 1 : i32
%419 = arith.extsi %418 : i32 to i64
%420 = llvm.mlir.constant(1 : i64) : i64
%421 = llvm.alloca %420 x i64 : (i64) -> !llvm.ptr
llvm.store %419, %421 : i64, !llvm.ptr
%422 = arith.constant 0 : i32
%423 = arith.extsi %422 : i32 to i64
%424 = llvm.mlir.constant(1 : i64) : i64
%425 = llvm.alloca %424 x i64 : (i64) -> !llvm.ptr
llvm.store %423, %425 : i64, !llvm.ptr
cf.br ^bb39
^bb39:
%426 = llvm.load %425 : !llvm.ptr -> i64
%427 = arith.constant 14 : i32
%429 = arith.extsi %427 : i32 to i64
%428 = arith.cmpi slt, %426, %429 : i64
cf.cond_br %428, ^bb40, ^bb41
^bb40:
%430 = llvm.load %421 : !llvm.ptr -> i64
%431 = arith.constant 11 : i32
%433 = arith.extsi %431 : i32 to i64
%432 = arith.muli %430, %433 : i64
llvm.store %432, %421 : i64, !llvm.ptr
%434 = llvm.load %425 : !llvm.ptr -> i64
%435 = arith.constant 1 : i32
%437 = arith.extsi %435 : i32 to i64
%436 = arith.addi %434, %437 : i64
llvm.store %436, %425 : i64, !llvm.ptr
cf.br ^bb39
^bb41:
%438 = llvm.mlir.addressof @str_0 : !llvm.ptr
%440 = llvm.load %421 : !llvm.ptr -> i64
%439 = func.call @solve_mod(%440) : (i64) -> i64
%441 = llvm.call @printf(%438, %439) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%442 = arith.constant 0 : i32
func.return %442 : i32
}
}