← All problems
Problem 1004
Balanced Integer: count integers whose longest strictly decreasing subsequence equals longest non-strictly increasing subsequence, mod 1e9+7. Uses hook-length / RSK correspondence: balanced <=> square Young diagrams. Count = sum of f^shape * s_shape(1^10) over square shapes, minus those ending in 9.
View problem on Project Euler
Performance comparison
Metric Our solution Best known
Time complexity O(n^2)O(n log log n)
Space complexity O(1)O(n)
Approach Flow solution Sieve or enumeration
Verdict Suboptimal
Flow source
# Project Euler 1004
# Balanced Integer: count integers whose longest strictly decreasing subsequence
# equals longest non-strictly increasing subsequence, mod 1e9+7.
# Uses hook-length / RSK correspondence: balanced <=> square Young diagrams.
# Count = sum of f^shape * s_shape(1^10) over square shapes, minus those ending in 9.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
}
const MOD: i64 = 1000000007
const ALPHA: i64 = 10
const MAX_SIDE: i64 = 10
function mod_pow(base: i64, exp: i64, m: i64) -> i64 {
let mut result: i64 = 1
let mut b: i64 = base % m
let mut e: i64 = exp
while e > 0 {
if (e & 1) == 1 {
result = result * b % m
}
b = b * b % m
e = e >> 1
}
return result
}
function mod_inv(a: i64, m: i64) -> i64 {
return mod_pow(a % m, m - 2, m)
}
# Global state for partition enumeration and accumulation
let mut shape: ptr<i64> = null
let mut factorial: ptr<i64> = null
let mut balanced_all: i64 = 0
let mut balanced_ending_nine: i64 = 0
# Compute shape weight mod MOD: f^shape * s_shape(1^ALPHA)
function shape_weight_mod(len: i64) -> i64 {
# Column heights
let col_h: ptr<i64> = calloc(11, 8)
let mut c: i64 = 0
while c < shape[0] {
col_h[c] = 0
let mut r: i64 = 0
while r < len {
if shape[r] >= c + 1 {
col_h[c] = col_h[c] + 1
}
r = r + 1
}
c = c + 1
}
let mut num: i64 = factorial[0]
let mut s: i64 = 0
let mut r: i64 = 0
while r < len {
s = s + shape[r]
r = r + 1
}
num = factorial[s]
let mut den: i64 = 1
let mut ri: i64 = 0
while ri < len {
let mut ci: i64 = 0
while ci < shape[ri] {
let hook: i64 = shape[ri] - ci + col_h[ci] - ri - 1
let content: i64 = ALPHA + ci - ri
num = num * content % MOD
den = den * hook % MOD
den = den * hook % MOD
ci = ci + 1
}
ri = ri + 1
}
free(col_h)
return num * mod_inv(den, MOD) % MOD
}
# Recursively enumerate partitions with parts <= limit, at most max_len parts
function enum_partitions(pos: i64, limit: i64, max_len: i64) -> void {
if pos > 0 {
# Process current partition
let len: i64 = pos
let size: i64 = 0
let mut r: i64 = 0
while r < len {
size = size + shape[r]
r = r + 1
}
let width: i64 = shape[0]
let height: i64 = len
let w: i64 = shape_weight_mod(len)
if size <= MAX_SIDE * MAX_SIDE && width == height {
balanced_all = (balanced_all + w) % MOD
}
if size + 1 <= MAX_SIDE * MAX_SIDE && height == width + 1 {
balanced_ending_nine = (balanced_ending_nine + w) % MOD
}
}
if max_len == 0 {
return
}
let mut part: i64 = limit
while part >= 1 {
shape[pos] = part
enum_partitions(pos + 1, part, max_len - 1)
part = part - 1
}
}
function main() -> i32 {
shape = calloc(16, 8)
factorial = calloc(101, 8)
# Precompute factorials mod MOD
factorial[0] = 1
let mut i: i64 = 1
while i <= 100 {
factorial[i] = factorial[i - 1] * i % MOD
i = i + 1
}
# balanced_ending_nine starts at 1 (the single digit 9)
balanced_ending_nine = 1
enum_partitions(0, MAX_SIDE, MAX_SIDE)
let answer: i64 = (balanced_all - balanced_ending_nine % MOD + MOD) % MOD
printf("%lld\n", answer)
free(shape)
free(factorial)
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_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t m);
int64_t mod_inv_i64_i64(int64_t a, int64_t m);
int64_t shape_weight_mod_i64(int64_t len);
void enum_partitions_i64_i64_i64(int64_t pos, int64_t limit, int64_t max_len);
int32_t main(void);
static const int64_t MOD = 1000000007;
static const int64_t ALPHA = 10;
static const int64_t MAX_SIDE = 10;
/* Module statics */
static int64_t* shape = NULL;
static int64_t* factorial = NULL;
static int64_t balanced_all = 0;
static int64_t balanced_ending_nine = 0;
int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t m) {
int64_t result = 1;
int64_t b = FLOW_CHECKED_MOD((base), (m));
int64_t e = exp;
while (e > 0) {
if ((e & 1) == 1) {
result = FLOW_CHECKED_MOD(((result * b)), (m));
}
b = FLOW_CHECKED_MOD(((b * b)), (m));
e = FLOW_CHECKED_SHR((e), (1));
}
return result;
}
int64_t mod_inv_i64_i64(int64_t a, int64_t m) {
return mod_pow_i64_i64_i64(FLOW_CHECKED_MOD((a), (m)), (m - 2), m);
}
int64_t shape_weight_mod_i64(int64_t len) {
int64_t* col_h = (int64_t*)(calloc(11, 8));
int64_t c = 0;
while (c < shape[0]) {
col_h[c] = 0;
int64_t r = 0;
while (r < len) {
if (shape[r] >= (c + 1)) {
col_h[c] = (col_h[c] + 1);
}
r = (r + 1);
}
c = (c + 1);
}
int64_t num = factorial[0];
int64_t s = 0;
int64_t r = 0;
while (r < len) {
s = (s + shape[r]);
r = (r + 1);
}
num = factorial[s];
int64_t den = 1;
int64_t ri = 0;
while (ri < len) {
int64_t ci = 0;
while (ci < shape[ri]) {
int64_t hook = ((((shape[ri] - ci) + col_h[ci]) - ri) - 1);
int64_t content = ((ALPHA + ci) - ri);
num = FLOW_CHECKED_MOD(((num * content)), (MOD));
den = FLOW_CHECKED_MOD(((den * hook)), (MOD));
den = FLOW_CHECKED_MOD(((den * hook)), (MOD));
ci = (ci + 1);
}
ri = (ri + 1);
}
free(col_h);
return FLOW_CHECKED_MOD(((num * mod_inv_i64_i64(den, MOD))), (MOD));
}
void enum_partitions_i64_i64_i64(int64_t pos, int64_t limit, int64_t max_len) {
if (pos > 0) {
int64_t len = pos;
int64_t size = 0;
int64_t r = 0;
while (r < len) {
size = (size + shape[r]);
r = (r + 1);
}
int64_t width = shape[0];
int64_t height = len;
int64_t w = shape_weight_mod_i64(len);
if ((size <= (MAX_SIDE * MAX_SIDE) && width == height)) {
balanced_all = FLOW_CHECKED_MOD(((balanced_all + w)), (MOD));
}
if (((size + 1) <= (MAX_SIDE * MAX_SIDE) && height == (width + 1))) {
balanced_ending_nine = FLOW_CHECKED_MOD(((balanced_ending_nine + w)), (MOD));
}
}
if (max_len == 0) {
return;
}
int64_t part = limit;
while (part >= 1) {
shape[pos] = part;
enum_partitions_i64_i64_i64((pos + 1), part, (max_len - 1));
part = (part - 1);
}
}
int32_t main(void) {
shape = calloc(16, 8);
factorial = calloc(101, 8);
factorial[0] = 1;
int64_t i = 1;
while (i <= 100) {
factorial[i] = FLOW_CHECKED_MOD(((factorial[(i - 1)] * i)), (MOD));
i = (i + 1);
}
balanced_ending_nine = 1;
enum_partitions_i64_i64_i64(0, MAX_SIDE, MAX_SIDE);
int64_t answer = FLOW_CHECKED_MOD((((balanced_all - FLOW_CHECKED_MOD((balanced_ending_nine), (MOD))) + MOD)), (MOD));
printf("%lld\n", answer);
free(shape);
free(factorial);
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: ALPHA
llvm.mlir.global internal constant @ALPHA(10 : i64) : i64
// Constant: MAX_SIDE
llvm.mlir.global internal constant @MAX_SIDE(10 : i64) : i64
func.func @mod_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
%0 = arith.constant 1 : i32
%1 = arith.extsi %0 : i32 to i64
%2 = llvm.mlir.constant(1 : i64) : i64
%3 = llvm.alloca %2 x i64 : (i64) -> !llvm.ptr
llvm.store %1, %3 : i64, !llvm.ptr
%4 = arith.remsi %arg0, %arg2 : i64
%5 = llvm.mlir.constant(1 : i64) : i64
%6 = llvm.alloca %5 x i64 : (i64) -> !llvm.ptr
llvm.store %4, %6 : i64, !llvm.ptr
%7 = llvm.mlir.constant(1 : i64) : i64
%8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb0:
%9 = llvm.load %8 : !llvm.ptr -> i64
%10 = arith.constant 0 : i32
%12 = arith.extsi %10 : i32 to i64
%11 = arith.cmpi sgt, %9, %12 : i64
cf.cond_br %11, ^bb1, ^bb2
^bb1:
%13 = llvm.load %8 : !llvm.ptr -> i64
%14 = arith.constant 1 : i32
%16 = arith.extsi %14 : i32 to i64
%15 = arith.andi %13, %16 : i64
%17 = arith.constant 1 : i32
%19 = arith.extsi %17 : i32 to i64
%18 = arith.cmpi eq, %15, %19 : i64
cf.cond_br %18, ^bb3, ^bb4
^bb3:
%20 = llvm.load %3 : !llvm.ptr -> i64
%21 = llvm.load %6 : !llvm.ptr -> i64
%22 = arith.muli %20, %21 : i64
%23 = arith.remsi %22, %arg2 : i64
llvm.store %23, %3 : i64, !llvm.ptr
cf.br ^bb5
^bb4:
cf.br ^bb5
^bb5:
%24 = llvm.load %6 : !llvm.ptr -> i64
%25 = llvm.load %6 : !llvm.ptr -> i64
%26 = arith.muli %24, %25 : i64
%27 = arith.remsi %26, %arg2 : i64
llvm.store %27, %6 : i64, !llvm.ptr
%28 = llvm.load %8 : !llvm.ptr -> i64
%29 = arith.constant 1 : i32
%31 = arith.extsi %29 : i32 to i64
%30 = arith.shrsi %28, %31 : i64
llvm.store %30, %8 : i64, !llvm.ptr
cf.br ^bb0
^bb2:
%32 = llvm.load %3 : !llvm.ptr -> i64
func.return %32 : i64
}
func.func @mod_inv(%arg0: i64, %arg1: i64) -> i64 {
%34 = arith.remsi %arg0, %arg1 : i64
%35 = arith.constant 2 : i32
%37 = arith.extsi %35 : i32 to i64
%36 = arith.subi %arg1, %37 : i64
%33 = func.call @mod_pow(%34, %36, %arg1) : (i64, i64, i64) -> i64
func.return %33 : i64
}
// Module static: shape
llvm.mlir.global internal @shape() {addr_space = 0 : i32} : !llvm.ptr {
%38 = llvm.mlir.zero : !llvm.ptr
llvm.return %38 : !llvm.ptr
}
// Module static: factorial
llvm.mlir.global internal @factorial() {addr_space = 0 : i32} : !llvm.ptr {
%39 = llvm.mlir.zero : !llvm.ptr
llvm.return %39 : !llvm.ptr
}
// Module static: balanced_all
llvm.mlir.global internal @balanced_all(0 : i64) : i64
// Module static: balanced_ending_nine
llvm.mlir.global internal @balanced_ending_nine(0 : i64) : i64
func.func @shape_weight_mod(%arg0: i64) -> i64 {
%41 = arith.constant 11 : i32
%42 = arith.constant 8 : i32
%43 = arith.extsi %41 : i32 to i64
%44 = arith.extsi %42 : i32 to i64
%40 = func.call @calloc(%43, %44) : (i64, i64) -> !llvm.ptr
%45 = arith.constant 0 : i32
%46 = arith.extsi %45 : i32 to i64
%47 = llvm.mlir.constant(1 : i64) : i64
%48 = llvm.alloca %47 x i64 : (i64) -> !llvm.ptr
llvm.store %46, %48 : i64, !llvm.ptr
cf.br ^bb6
^bb6:
%49 = llvm.load %48 : !llvm.ptr -> i64
%51 = llvm.mlir.addressof @shape : !llvm.ptr
%52 = llvm.load %51 : !llvm.ptr -> !llvm.ptr
%53 = arith.constant 0 : i32
%54 = arith.extsi %53 : i32 to i64
%55 = llvm.getelementptr %52[%54] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%50 = llvm.load %55 : !llvm.ptr -> i64
%56 = arith.cmpi slt, %49, %50 : i64
cf.cond_br %56, ^bb7, ^bb8
^bb7:
%57 = arith.constant 0 : i32
%58 = llvm.load %48 : !llvm.ptr -> i64
%59 = arith.extsi %57 : i32 to i64
%60 = llvm.getelementptr %40[%58] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %59, %60 : i64, !llvm.ptr
%61 = arith.constant 0 : i32
%62 = arith.extsi %61 : i32 to i64
%63 = llvm.mlir.constant(1 : i64) : i64
%64 = llvm.alloca %63 x i64 : (i64) -> !llvm.ptr
llvm.store %62, %64 : i64, !llvm.ptr
cf.br ^bb9
^bb9:
%65 = llvm.load %64 : !llvm.ptr -> i64
%66 = arith.cmpi slt, %65, %arg0 : i64
cf.cond_br %66, ^bb10, ^bb11
^bb10:
%68 = llvm.mlir.addressof @shape : !llvm.ptr
%69 = llvm.load %68 : !llvm.ptr -> !llvm.ptr
%70 = llvm.load %64 : !llvm.ptr -> i64
%71 = llvm.getelementptr %69[%70] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%67 = llvm.load %71 : !llvm.ptr -> i64
%72 = llvm.load %48 : !llvm.ptr -> i64
%73 = arith.constant 1 : i32
%75 = arith.extsi %73 : i32 to i64
%74 = arith.addi %72, %75 : i64
%76 = arith.cmpi sge, %67, %74 : i64
cf.cond_br %76, ^bb12, ^bb13
^bb12:
%78 = llvm.load %48 : !llvm.ptr -> i64
%79 = llvm.getelementptr %40[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%77 = llvm.load %79 : !llvm.ptr -> i64
%80 = arith.constant 1 : i32
%82 = arith.extsi %80 : i32 to i64
%81 = arith.addi %77, %82 : i64
%83 = llvm.load %48 : !llvm.ptr -> i64
%84 = llvm.getelementptr %40[%83] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %81, %84 : i64, !llvm.ptr
cf.br ^bb14
^bb13:
cf.br ^bb14
^bb14:
%85 = llvm.load %64 : !llvm.ptr -> i64
%86 = arith.constant 1 : i32
%88 = arith.extsi %86 : i32 to i64
%87 = arith.addi %85, %88 : i64
llvm.store %87, %64 : i64, !llvm.ptr
cf.br ^bb9
^bb11:
%89 = llvm.load %48 : !llvm.ptr -> i64
%90 = arith.constant 1 : i32
%92 = arith.extsi %90 : i32 to i64
%91 = arith.addi %89, %92 : i64
llvm.store %91, %48 : i64, !llvm.ptr
cf.br ^bb6
^bb8:
%94 = llvm.mlir.addressof @factorial : !llvm.ptr
%95 = llvm.load %94 : !llvm.ptr -> !llvm.ptr
%96 = arith.constant 0 : i32
%97 = arith.extsi %96 : i32 to i64
%98 = llvm.getelementptr %95[%97] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%93 = llvm.load %98 : !llvm.ptr -> i64
%99 = llvm.mlir.constant(1 : i64) : i64
%100 = llvm.alloca %99 x i64 : (i64) -> !llvm.ptr
llvm.store %93, %100 : i64, !llvm.ptr
%101 = arith.constant 0 : i32
%102 = arith.extsi %101 : i32 to i64
%103 = llvm.mlir.constant(1 : i64) : i64
%104 = llvm.alloca %103 x i64 : (i64) -> !llvm.ptr
llvm.store %102, %104 : i64, !llvm.ptr
%105 = arith.constant 0 : i32
%106 = arith.extsi %105 : i32 to i64
%107 = llvm.mlir.constant(1 : i64) : i64
%108 = llvm.alloca %107 x i64 : (i64) -> !llvm.ptr
llvm.store %106, %108 : i64, !llvm.ptr
cf.br ^bb15
^bb15:
%109 = llvm.load %108 : !llvm.ptr -> i64
%110 = arith.cmpi slt, %109, %arg0 : i64
cf.cond_br %110, ^bb16, ^bb17
^bb16:
%111 = llvm.load %104 : !llvm.ptr -> i64
%113 = llvm.mlir.addressof @shape : !llvm.ptr
%114 = llvm.load %113 : !llvm.ptr -> !llvm.ptr
%115 = llvm.load %108 : !llvm.ptr -> i64
%116 = llvm.getelementptr %114[%115] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%112 = llvm.load %116 : !llvm.ptr -> i64
%117 = arith.addi %111, %112 : i64
llvm.store %117, %104 : i64, !llvm.ptr
%118 = llvm.load %108 : !llvm.ptr -> i64
%119 = arith.constant 1 : i32
%121 = arith.extsi %119 : i32 to i64
%120 = arith.addi %118, %121 : i64
llvm.store %120, %108 : i64, !llvm.ptr
cf.br ^bb15
^bb17:
%123 = llvm.mlir.addressof @factorial : !llvm.ptr
%124 = llvm.load %123 : !llvm.ptr -> !llvm.ptr
%125 = llvm.load %104 : !llvm.ptr -> i64
%126 = llvm.getelementptr %124[%125] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%122 = llvm.load %126 : !llvm.ptr -> i64
llvm.store %122, %100 : i64, !llvm.ptr
%127 = arith.constant 1 : i32
%128 = arith.extsi %127 : i32 to i64
%129 = llvm.mlir.constant(1 : i64) : i64
%130 = llvm.alloca %129 x i64 : (i64) -> !llvm.ptr
llvm.store %128, %130 : i64, !llvm.ptr
%131 = arith.constant 0 : i32
%132 = arith.extsi %131 : i32 to i64
%133 = llvm.mlir.constant(1 : i64) : i64
%134 = llvm.alloca %133 x i64 : (i64) -> !llvm.ptr
llvm.store %132, %134 : i64, !llvm.ptr
cf.br ^bb18
^bb18:
%135 = llvm.load %134 : !llvm.ptr -> i64
%136 = arith.cmpi slt, %135, %arg0 : i64
cf.cond_br %136, ^bb19, ^bb20
^bb19:
%137 = arith.constant 0 : i32
%138 = arith.extsi %137 : i32 to i64
%139 = llvm.mlir.constant(1 : i64) : i64
%140 = llvm.alloca %139 x i64 : (i64) -> !llvm.ptr
llvm.store %138, %140 : i64, !llvm.ptr
cf.br ^bb21
^bb21:
%141 = llvm.load %140 : !llvm.ptr -> i64
%143 = llvm.mlir.addressof @shape : !llvm.ptr
%144 = llvm.load %143 : !llvm.ptr -> !llvm.ptr
%145 = llvm.load %134 : !llvm.ptr -> i64
%146 = llvm.getelementptr %144[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%142 = llvm.load %146 : !llvm.ptr -> i64
%147 = arith.cmpi slt, %141, %142 : i64
cf.cond_br %147, ^bb22, ^bb23
^bb22:
%149 = llvm.mlir.addressof @shape : !llvm.ptr
%150 = llvm.load %149 : !llvm.ptr -> !llvm.ptr
%151 = llvm.load %134 : !llvm.ptr -> i64
%152 = llvm.getelementptr %150[%151] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%148 = llvm.load %152 : !llvm.ptr -> i64
%153 = llvm.load %140 : !llvm.ptr -> i64
%154 = arith.subi %148, %153 : i64
%156 = llvm.load %140 : !llvm.ptr -> i64
%157 = llvm.getelementptr %40[%156] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%155 = llvm.load %157 : !llvm.ptr -> i64
%158 = arith.addi %154, %155 : i64
%159 = llvm.load %134 : !llvm.ptr -> i64
%160 = arith.subi %158, %159 : i64
%161 = arith.constant 1 : i32
%163 = arith.extsi %161 : i32 to i64
%162 = arith.subi %160, %163 : i64
%164 = llvm.mlir.addressof @ALPHA : !llvm.ptr
%165 = llvm.load %164 : !llvm.ptr -> i64
%166 = llvm.load %140 : !llvm.ptr -> i64
%167 = arith.addi %165, %166 : i64
%168 = llvm.load %134 : !llvm.ptr -> i64
%169 = arith.subi %167, %168 : i64
%170 = llvm.load %100 : !llvm.ptr -> i64
%171 = arith.muli %170, %169 : i64
%172 = llvm.mlir.addressof @MOD : !llvm.ptr
%173 = llvm.load %172 : !llvm.ptr -> i64
%174 = arith.remsi %171, %173 : i64
llvm.store %174, %100 : i64, !llvm.ptr
%175 = llvm.load %130 : !llvm.ptr -> i64
%176 = arith.muli %175, %162 : i64
%177 = llvm.mlir.addressof @MOD : !llvm.ptr
%178 = llvm.load %177 : !llvm.ptr -> i64
%179 = arith.remsi %176, %178 : i64
llvm.store %179, %130 : i64, !llvm.ptr
%180 = llvm.load %130 : !llvm.ptr -> i64
%181 = arith.muli %180, %162 : i64
%182 = llvm.mlir.addressof @MOD : !llvm.ptr
%183 = llvm.load %182 : !llvm.ptr -> i64
%184 = arith.remsi %181, %183 : i64
llvm.store %184, %130 : i64, !llvm.ptr
%185 = llvm.load %140 : !llvm.ptr -> i64
%186 = arith.constant 1 : i32
%188 = arith.extsi %186 : i32 to i64
%187 = arith.addi %185, %188 : i64
llvm.store %187, %140 : i64, !llvm.ptr
cf.br ^bb21
^bb23:
%189 = llvm.load %134 : !llvm.ptr -> i64
%190 = arith.constant 1 : i32
%192 = arith.extsi %190 : i32 to i64
%191 = arith.addi %189, %192 : i64
llvm.store %191, %134 : i64, !llvm.ptr
cf.br ^bb18
^bb20:
func.call @free(%40) : (!llvm.ptr) -> ()
%194 = llvm.load %100 : !llvm.ptr -> i64
%196 = llvm.load %130 : !llvm.ptr -> i64
%197 = llvm.mlir.addressof @MOD : !llvm.ptr
%198 = llvm.load %197 : !llvm.ptr -> i64
%195 = func.call @mod_inv(%196, %198) : (i64, i64) -> i64
%199 = arith.muli %194, %195 : i64
%200 = llvm.mlir.addressof @MOD : !llvm.ptr
%201 = llvm.load %200 : !llvm.ptr -> i64
%202 = arith.remsi %199, %201 : i64
func.return %202 : i64
}
func.func @enum_partitions(%arg0: i64, %arg1: i64, %arg2: i64) -> () {
%203 = arith.constant 0 : i32
%205 = arith.extsi %203 : i32 to i64
%204 = arith.cmpi sgt, %arg0, %205 : i64
cf.cond_br %204, ^bb24, ^bb25
^bb24:
%206 = arith.constant 0 : i32
%207 = arith.extsi %206 : i32 to i64
%208 = arith.constant 0 : i32
%209 = arith.extsi %208 : i32 to i64
%210 = llvm.mlir.constant(1 : i64) : i64
%211 = llvm.alloca %210 x i64 : (i64) -> !llvm.ptr
llvm.store %209, %211 : i64, !llvm.ptr
cf.br ^bb27(%207 : i64)
^bb27(%212: i64):
%213 = llvm.load %211 : !llvm.ptr -> i64
%214 = arith.cmpi slt, %213, %arg0 : i64
cf.cond_br %214, ^bb28(%212 : i64), ^bb29(%212 : i64)
^bb28(%215: i64):
%217 = llvm.mlir.addressof @shape : !llvm.ptr
%218 = llvm.load %217 : !llvm.ptr -> !llvm.ptr
%219 = llvm.load %211 : !llvm.ptr -> i64
%220 = llvm.getelementptr %218[%219] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%216 = llvm.load %220 : !llvm.ptr -> i64
%221 = arith.addi %215, %216 : i64
%222 = llvm.load %211 : !llvm.ptr -> i64
%223 = arith.constant 1 : i32
%225 = arith.extsi %223 : i32 to i64
%224 = arith.addi %222, %225 : i64
llvm.store %224, %211 : i64, !llvm.ptr
cf.br ^bb27(%221 : i64)
^bb29(%226: i64):
%228 = llvm.mlir.addressof @shape : !llvm.ptr
%229 = llvm.load %228 : !llvm.ptr -> !llvm.ptr
%230 = arith.constant 0 : i32
%231 = arith.extsi %230 : i32 to i64
%232 = llvm.getelementptr %229[%231] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%227 = llvm.load %232 : !llvm.ptr -> i64
%233 = func.call @shape_weight_mod(%arg0) : (i64) -> i64
%234 = llvm.mlir.addressof @MAX_SIDE : !llvm.ptr
%235 = llvm.load %234 : !llvm.ptr -> i64
%236 = llvm.mlir.addressof @MAX_SIDE : !llvm.ptr
%237 = llvm.load %236 : !llvm.ptr -> i64
%238 = arith.muli %235, %237 : i64
%239 = arith.cmpi sle, %226, %238 : i64
%240 = scf.if %239 -> (i1) {
%241 = arith.cmpi eq, %227, %arg0 : i64
scf.yield %241 : i1
} else {
%242 = arith.constant false
scf.yield %242 : i1
}
cf.cond_br %240, ^bb30, ^bb31
^bb30:
%243 = llvm.mlir.addressof @balanced_all : !llvm.ptr
%244 = llvm.load %243 : !llvm.ptr -> i64
%245 = arith.addi %244, %233 : i64
%246 = llvm.mlir.addressof @MOD : !llvm.ptr
%247 = llvm.load %246 : !llvm.ptr -> i64
%248 = arith.remsi %245, %247 : i64
%249 = llvm.mlir.addressof @balanced_all : !llvm.ptr
llvm.store %248, %249 : i64, !llvm.ptr
cf.br ^bb32
^bb31:
cf.br ^bb32
^bb32:
%250 = arith.constant 1 : i32
%252 = arith.extsi %250 : i32 to i64
%251 = arith.addi %226, %252 : i64
%253 = llvm.mlir.addressof @MAX_SIDE : !llvm.ptr
%254 = llvm.load %253 : !llvm.ptr -> i64
%255 = llvm.mlir.addressof @MAX_SIDE : !llvm.ptr
%256 = llvm.load %255 : !llvm.ptr -> i64
%257 = arith.muli %254, %256 : i64
%258 = arith.cmpi sle, %251, %257 : i64
%259 = scf.if %258 -> (i1) {
%260 = arith.constant 1 : i32
%262 = arith.extsi %260 : i32 to i64
%261 = arith.addi %227, %262 : i64
%263 = arith.cmpi eq, %arg0, %261 : i64
scf.yield %263 : i1
} else {
%264 = arith.constant false
scf.yield %264 : i1
}
cf.cond_br %259, ^bb33, ^bb34
^bb33:
%265 = llvm.mlir.addressof @balanced_ending_nine : !llvm.ptr
%266 = llvm.load %265 : !llvm.ptr -> i64
%267 = arith.addi %266, %233 : i64
%268 = llvm.mlir.addressof @MOD : !llvm.ptr
%269 = llvm.load %268 : !llvm.ptr -> i64
%270 = arith.remsi %267, %269 : i64
%271 = llvm.mlir.addressof @balanced_ending_nine : !llvm.ptr
llvm.store %270, %271 : i64, !llvm.ptr
cf.br ^bb35
^bb34:
cf.br ^bb35
^bb35:
cf.br ^bb26
^bb25:
cf.br ^bb26
^bb26:
%272 = arith.constant 0 : i32
%274 = arith.extsi %272 : i32 to i64
%273 = arith.cmpi eq, %arg2, %274 : i64
cf.cond_br %273, ^bb36, ^bb37
^bb36:
func.return
^bb37:
cf.br ^bb38
^bb38:
%275 = llvm.mlir.constant(1 : i64) : i64
%276 = llvm.alloca %275 x i64 : (i64) -> !llvm.ptr
llvm.store %arg1, %276 : i64, !llvm.ptr
cf.br ^bb39
^bb39:
%277 = llvm.load %276 : !llvm.ptr -> i64
%278 = arith.constant 1 : i32
%280 = arith.extsi %278 : i32 to i64
%279 = arith.cmpi sge, %277, %280 : i64
cf.cond_br %279, ^bb40, ^bb41
^bb40:
%281 = llvm.load %276 : !llvm.ptr -> i64
%282 = llvm.mlir.addressof @shape : !llvm.ptr
%283 = llvm.load %282 : !llvm.ptr -> !llvm.ptr
%284 = llvm.getelementptr %283[%arg0] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %281, %284 : i64, !llvm.ptr
%286 = arith.constant 1 : i32
%288 = arith.extsi %286 : i32 to i64
%287 = arith.addi %arg0, %288 : i64
%289 = llvm.load %276 : !llvm.ptr -> i64
%290 = arith.constant 1 : i32
%292 = arith.extsi %290 : i32 to i64
%291 = arith.subi %arg2, %292 : i64
func.call @enum_partitions(%287, %289, %291) : (i64, i64, i64) -> ()
%293 = llvm.load %276 : !llvm.ptr -> i64
%294 = arith.constant 1 : i32
%296 = arith.extsi %294 : i32 to i64
%295 = arith.subi %293, %296 : i64
llvm.store %295, %276 : i64, !llvm.ptr
cf.br ^bb39
^bb41:
func.return
}
func.func @main() -> i32 {
%298 = arith.constant 16 : i32
%299 = arith.constant 8 : i32
%300 = arith.extsi %298 : i32 to i64
%301 = arith.extsi %299 : i32 to i64
%297 = func.call @calloc(%300, %301) : (i64, i64) -> !llvm.ptr
%302 = llvm.mlir.addressof @shape : !llvm.ptr
llvm.store %297, %302 : !llvm.ptr, !llvm.ptr
%304 = arith.constant 101 : i32
%305 = arith.constant 8 : i32
%306 = arith.extsi %304 : i32 to i64
%307 = arith.extsi %305 : i32 to i64
%303 = func.call @calloc(%306, %307) : (i64, i64) -> !llvm.ptr
%308 = llvm.mlir.addressof @factorial : !llvm.ptr
llvm.store %303, %308 : !llvm.ptr, !llvm.ptr
%309 = arith.constant 1 : i32
%310 = llvm.mlir.addressof @factorial : !llvm.ptr
%311 = llvm.load %310 : !llvm.ptr -> !llvm.ptr
%312 = arith.constant 0 : i32
%313 = arith.extsi %309 : i32 to i64
%314 = arith.extsi %312 : i32 to i64
%315 = llvm.getelementptr %311[%314] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %313, %315 : i64, !llvm.ptr
%316 = arith.constant 1 : i32
%317 = arith.extsi %316 : i32 to i64
%318 = llvm.mlir.constant(1 : i64) : i64
%319 = llvm.alloca %318 x i64 : (i64) -> !llvm.ptr
llvm.store %317, %319 : i64, !llvm.ptr
cf.br ^bb42
^bb42:
%320 = llvm.load %319 : !llvm.ptr -> i64
%321 = arith.constant 100 : i32
%323 = arith.extsi %321 : i32 to i64
%322 = arith.cmpi sle, %320, %323 : i64
cf.cond_br %322, ^bb43, ^bb44
^bb43:
%325 = llvm.mlir.addressof @factorial : !llvm.ptr
%326 = llvm.load %325 : !llvm.ptr -> !llvm.ptr
%327 = llvm.load %319 : !llvm.ptr -> i64
%328 = arith.constant 1 : i32
%330 = arith.extsi %328 : i32 to i64
%329 = arith.subi %327, %330 : i64
%331 = llvm.getelementptr %326[%329] : (!llvm.ptr, i64) -> !llvm.ptr, i64
%324 = llvm.load %331 : !llvm.ptr -> i64
%332 = llvm.load %319 : !llvm.ptr -> i64
%333 = arith.muli %324, %332 : i64
%334 = llvm.mlir.addressof @MOD : !llvm.ptr
%335 = llvm.load %334 : !llvm.ptr -> i64
%336 = arith.remsi %333, %335 : i64
%337 = llvm.mlir.addressof @factorial : !llvm.ptr
%338 = llvm.load %337 : !llvm.ptr -> !llvm.ptr
%339 = llvm.load %319 : !llvm.ptr -> i64
%340 = llvm.getelementptr %338[%339] : (!llvm.ptr, i64) -> !llvm.ptr, i64
llvm.store %336, %340 : i64, !llvm.ptr
%341 = llvm.load %319 : !llvm.ptr -> i64
%342 = arith.constant 1 : i32
%344 = arith.extsi %342 : i32 to i64
%343 = arith.addi %341, %344 : i64
llvm.store %343, %319 : i64, !llvm.ptr
cf.br ^bb42
^bb44:
%345 = arith.constant 1 : i32
%346 = arith.extsi %345 : i32 to i64
%347 = llvm.mlir.addressof @balanced_ending_nine : !llvm.ptr
llvm.store %346, %347 : i64, !llvm.ptr
%349 = arith.constant 0 : i32
%350 = llvm.mlir.addressof @MAX_SIDE : !llvm.ptr
%351 = llvm.load %350 : !llvm.ptr -> i64
%352 = llvm.mlir.addressof @MAX_SIDE : !llvm.ptr
%353 = llvm.load %352 : !llvm.ptr -> i64
%354 = arith.extsi %349 : i32 to i64
func.call @enum_partitions(%354, %351, %353) : (i64, i64, i64) -> ()
%355 = llvm.mlir.addressof @balanced_all : !llvm.ptr
%356 = llvm.load %355 : !llvm.ptr -> i64
%357 = llvm.mlir.addressof @balanced_ending_nine : !llvm.ptr
%358 = llvm.load %357 : !llvm.ptr -> i64
%359 = llvm.mlir.addressof @MOD : !llvm.ptr
%360 = llvm.load %359 : !llvm.ptr -> i64
%361 = arith.remsi %358, %360 : i64
%362 = arith.subi %356, %361 : i64
%363 = llvm.mlir.addressof @MOD : !llvm.ptr
%364 = llvm.load %363 : !llvm.ptr -> i64
%365 = arith.addi %362, %364 : i64
%366 = llvm.mlir.addressof @MOD : !llvm.ptr
%367 = llvm.load %366 : !llvm.ptr -> i64
%368 = arith.remsi %365, %367 : i64
%369 = llvm.mlir.addressof @str_0 : !llvm.ptr
%370 = llvm.call @printf(%369, %368) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
%372 = llvm.mlir.addressof @shape : !llvm.ptr
%373 = llvm.load %372 : !llvm.ptr -> !llvm.ptr
func.call @free(%373) : (!llvm.ptr) -> ()
%375 = llvm.mlir.addressof @factorial : !llvm.ptr
%376 = llvm.load %375 : !llvm.ptr -> !llvm.ptr
func.call @free(%376) : (!llvm.ptr) -> ()
%377 = arith.constant 0 : i32
func.return %377 : i32
}
}