Problem 702

Jumping Flea — S(N) via modular-multiplication inversion counts.

Answer622305608172525546
Output622305608172525546
StatusPASS
Native helperno
Runtime0 ms
Peak memory1072 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

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

Flow source

# Project Euler 702
# Jumping Flea — S(N) via modular-multiplication inversion counts.

function inv_count(x0: i64, m: i64) -> i64 {
    if m <= 2 { return 0 }
    let mut x: i64 = x0 % m
    if x <= 1 { return 0 }
    if x == m - 1 { return (m - 1) * (m - 2) / 2 }
    let t: i64 = m / x
    let y: i64 = m - t * x
    let block: i64 = (t * (t + 1) / 2) * (x * (x - 1) / 2)
    return block + (t + 1) * inv_count(x, y) - t * inv_count(x, x - y)
}

function g(x: i64, m: i64) -> i64 {
    if m <= 2 { return 0 }
    return (m - 1) * (m - 2) - inv_count(x, m)
}

function bit_length(n0: i64) -> i64 {
    let mut n: i64 = n0
    let mut d: i64 = 0
    while n > 0 {
        n = n >> 1
        d = d + 1
    }
    return d
}

function S(N: i64) -> i64 {
    let D: i64 = bit_length(N)
    let mut total: i64 = (N * (3 * N + 1) / 2) * (D + 1)
    let mut d: i64 = 2
    while d <= D {
        total = total - g(N, 1 << d)
        d = d + 1
    }
    total = total + 2 * g(N, (1 << D) - N)
    return total
}

function main() -> i32 {
    printf("%lld\n", S(123456789))
    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 inv_count_i64_i64(int64_t x0, int64_t m);
int64_t g_i64_i64(int64_t x, int64_t m);
int64_t bit_length_i64(int64_t n0);
int64_t S_i64(int64_t N);
int32_t main(void);

int64_t inv_count_i64_i64(int64_t x0, int64_t m) {
    if (m <= 2) {
        return 0;
    }
    int64_t x = FLOW_CHECKED_MOD((x0), (m));
    if (x <= 1) {
        return 0;
    }
    if (x == (m - 1)) {
        return FLOW_CHECKED_DIV((((m - 1) * (m - 2))), (2));
    }
    int64_t t = FLOW_CHECKED_DIV((m), (x));
    int64_t y = (m - (t * x));
    int64_t block = (FLOW_CHECKED_DIV(((t * (t + 1))), (2)) * FLOW_CHECKED_DIV(((x * (x - 1))), (2)));
    return ((block + ((t + 1) * inv_count_i64_i64(x, y))) - (t * inv_count_i64_i64(x, (x - y))));
}

int64_t g_i64_i64(int64_t x, int64_t m) {
    if (m <= 2) {
        return 0;
    }
    return (((m - 1) * (m - 2)) - inv_count_i64_i64(x, m));
}

int64_t bit_length_i64(int64_t n0) {
    int64_t n = n0;
    int64_t d = 0;
    while (n > 0) {
        n = FLOW_CHECKED_SHR((n), (1));
        d = (d + 1);
    }
    return d;
}

int64_t S_i64(int64_t N) {
    int64_t D = bit_length_i64(N);
    int64_t total = (FLOW_CHECKED_DIV(((N * ((3 * N) + 1))), (2)) * (D + 1));
    int64_t d = 2;
    while (d <= D) {
        total = (total - g_i64_i64(N, FLOW_CHECKED_SHL((1), (d))));
        d = (d + 1);
    }
    total = (total + (2 * g_i64_i64(N, (FLOW_CHECKED_SHL((1), (D)) - N))));
    return total;
}

int32_t main(void) {
    printf("%lld\n", S_i64(123456789));
    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 @inv_count(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.constant 2 : i32
    %2 = arith.extsi %0 : i32 to i64
    %1 = arith.cmpi sle, %arg1, %2 : i64
    cf.cond_br %1, ^bb0, ^bb1
    ^bb0:
      %3 = arith.constant 0 : i32
      %4 = arith.extsi %3 : i32 to i64
      func.return %4 : i64
    ^bb1:
      cf.br ^bb2
    ^bb2:
    %5 = arith.remsi %arg0, %arg1 : i64
    %6 = llvm.mlir.constant(1 : i64) : i64
    %7 = llvm.alloca %6 x i64 : (i64) -> !llvm.ptr
    llvm.store %5, %7 : i64, !llvm.ptr
    %8 = llvm.load %7 : !llvm.ptr -> i64
    %9 = arith.constant 1 : i32
    %11 = arith.extsi %9 : i32 to i64
    %10 = arith.cmpi sle, %8, %11 : i64
    cf.cond_br %10, ^bb3, ^bb4
    ^bb3:
      %12 = arith.constant 0 : i32
      %13 = arith.extsi %12 : i32 to i64
      func.return %13 : i64
    ^bb4:
      cf.br ^bb5
    ^bb5:
    %14 = llvm.load %7 : !llvm.ptr -> i64
    %15 = arith.constant 1 : i32
    %17 = arith.extsi %15 : i32 to i64
    %16 = arith.subi %arg1, %17 : i64
    %18 = arith.cmpi eq, %14, %16 : i64
    cf.cond_br %18, ^bb6, ^bb7
    ^bb6:
      %19 = arith.constant 1 : i32
      %21 = arith.extsi %19 : i32 to i64
      %20 = arith.subi %arg1, %21 : i64
      %22 = arith.constant 2 : i32
      %24 = arith.extsi %22 : i32 to i64
      %23 = arith.subi %arg1, %24 : i64
      %25 = arith.muli %20, %23 : i64
      %26 = arith.constant 2 : i32
      %28 = arith.extsi %26 : i32 to i64
      %27 = arith.divsi %25, %28 : i64
      func.return %27 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %29 = llvm.load %7 : !llvm.ptr -> i64
    %30 = arith.divsi %arg1, %29 : i64
    %31 = llvm.load %7 : !llvm.ptr -> i64
    %32 = arith.muli %30, %31 : i64
    %33 = arith.subi %arg1, %32 : i64
    %34 = arith.constant 1 : i32
    %36 = arith.extsi %34 : i32 to i64
    %35 = arith.addi %30, %36 : i64
    %37 = arith.muli %30, %35 : i64
    %38 = arith.constant 2 : i32
    %40 = arith.extsi %38 : i32 to i64
    %39 = arith.divsi %37, %40 : i64
    %41 = llvm.load %7 : !llvm.ptr -> i64
    %42 = llvm.load %7 : !llvm.ptr -> i64
    %43 = arith.constant 1 : i32
    %45 = arith.extsi %43 : i32 to i64
    %44 = arith.subi %42, %45 : i64
    %46 = arith.muli %41, %44 : i64
    %47 = arith.constant 2 : i32
    %49 = arith.extsi %47 : i32 to i64
    %48 = arith.divsi %46, %49 : i64
    %50 = arith.muli %39, %48 : i64
    %51 = arith.constant 1 : i32
    %53 = arith.extsi %51 : i32 to i64
    %52 = arith.addi %30, %53 : i64
    %55 = llvm.load %7 : !llvm.ptr -> i64
    %54 = func.call @inv_count(%55, %33) : (i64, i64) -> i64
    %56 = arith.muli %52, %54 : i64
    %57 = arith.addi %50, %56 : i64
    %59 = llvm.load %7 : !llvm.ptr -> i64
    %60 = llvm.load %7 : !llvm.ptr -> i64
    %61 = arith.subi %60, %33 : i64
    %58 = func.call @inv_count(%59, %61) : (i64, i64) -> i64
    %62 = arith.muli %30, %58 : i64
    %63 = arith.subi %57, %62 : i64
    func.return %63 : i64
  }
  func.func @g(%arg0: i64, %arg1: i64) -> i64 {
    %64 = arith.constant 2 : i32
    %66 = arith.extsi %64 : i32 to i64
    %65 = arith.cmpi sle, %arg1, %66 : i64
    cf.cond_br %65, ^bb9, ^bb10
    ^bb9:
      %67 = arith.constant 0 : i32
      %68 = arith.extsi %67 : i32 to i64
      func.return %68 : i64
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %69 = arith.constant 1 : i32
    %71 = arith.extsi %69 : i32 to i64
    %70 = arith.subi %arg1, %71 : i64
    %72 = arith.constant 2 : i32
    %74 = arith.extsi %72 : i32 to i64
    %73 = arith.subi %arg1, %74 : i64
    %75 = arith.muli %70, %73 : i64
    %76 = func.call @inv_count(%arg0, %arg1) : (i64, i64) -> i64
    %77 = arith.subi %75, %76 : i64
    func.return %77 : i64
  }
  func.func @bit_length(%arg0: i64) -> i64 {
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %79 : i64, !llvm.ptr
    %80 = arith.constant 0 : i32
    %81 = arith.extsi %80 : i32 to i64
    %82 = llvm.mlir.constant(1 : i64) : i64
    %83 = llvm.alloca %82 x i64 : (i64) -> !llvm.ptr
    llvm.store %81, %83 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %84 = llvm.load %79 : !llvm.ptr -> i64
    %85 = arith.constant 0 : i32
    %87 = arith.extsi %85 : i32 to i64
    %86 = arith.cmpi sgt, %84, %87 : i64
    cf.cond_br %86, ^bb13, ^bb14
    ^bb13:
      %88 = llvm.load %79 : !llvm.ptr -> i64
      %89 = arith.constant 1 : i32
      %91 = arith.extsi %89 : i32 to i64
      %90 = arith.shrsi %88, %91 : i64
      llvm.store %90, %79 : i64, !llvm.ptr
      %92 = llvm.load %83 : !llvm.ptr -> i64
      %93 = arith.constant 1 : i32
      %95 = arith.extsi %93 : i32 to i64
      %94 = arith.addi %92, %95 : i64
      llvm.store %94, %83 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %96 = llvm.load %83 : !llvm.ptr -> i64
    func.return %96 : i64
  }
  func.func @S(%arg0: i64) -> i64 {
    %97 = func.call @bit_length(%arg0) : (i64) -> i64
    %98 = arith.constant 3 : i32
    %100 = arith.extsi %98 : i32 to i64
    %99 = arith.muli %100, %arg0 : i64
    %101 = arith.constant 1 : i32
    %103 = arith.extsi %101 : i32 to i64
    %102 = arith.addi %99, %103 : i64
    %104 = arith.muli %arg0, %102 : i64
    %105 = arith.constant 2 : i32
    %107 = arith.extsi %105 : i32 to i64
    %106 = arith.divsi %104, %107 : i64
    %108 = arith.constant 1 : i32
    %110 = arith.extsi %108 : i32 to i64
    %109 = arith.addi %97, %110 : i64
    %111 = arith.muli %106, %109 : i64
    %112 = llvm.mlir.constant(1 : i64) : i64
    %113 = llvm.alloca %112 x i64 : (i64) -> !llvm.ptr
    llvm.store %111, %113 : i64, !llvm.ptr
    %114 = arith.constant 2 : i32
    %115 = arith.extsi %114 : i32 to i64
    %116 = llvm.mlir.constant(1 : i64) : i64
    %117 = llvm.alloca %116 x i64 : (i64) -> !llvm.ptr
    llvm.store %115, %117 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %118 = llvm.load %117 : !llvm.ptr -> i64
    %119 = arith.cmpi sle, %118, %97 : i64
    cf.cond_br %119, ^bb16, ^bb17
    ^bb16:
      %120 = llvm.load %113 : !llvm.ptr -> i64
      %122 = arith.constant 1 : i32
      %123 = llvm.load %117 : !llvm.ptr -> i64
      %125 = arith.extsi %122 : i32 to i64
      %124 = arith.shli %125, %123 : i64
      %121 = func.call @g(%arg0, %124) : (i64, i64) -> i64
      %126 = arith.subi %120, %121 : i64
      llvm.store %126, %113 : i64, !llvm.ptr
      %127 = llvm.load %117 : !llvm.ptr -> i64
      %128 = arith.constant 1 : i32
      %130 = arith.extsi %128 : i32 to i64
      %129 = arith.addi %127, %130 : i64
      llvm.store %129, %117 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %131 = llvm.load %113 : !llvm.ptr -> i64
    %132 = arith.constant 2 : i32
    %134 = arith.constant 1 : i32
    %136 = arith.extsi %134 : i32 to i64
    %135 = arith.shli %136, %97 : i64
    %137 = arith.subi %135, %arg0 : i64
    %133 = func.call @g(%arg0, %137) : (i64, i64) -> i64
    %139 = arith.extsi %132 : i32 to i64
    %138 = arith.muli %139, %133 : i64
    %140 = arith.addi %131, %138 : i64
    llvm.store %140, %113 : i64, !llvm.ptr
    %141 = llvm.load %113 : !llvm.ptr -> i64
    func.return %141 : i64
  }
  func.func @main() -> i32 {
    %142 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %144 = arith.constant 123456789 : i32
    %145 = arith.extsi %144 : i32 to i64
    %143 = func.call @S(%145) : (i64) -> i64
    %146 = llvm.call @printf(%142, %143) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %147 = arith.constant 0 : i32
    func.return %147 : i32
  }
}