Problem 665

Greedily build losing pairs using successor-DSU structures. f(10^7) = sum of (a+b) over losing positions with a+b <= 10^7.

Answer11541685709674
Output11541685709674
StatusPASS
Native helperno
Runtime230 ms
Peak memory489408 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n^3)
Space complexityO(n)O(n^2)
ApproachFlow solutionGame theory DP
VerdictOptimal

Flow source

# Project Euler 665: Proportionate Nim
# Greedily build losing pairs using successor-DSU structures.
# f(10^7) = sum of (a+b) over losing positions with a+b <= 10^7.

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

function dsu_find(parent: ptr<i64>, x: i64) -> i64 {
    let mut cx: i64 = x
    while parent[cx] != cx {
        parent[cx] = parent[parent[cx]]
        cx = parent[cx]
    }
    return cx
}

function dsu_mark(parent: ptr<i64>, x: i64) -> void {
    if parent[x] == x {
        parent[x] = dsu_find(parent, x + 1)
    }
}

function main() -> i32 {
    let M: i64 = 10000000
    let half: i64 = M / 2
    let max_coord: i64 = M * 5 / 4 + 100

    # DSU for coordinates and differences
    let coord_parent: ptr<i64> = calloc(max_coord + 2, 8)
    let diff_parent: ptr<i64> = calloc(max_coord + 2, 8)
    if coord_parent == null || diff_parent == null { return 1 }
    for i in 0..(max_coord + 2) {
        coord_parent[i] = i
        diff_parent[i] = i
    }

    # DSU for v = x - 2y, range [-2*max_coord, max_coord]
    let v_min: i64 = -2 * max_coord
    let v_offset: i64 = -v_min
    let v_len: i64 = 3 * max_coord + 1
    let v_parent: ptr<i64> = calloc(v_len + 1, 8)
    if v_parent == null { return 1 }
    for i in 0..(v_len + 1) {
        v_parent[i] = i
    }

    # Helper for v operations
    # v_idx(v) = v + v_offset
    # v_find(i) = dsu_find(v_parent, i)
    # v_mark(v): mark v as used

    # Base losing position (0,0)
    dsu_mark(coord_parent, 0)
    dsu_mark(diff_parent, 0)
    # v_mark(0)
    let i0: i64 = 0 + v_offset
    if v_parent[i0] == i0 {
        v_parent[i0] = dsu_find(v_parent, i0 + 1)
    }

    let mut total: i64 = 0
    let mut a: i64 = 1

    while true {
        a = dsu_find(coord_parent, a)
        if a > half { break }

        let mut b: i64 = a + 1
        let mut d: i64 = 0
        let mut v1: i64 = 0
        let mut v2: i64 = 0
        while true {
            b = dsu_find(coord_parent, b)

            d = b - a
            if diff_parent[d] != d {
                let nd: i64 = dsu_find(diff_parent, d)
                b = a + nd
                continue
            }

            v1 = b - 2 * a
            let i1: i64 = v1 + v_offset
            if v_parent[i1] != i1 {
                let ni: i64 = dsu_find(v_parent, i1)
                if ni >= v_len { return 1 }
                let next_v: i64 = ni - v_offset
                b = 2 * a + next_v
                continue
            }

            # Second orientation: a - 2b
            v2 = a - 2 * b
            let i2: i64 = v2 + v_offset
            if v_parent[i2] != i2 {
                b = b + 1
                continue
            }

            break
        }

        # Record losing pair (a,b)
        dsu_mark(coord_parent, a)
        dsu_mark(coord_parent, b)
        dsu_mark(diff_parent, d)
        # v_mark(v1)
        let iv1: i64 = v1 + v_offset
        if v_parent[iv1] == iv1 {
            v_parent[iv1] = dsu_find(v_parent, iv1 + 1)
        }
        # v_mark(v2)
        let iv2: i64 = v2 + v_offset
        if v_parent[iv2] == iv2 {
            v_parent[iv2] = dsu_find(v_parent, iv2 + 1)
        }

        let s: i64 = a + b
        if s <= M {
            total = total + s
        }

        a = a + 1
    }

    printf("%lld\n", total)

    free(v_parent)
    free(diff_parent)
    free(coord_parent)
    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 dsu_find_ptr_i64_i64(int64_t* parent, int64_t x);
void dsu_mark_ptr_i64_i64(int64_t* parent, int64_t x);
int32_t main(void);



int64_t dsu_find_ptr_i64_i64(int64_t* parent, int64_t x) {
    int64_t cx = x;
    while (parent[cx] != cx) {
        parent[cx] = parent[parent[cx]];
        cx = parent[cx];
    }
    return cx;
}

void dsu_mark_ptr_i64_i64(int64_t* parent, int64_t x) {
    if (parent[x] == x) {
        parent[x] = dsu_find_ptr_i64_i64(parent, (x + 1));
    }
}

int32_t main(void) {
    int64_t M = 10000000;
    int64_t half = FLOW_CHECKED_DIV((M), (2));
    int64_t max_coord = (FLOW_CHECKED_DIV(((M * 5)), (4)) + 100);
    int64_t* coord_parent = (int64_t*)(calloc((max_coord + 2), 8));
    int64_t* diff_parent = (int64_t*)(calloc((max_coord + 2), 8));
    if ((coord_parent == NULL || diff_parent == NULL)) {
        return 1;
    }
    int32_t __flow_step_1 = 1;
    for (int32_t i = 0; (0 <= (max_coord + 2)) ? i < (max_coord + 2) : i > (max_coord + 2); i += (0 <= (max_coord + 2)) ? 1 : -1) {
        coord_parent[i] = i;
        diff_parent[i] = i;
    }
    int64_t v_min = ((-2) * max_coord);
    int64_t v_offset = (-v_min);
    int64_t v_len = ((3 * max_coord) + 1);
    int64_t* v_parent = (int64_t*)(calloc((v_len + 1), 8));
    if (v_parent == NULL) {
        return 1;
    }
    int32_t __flow_step_2 = 1;
    for (int32_t i = 0; (0 <= (v_len + 1)) ? i < (v_len + 1) : i > (v_len + 1); i += (0 <= (v_len + 1)) ? 1 : -1) {
        v_parent[i] = i;
    }
    dsu_mark_ptr_i64_i64(coord_parent, 0);
    dsu_mark_ptr_i64_i64(diff_parent, 0);
    int64_t i0 = (0 + v_offset);
    if (v_parent[i0] == i0) {
        v_parent[i0] = dsu_find_ptr_i64_i64(v_parent, (i0 + 1));
    }
    int64_t total = 0;
    int64_t a = 1;
    while (1) {
        a = dsu_find_ptr_i64_i64(coord_parent, a);
        if (a > half) {
            break;
        }
        int64_t b = (a + 1);
        int64_t d = 0;
        int64_t v1 = 0;
        int64_t v2 = 0;
        while (1) {
            b = dsu_find_ptr_i64_i64(coord_parent, b);
            d = (b - a);
            if (diff_parent[d] != d) {
                int64_t nd = dsu_find_ptr_i64_i64(diff_parent, d);
                b = (a + nd);
                continue;
            }
            v1 = (b - (2 * a));
            int64_t i1 = (v1 + v_offset);
            if (v_parent[i1] != i1) {
                int64_t ni = dsu_find_ptr_i64_i64(v_parent, i1);
                if (ni >= v_len) {
                    return 1;
                }
                int64_t next_v = (ni - v_offset);
                b = ((2 * a) + next_v);
                continue;
            }
            v2 = (a - (2 * b));
            int64_t i2 = (v2 + v_offset);
            if (v_parent[i2] != i2) {
                b = (b + 1);
                continue;
            }
            break;
        }
        dsu_mark_ptr_i64_i64(coord_parent, a);
        dsu_mark_ptr_i64_i64(coord_parent, b);
        dsu_mark_ptr_i64_i64(diff_parent, d);
        int64_t iv1 = (v1 + v_offset);
        if (v_parent[iv1] == iv1) {
            v_parent[iv1] = dsu_find_ptr_i64_i64(v_parent, (iv1 + 1));
        }
        int64_t iv2 = (v2 + v_offset);
        if (v_parent[iv2] == iv2) {
            v_parent[iv2] = dsu_find_ptr_i64_i64(v_parent, (iv2 + 1));
        }
        int64_t s = (a + b);
        if (s <= M) {
            total = (total + s);
        }
        a = (a + 1);
    }
    printf("%lld\n", total);
    free(v_parent);
    free(diff_parent);
    free(coord_parent);
    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) -> ()
  func.func @dsu_find(%arg0: !llvm.ptr, %arg1: i64) -> i64 {
    %0 = llvm.mlir.constant(1 : i64) : i64
    %1 = llvm.alloca %0 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %1 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %3 = llvm.load %1 : !llvm.ptr -> i64
    %4 = llvm.getelementptr %arg0[%3] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %2 = llvm.load %4 : !llvm.ptr -> i64
    %5 = llvm.load %1 : !llvm.ptr -> i64
    %6 = arith.cmpi ne, %2, %5 : i64
    cf.cond_br %6, ^bb1, ^bb2
    ^bb1:
      %9 = llvm.load %1 : !llvm.ptr -> i64
      %10 = llvm.getelementptr %arg0[%9] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %8 = llvm.load %10 : !llvm.ptr -> i64
      %11 = llvm.getelementptr %arg0[%8] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %7 = llvm.load %11 : !llvm.ptr -> i64
      %12 = llvm.load %1 : !llvm.ptr -> i64
      %13 = llvm.getelementptr %arg0[%12] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %7, %13 : i64, !llvm.ptr
      %15 = llvm.load %1 : !llvm.ptr -> i64
      %16 = llvm.getelementptr %arg0[%15] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %14 = llvm.load %16 : !llvm.ptr -> i64
      llvm.store %14, %1 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %17 = llvm.load %1 : !llvm.ptr -> i64
    func.return %17 : i64
  }
  func.func @dsu_mark(%arg0: !llvm.ptr, %arg1: i64) -> () {
    %19 = llvm.getelementptr %arg0[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %18 = llvm.load %19 : !llvm.ptr -> i64
    %20 = arith.cmpi eq, %18, %arg1 : i64
    cf.cond_br %20, ^bb3, ^bb4
    ^bb3:
      %22 = arith.constant 1 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.addi %arg1, %24 : i64
      %21 = func.call @dsu_find(%arg0, %23) : (!llvm.ptr, i64) -> i64
      %25 = llvm.getelementptr %arg0[%arg1] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %21, %25 : i64, !llvm.ptr
      cf.br ^bb5
    ^bb4:
      cf.br ^bb5
    ^bb5:
    func.return
  }
  func.func @main() -> i32 {
    %26 = arith.constant 10000000 : i32
    %27 = arith.extsi %26 : i32 to i64
    %28 = arith.constant 2 : i32
    %30 = arith.extsi %28 : i32 to i64
    %29 = arith.divsi %27, %30 : i64
    %31 = arith.constant 5 : i32
    %33 = arith.extsi %31 : i32 to i64
    %32 = arith.muli %27, %33 : i64
    %34 = arith.constant 4 : i32
    %36 = arith.extsi %34 : i32 to i64
    %35 = arith.divsi %32, %36 : i64
    %37 = arith.constant 100 : i32
    %39 = arith.extsi %37 : i32 to i64
    %38 = arith.addi %35, %39 : i64
    %41 = arith.constant 2 : i32
    %43 = arith.extsi %41 : i32 to i64
    %42 = arith.addi %38, %43 : i64
    %44 = arith.constant 8 : i32
    %45 = arith.extsi %44 : i32 to i64
    %40 = func.call @calloc(%42, %45) : (i64, i64) -> !llvm.ptr
    %47 = arith.constant 2 : i32
    %49 = arith.extsi %47 : i32 to i64
    %48 = arith.addi %38, %49 : i64
    %50 = arith.constant 8 : i32
    %51 = arith.extsi %50 : i32 to i64
    %46 = func.call @calloc(%48, %51) : (i64, i64) -> !llvm.ptr
    %52 = llvm.mlir.zero : !llvm.ptr
    %53 = llvm.icmp "eq" %40, %52 : !llvm.ptr
    %54 = scf.if %53 -> (i1) {
      %55 = arith.constant true
      scf.yield %55 : i1
    } else {
      %56 = llvm.mlir.zero : !llvm.ptr
      %57 = llvm.icmp "eq" %46, %56 : !llvm.ptr
      scf.yield %57 : i1
    }
    cf.cond_br %54, ^bb6, ^bb7
    ^bb6:
      %58 = arith.constant 1 : i32
      func.return %58 : i32
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %59 = arith.constant 0 : i32
    %60 = arith.constant 2 : i32
    %62 = arith.extsi %60 : i32 to i64
    %61 = arith.addi %38, %62 : i64
    %63 = arith.index_cast %59 : i32 to index
    %64 = arith.index_cast %61 : i32 to index
    %66 = arith.constant 1 : index
    %67 = arith.constant -1 : index
    %68 = arith.cmpi sle, %63, %64 : index
    %65 = arith.select %68, %66, %67 : index
    cf.br ^bb9(%63 : index)
    ^bb9(%69: index):
    %70 = arith.cmpi slt, %69, %64 : index
    %71 = arith.cmpi sgt, %69, %64 : index
    %72 = arith.select %68, %70, %71 : i1
    cf.cond_br %72, ^bb10(%69 : index), ^bb11(%69 : index)
    ^bb10(%73: index):
      %74 = arith.index_cast %73 : index to i64
      %75 = arith.index_cast %73 : index to i64
      %76 = llvm.getelementptr %40[%75] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %74, %76 : i64, !llvm.ptr
      %77 = arith.index_cast %73 : index to i64
      %78 = arith.index_cast %73 : index to i64
      %79 = llvm.getelementptr %46[%78] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %77, %79 : i64, !llvm.ptr
      %80 = arith.addi %73, %65 : index
      cf.br ^bb9(%80 : index)
    ^bb11(%81: index):
    %82 = arith.constant 2 : i32
    %84 = arith.constant 0 : i32
    %83 = arith.subi %84, %82 : i32
    %86 = arith.extsi %83 : i32 to i64
    %85 = arith.muli %86, %38 : i64
    %88 = arith.constant 0 : i64
    %87 = arith.subi %88, %85 : i64
    %89 = arith.constant 3 : i32
    %91 = arith.extsi %89 : i32 to i64
    %90 = arith.muli %91, %38 : i64
    %92 = arith.constant 1 : i32
    %94 = arith.extsi %92 : i32 to i64
    %93 = arith.addi %90, %94 : i64
    %96 = arith.constant 1 : i32
    %98 = arith.extsi %96 : i32 to i64
    %97 = arith.addi %93, %98 : i64
    %99 = arith.constant 8 : i32
    %100 = arith.extsi %99 : i32 to i64
    %95 = func.call @calloc(%97, %100) : (i64, i64) -> !llvm.ptr
    %101 = llvm.mlir.zero : !llvm.ptr
    %102 = llvm.icmp "eq" %95, %101 : !llvm.ptr
    cf.cond_br %102, ^bb12, ^bb13
    ^bb12:
      %103 = arith.constant 1 : i32
      func.return %103 : i32
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %104 = arith.constant 0 : i32
    %105 = arith.constant 1 : i32
    %107 = arith.extsi %105 : i32 to i64
    %106 = arith.addi %93, %107 : i64
    %108 = arith.index_cast %104 : i32 to index
    %109 = arith.index_cast %106 : i32 to index
    %111 = arith.constant 1 : index
    %112 = arith.constant -1 : index
    %113 = arith.cmpi sle, %108, %109 : index
    %110 = arith.select %113, %111, %112 : index
    cf.br ^bb15(%108 : index)
    ^bb15(%114: index):
    %115 = arith.cmpi slt, %114, %109 : index
    %116 = arith.cmpi sgt, %114, %109 : index
    %117 = arith.select %113, %115, %116 : i1
    cf.cond_br %117, ^bb16(%114 : index), ^bb17(%114 : index)
    ^bb16(%118: index):
      %119 = arith.index_cast %118 : index to i64
      %120 = arith.index_cast %118 : index to i64
      %121 = llvm.getelementptr %95[%120] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %119, %121 : i64, !llvm.ptr
      %122 = arith.addi %118, %110 : index
      cf.br ^bb15(%122 : index)
    ^bb17(%123: index):
    %125 = arith.constant 0 : i32
    %126 = arith.extsi %125 : i32 to i64
    func.call @dsu_mark(%40, %126) : (!llvm.ptr, i64) -> ()
    %128 = arith.constant 0 : i32
    %129 = arith.extsi %128 : i32 to i64
    func.call @dsu_mark(%46, %129) : (!llvm.ptr, i64) -> ()
    %130 = arith.constant 0 : i32
    %132 = arith.extsi %130 : i32 to i64
    %131 = arith.addi %132, %87 : i64
    %134 = llvm.getelementptr %95[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %133 = llvm.load %134 : !llvm.ptr -> i64
    %135 = arith.cmpi eq, %133, %131 : i64
    cf.cond_br %135, ^bb18, ^bb19
    ^bb18:
      %137 = arith.constant 1 : i32
      %139 = arith.extsi %137 : i32 to i64
      %138 = arith.addi %131, %139 : i64
      %136 = func.call @dsu_find(%95, %138) : (!llvm.ptr, i64) -> i64
      %140 = llvm.getelementptr %95[%131] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %136, %140 : i64, !llvm.ptr
      cf.br ^bb20
    ^bb19:
      cf.br ^bb20
    ^bb20:
    %141 = arith.constant 0 : i32
    %142 = arith.extsi %141 : i32 to i64
    %143 = llvm.mlir.constant(1 : i64) : i64
    %144 = llvm.alloca %143 x i64 : (i64) -> !llvm.ptr
    llvm.store %142, %144 : i64, !llvm.ptr
    %145 = arith.constant 1 : i32
    %146 = arith.extsi %145 : i32 to i64
    %147 = llvm.mlir.constant(1 : i64) : i64
    %148 = llvm.alloca %147 x i64 : (i64) -> !llvm.ptr
    llvm.store %146, %148 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %149 = arith.constant 1 : i1
    cf.cond_br %149, ^bb22, ^bb23
    ^bb22:
      %151 = llvm.load %148 : !llvm.ptr -> i64
      %150 = func.call @dsu_find(%40, %151) : (!llvm.ptr, i64) -> i64
      llvm.store %150, %148 : i64, !llvm.ptr
      %152 = llvm.load %148 : !llvm.ptr -> i64
      %153 = arith.cmpi sgt, %152, %29 : i64
      cf.cond_br %153, ^bb24, ^bb25
      ^bb24:
        cf.br ^bb23
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %154 = llvm.load %148 : !llvm.ptr -> i64
      %155 = arith.constant 1 : i32
      %157 = arith.extsi %155 : i32 to i64
      %156 = arith.addi %154, %157 : i64
      %158 = llvm.mlir.constant(1 : i64) : i64
      %159 = llvm.alloca %158 x i64 : (i64) -> !llvm.ptr
      llvm.store %156, %159 : i64, !llvm.ptr
      %160 = arith.constant 0 : i32
      %161 = arith.extsi %160 : i32 to i64
      %162 = llvm.mlir.constant(1 : i64) : i64
      %163 = llvm.alloca %162 x i64 : (i64) -> !llvm.ptr
      llvm.store %161, %163 : i64, !llvm.ptr
      %164 = arith.constant 0 : i32
      %165 = arith.extsi %164 : i32 to i64
      %166 = llvm.mlir.constant(1 : i64) : i64
      %167 = llvm.alloca %166 x i64 : (i64) -> !llvm.ptr
      llvm.store %165, %167 : i64, !llvm.ptr
      %168 = arith.constant 0 : i32
      %169 = arith.extsi %168 : i32 to i64
      %170 = llvm.mlir.constant(1 : i64) : i64
      %171 = llvm.alloca %170 x i64 : (i64) -> !llvm.ptr
      llvm.store %169, %171 : i64, !llvm.ptr
      cf.br ^bb27
      ^bb27:
      %172 = arith.constant 1 : i1
      cf.cond_br %172, ^bb28, ^bb29
      ^bb28:
        %174 = llvm.load %159 : !llvm.ptr -> i64
        %173 = func.call @dsu_find(%40, %174) : (!llvm.ptr, i64) -> i64
        llvm.store %173, %159 : i64, !llvm.ptr
        %175 = llvm.load %159 : !llvm.ptr -> i64
        %176 = llvm.load %148 : !llvm.ptr -> i64
        %177 = arith.subi %175, %176 : i64
        llvm.store %177, %163 : i64, !llvm.ptr
        %179 = llvm.load %163 : !llvm.ptr -> i64
        %180 = llvm.getelementptr %46[%179] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %178 = llvm.load %180 : !llvm.ptr -> i64
        %181 = llvm.load %163 : !llvm.ptr -> i64
        %182 = arith.cmpi ne, %178, %181 : i64
        cf.cond_br %182, ^bb30, ^bb31
        ^bb30:
          %184 = llvm.load %163 : !llvm.ptr -> i64
          %183 = func.call @dsu_find(%46, %184) : (!llvm.ptr, i64) -> i64
          %185 = llvm.load %148 : !llvm.ptr -> i64
          %186 = arith.addi %185, %183 : i64
          llvm.store %186, %159 : i64, !llvm.ptr
          cf.br ^bb27
        ^bb31:
          cf.br ^bb32
        ^bb32:
        %187 = llvm.load %159 : !llvm.ptr -> i64
        %188 = arith.constant 2 : i32
        %189 = llvm.load %148 : !llvm.ptr -> i64
        %191 = arith.extsi %188 : i32 to i64
        %190 = arith.muli %191, %189 : i64
        %192 = arith.subi %187, %190 : i64
        llvm.store %192, %167 : i64, !llvm.ptr
        %193 = llvm.load %167 : !llvm.ptr -> i64
        %194 = arith.addi %193, %87 : i64
        %196 = llvm.getelementptr %95[%194] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %195 = llvm.load %196 : !llvm.ptr -> i64
        %197 = arith.cmpi ne, %195, %194 : i64
        cf.cond_br %197, ^bb33, ^bb34
        ^bb33:
          %198 = func.call @dsu_find(%95, %194) : (!llvm.ptr, i64) -> i64
          %199 = arith.cmpi sge, %198, %93 : i64
          cf.cond_br %199, ^bb36, ^bb37
          ^bb36:
            %200 = arith.constant 1 : i32
            func.return %200 : i32
          ^bb37:
            cf.br ^bb38
          ^bb38:
          %201 = arith.subi %198, %87 : i64
          %202 = arith.constant 2 : i32
          %203 = llvm.load %148 : !llvm.ptr -> i64
          %205 = arith.extsi %202 : i32 to i64
          %204 = arith.muli %205, %203 : i64
          %206 = arith.addi %204, %201 : i64
          llvm.store %206, %159 : i64, !llvm.ptr
          cf.br ^bb27
        ^bb34:
          cf.br ^bb35
        ^bb35:
        %207 = llvm.load %148 : !llvm.ptr -> i64
        %208 = arith.constant 2 : i32
        %209 = llvm.load %159 : !llvm.ptr -> i64
        %211 = arith.extsi %208 : i32 to i64
        %210 = arith.muli %211, %209 : i64
        %212 = arith.subi %207, %210 : i64
        llvm.store %212, %171 : i64, !llvm.ptr
        %213 = llvm.load %171 : !llvm.ptr -> i64
        %214 = arith.addi %213, %87 : i64
        %216 = llvm.getelementptr %95[%214] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %215 = llvm.load %216 : !llvm.ptr -> i64
        %217 = arith.cmpi ne, %215, %214 : i64
        cf.cond_br %217, ^bb39, ^bb40
        ^bb39:
          %218 = llvm.load %159 : !llvm.ptr -> i64
          %219 = arith.constant 1 : i32
          %221 = arith.extsi %219 : i32 to i64
          %220 = arith.addi %218, %221 : i64
          llvm.store %220, %159 : i64, !llvm.ptr
          cf.br ^bb27
        ^bb40:
          cf.br ^bb41
        ^bb41:
        cf.br ^bb29
      ^bb29:
      %223 = llvm.load %148 : !llvm.ptr -> i64
      func.call @dsu_mark(%40, %223) : (!llvm.ptr, i64) -> ()
      %225 = llvm.load %159 : !llvm.ptr -> i64
      func.call @dsu_mark(%40, %225) : (!llvm.ptr, i64) -> ()
      %227 = llvm.load %163 : !llvm.ptr -> i64
      func.call @dsu_mark(%46, %227) : (!llvm.ptr, i64) -> ()
      %228 = llvm.load %167 : !llvm.ptr -> i64
      %229 = arith.addi %228, %87 : i64
      %231 = llvm.getelementptr %95[%229] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %230 = llvm.load %231 : !llvm.ptr -> i64
      %232 = arith.cmpi eq, %230, %229 : i64
      cf.cond_br %232, ^bb42, ^bb43
      ^bb42:
        %234 = arith.constant 1 : i32
        %236 = arith.extsi %234 : i32 to i64
        %235 = arith.addi %229, %236 : i64
        %233 = func.call @dsu_find(%95, %235) : (!llvm.ptr, i64) -> i64
        %237 = llvm.getelementptr %95[%229] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %233, %237 : i64, !llvm.ptr
        cf.br ^bb44
      ^bb43:
        cf.br ^bb44
      ^bb44:
      %238 = llvm.load %171 : !llvm.ptr -> i64
      %239 = arith.addi %238, %87 : i64
      %241 = llvm.getelementptr %95[%239] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %240 = llvm.load %241 : !llvm.ptr -> i64
      %242 = arith.cmpi eq, %240, %239 : i64
      cf.cond_br %242, ^bb45, ^bb46
      ^bb45:
        %244 = arith.constant 1 : i32
        %246 = arith.extsi %244 : i32 to i64
        %245 = arith.addi %239, %246 : i64
        %243 = func.call @dsu_find(%95, %245) : (!llvm.ptr, i64) -> i64
        %247 = llvm.getelementptr %95[%239] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %243, %247 : i64, !llvm.ptr
        cf.br ^bb47
      ^bb46:
        cf.br ^bb47
      ^bb47:
      %248 = llvm.load %148 : !llvm.ptr -> i64
      %249 = llvm.load %159 : !llvm.ptr -> i64
      %250 = arith.addi %248, %249 : i64
      %251 = arith.cmpi sle, %250, %27 : i64
      cf.cond_br %251, ^bb48, ^bb49
      ^bb48:
        %252 = llvm.load %144 : !llvm.ptr -> i64
        %253 = arith.addi %252, %250 : i64
        llvm.store %253, %144 : i64, !llvm.ptr
        cf.br ^bb50
      ^bb49:
        cf.br ^bb50
      ^bb50:
      %254 = llvm.load %148 : !llvm.ptr -> i64
      %255 = arith.constant 1 : i32
      %257 = arith.extsi %255 : i32 to i64
      %256 = arith.addi %254, %257 : i64
      llvm.store %256, %148 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %258 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %259 = llvm.load %144 : !llvm.ptr -> i64
    %260 = llvm.call @printf(%258, %259) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    func.call @free(%95) : (!llvm.ptr) -> ()
    func.call @free(%46) : (!llvm.ptr) -> ()
    func.call @free(%40) : (!llvm.ptr) -> ()
    %264 = arith.constant 0 : i32
    func.return %264 : i32
  }
}