Problem 684

Inverse digit sum: sum_{i=2..90} S(F_i) mod 1e9+7.

Answer922058210
Output922058210
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(n)
Space complexityO(1)O(n)
ApproachFlow solutionBig-integer arithmetic
VerdictOptimal

Flow source

# Project Euler 684
# Inverse digit sum: sum_{i=2..90} S(F_i) mod 1e9+7.

const MOD: i64 = 1000000007

function modpow(base0: i64, exp0: i64, mod: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = base0 % mod
    let mut e: i64 = exp0
    while e > 0 {
        if (e & 1) == 1 {
            let t: i128 = (r as i128) * (b as i128) % (mod as i128)
            r = t as i64
        }
        let t2: i128 = (b as i128) * (b as i128) % (mod as i128)
        b = t2 as i64
        e = e / 2
    }
    return r
}

function S_mod(k: i64) -> i64 {
    if k <= 0 { return 0 }
    let q: i64 = k / 9
    let r: i64 = k % 9
    let p10: i64 = modpow(10, q, MOD)
    let mut part_full: i64 = (5 * ((p10 - 1 + MOD) % MOD) - 9 * (q % MOD)) % MOD
    if part_full < 0 { part_full = part_full + MOD }
    let tri: i64 = (r + 1) * (r + 2) / 2
    let mut part_tail: i64 = (p10 * (tri % MOD) - (r + 1)) % MOD
    if part_tail < 0 { part_tail = part_tail + MOD }
    return (part_full + part_tail) % MOD
}

function main() -> i32 {
    let mut total: i64 = 0
    let mut f0: i64 = 0
    let mut f1: i64 = 1
    let mut i: i64 = 2
    while i <= 90 {
        let next: i64 = f0 + f1
        f0 = f1
        f1 = next
        total = (total + S_mod(f1)) % MOD
        i = i + 1
    }
    printf("%lld\n", total)
    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);
int64_t S_mod_i64(int64_t k);
int32_t main(void);

static const int64_t MOD = 1000000007;

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));
    int64_t e = exp0;
    while (e > 0) {
        if ((e & 1) == 1) {
            __int128 t = FLOW_CHECKED_MOD(((((__int128)(r)) * ((__int128)(b)))), (((__int128)(mod))));
            r = ((int64_t)(t));
        }
        __int128 t2 = FLOW_CHECKED_MOD(((((__int128)(b)) * ((__int128)(b)))), (((__int128)(mod))));
        b = ((int64_t)(t2));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return r;
}

int64_t S_mod_i64(int64_t k) {
    if (k <= 0) {
        return 0;
    }
    int64_t q = FLOW_CHECKED_DIV((k), (9));
    int64_t r = FLOW_CHECKED_MOD((k), (9));
    int64_t p10 = modpow_i64_i64_i64(10, q, MOD);
    int64_t part_full = FLOW_CHECKED_MOD((((5 * FLOW_CHECKED_MOD((((p10 - 1) + MOD)), (MOD))) - (9 * FLOW_CHECKED_MOD((q), (MOD))))), (MOD));
    if (part_full < 0) {
        part_full = (part_full + MOD);
    }
    int64_t tri = FLOW_CHECKED_DIV((((r + 1) * (r + 2))), (2));
    int64_t part_tail = FLOW_CHECKED_MOD((((p10 * FLOW_CHECKED_MOD((tri), (MOD))) - (r + 1))), (MOD));
    if (part_tail < 0) {
        part_tail = (part_tail + MOD);
    }
    return FLOW_CHECKED_MOD(((part_full + part_tail)), (MOD));
}

int32_t main(void) {
    int64_t total = 0;
    int64_t f0 = 0;
    int64_t f1 = 1;
    int64_t i = 2;
    while (i <= 90) {
        int64_t next = (f0 + f1);
        f0 = f1;
        f1 = next;
        total = FLOW_CHECKED_MOD(((total + S_mod_i64(f1))), (MOD));
        i = (i + 1);
    }
    printf("%lld\n", total);
    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>
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
  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.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 = arith.extsi %20 : i64 to i128
        %22 = llvm.load %6 : !llvm.ptr -> i64
        %23 = arith.extsi %22 : i64 to i128
        %25 = arith.trunci %21 : i128 to i64
        %26 = arith.trunci %23 : i128 to i64
        %24 = arith.muli %25, %26 : i64
        %27 = arith.extsi %arg2 : i64 to i128
        %29 = arith.trunci %27 : i128 to i64
        %28 = arith.remsi %24, %29 : i64
        %30 = arith.extsi %28 : i64 to i128
        %31 = arith.trunci %30 : i128 to i64
        llvm.store %31, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %32 = llvm.load %6 : !llvm.ptr -> i64
      %33 = arith.extsi %32 : i64 to i128
      %34 = llvm.load %6 : !llvm.ptr -> i64
      %35 = arith.extsi %34 : i64 to i128
      %37 = arith.trunci %33 : i128 to i64
      %38 = arith.trunci %35 : i128 to i64
      %36 = arith.muli %37, %38 : i64
      %39 = arith.extsi %arg2 : i64 to i128
      %41 = arith.trunci %39 : i128 to i64
      %40 = arith.remsi %36, %41 : i64
      %42 = arith.extsi %40 : i64 to i128
      %43 = arith.trunci %42 : i128 to i64
      llvm.store %43, %6 : i64, !llvm.ptr
      %44 = llvm.load %8 : !llvm.ptr -> i64
      %45 = arith.constant 2 : i32
      %47 = arith.extsi %45 : i32 to i64
      %46 = arith.divsi %44, %47 : i64
      llvm.store %46, %8 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %48 = llvm.load %3 : !llvm.ptr -> i64
    func.return %48 : i64
  }
  func.func @S_mod(%arg0: i64) -> i64 {
    %49 = arith.constant 0 : i32
    %51 = arith.extsi %49 : i32 to i64
    %50 = arith.cmpi sle, %arg0, %51 : i64
    cf.cond_br %50, ^bb6, ^bb7
    ^bb6:
      %52 = arith.constant 0 : i32
      %53 = arith.extsi %52 : i32 to i64
      func.return %53 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %54 = arith.constant 9 : i32
    %56 = arith.extsi %54 : i32 to i64
    %55 = arith.divsi %arg0, %56 : i64
    %57 = arith.constant 9 : i32
    %59 = arith.extsi %57 : i32 to i64
    %58 = arith.remsi %arg0, %59 : i64
    %61 = arith.constant 10 : i32
    %62 = llvm.mlir.addressof @MOD : !llvm.ptr
    %63 = llvm.load %62 : !llvm.ptr -> i64
    %64 = arith.extsi %61 : i32 to i64
    %60 = func.call @modpow(%64, %55, %63) : (i64, i64, i64) -> i64
    %65 = arith.constant 5 : i32
    %66 = arith.constant 1 : i32
    %68 = arith.extsi %66 : i32 to i64
    %67 = arith.subi %60, %68 : i64
    %69 = llvm.mlir.addressof @MOD : !llvm.ptr
    %70 = llvm.load %69 : !llvm.ptr -> i64
    %71 = arith.addi %67, %70 : i64
    %72 = llvm.mlir.addressof @MOD : !llvm.ptr
    %73 = llvm.load %72 : !llvm.ptr -> i64
    %74 = arith.remsi %71, %73 : i64
    %76 = arith.extsi %65 : i32 to i64
    %75 = arith.muli %76, %74 : i64
    %77 = arith.constant 9 : i32
    %78 = llvm.mlir.addressof @MOD : !llvm.ptr
    %79 = llvm.load %78 : !llvm.ptr -> i64
    %80 = arith.remsi %55, %79 : i64
    %82 = arith.extsi %77 : i32 to i64
    %81 = arith.muli %82, %80 : i64
    %83 = arith.subi %75, %81 : i64
    %84 = llvm.mlir.addressof @MOD : !llvm.ptr
    %85 = llvm.load %84 : !llvm.ptr -> i64
    %86 = arith.remsi %83, %85 : i64
    %87 = llvm.mlir.constant(1 : i64) : i64
    %88 = llvm.alloca %87 x i64 : (i64) -> !llvm.ptr
    llvm.store %86, %88 : i64, !llvm.ptr
    %89 = llvm.load %88 : !llvm.ptr -> i64
    %90 = arith.constant 0 : i32
    %92 = arith.extsi %90 : i32 to i64
    %91 = arith.cmpi slt, %89, %92 : i64
    cf.cond_br %91, ^bb9, ^bb10
    ^bb9:
      %93 = llvm.load %88 : !llvm.ptr -> i64
      %94 = llvm.mlir.addressof @MOD : !llvm.ptr
      %95 = llvm.load %94 : !llvm.ptr -> i64
      %96 = arith.addi %93, %95 : i64
      llvm.store %96, %88 : i64, !llvm.ptr
      cf.br ^bb11
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %97 = arith.constant 1 : i32
    %99 = arith.extsi %97 : i32 to i64
    %98 = arith.addi %58, %99 : i64
    %100 = arith.constant 2 : i32
    %102 = arith.extsi %100 : i32 to i64
    %101 = arith.addi %58, %102 : i64
    %103 = arith.muli %98, %101 : i64
    %104 = arith.constant 2 : i32
    %106 = arith.extsi %104 : i32 to i64
    %105 = arith.divsi %103, %106 : i64
    %107 = llvm.mlir.addressof @MOD : !llvm.ptr
    %108 = llvm.load %107 : !llvm.ptr -> i64
    %109 = arith.remsi %105, %108 : i64
    %110 = arith.muli %60, %109 : i64
    %111 = arith.constant 1 : i32
    %113 = arith.extsi %111 : i32 to i64
    %112 = arith.addi %58, %113 : i64
    %114 = arith.subi %110, %112 : i64
    %115 = llvm.mlir.addressof @MOD : !llvm.ptr
    %116 = llvm.load %115 : !llvm.ptr -> i64
    %117 = arith.remsi %114, %116 : i64
    %118 = llvm.mlir.constant(1 : i64) : i64
    %119 = llvm.alloca %118 x i64 : (i64) -> !llvm.ptr
    llvm.store %117, %119 : i64, !llvm.ptr
    %120 = llvm.load %119 : !llvm.ptr -> i64
    %121 = arith.constant 0 : i32
    %123 = arith.extsi %121 : i32 to i64
    %122 = arith.cmpi slt, %120, %123 : i64
    cf.cond_br %122, ^bb12, ^bb13
    ^bb12:
      %124 = llvm.load %119 : !llvm.ptr -> i64
      %125 = llvm.mlir.addressof @MOD : !llvm.ptr
      %126 = llvm.load %125 : !llvm.ptr -> i64
      %127 = arith.addi %124, %126 : i64
      llvm.store %127, %119 : i64, !llvm.ptr
      cf.br ^bb14
    ^bb13:
      cf.br ^bb14
    ^bb14:
    %128 = llvm.load %88 : !llvm.ptr -> i64
    %129 = llvm.load %119 : !llvm.ptr -> i64
    %130 = arith.addi %128, %129 : i64
    %131 = llvm.mlir.addressof @MOD : !llvm.ptr
    %132 = llvm.load %131 : !llvm.ptr -> i64
    %133 = arith.remsi %130, %132 : i64
    func.return %133 : i64
  }
  func.func @main() -> i32 {
    %134 = arith.constant 0 : i32
    %135 = arith.extsi %134 : i32 to i64
    %136 = llvm.mlir.constant(1 : i64) : i64
    %137 = llvm.alloca %136 x i64 : (i64) -> !llvm.ptr
    llvm.store %135, %137 : i64, !llvm.ptr
    %138 = arith.constant 0 : i32
    %139 = arith.extsi %138 : i32 to i64
    %140 = llvm.mlir.constant(1 : i64) : i64
    %141 = llvm.alloca %140 x i64 : (i64) -> !llvm.ptr
    llvm.store %139, %141 : i64, !llvm.ptr
    %142 = arith.constant 1 : i32
    %143 = arith.extsi %142 : i32 to i64
    %144 = llvm.mlir.constant(1 : i64) : i64
    %145 = llvm.alloca %144 x i64 : (i64) -> !llvm.ptr
    llvm.store %143, %145 : i64, !llvm.ptr
    %146 = arith.constant 2 : i32
    %147 = arith.extsi %146 : i32 to i64
    %148 = llvm.mlir.constant(1 : i64) : i64
    %149 = llvm.alloca %148 x i64 : (i64) -> !llvm.ptr
    llvm.store %147, %149 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %150 = llvm.load %149 : !llvm.ptr -> i64
    %151 = arith.constant 90 : i32
    %153 = arith.extsi %151 : i32 to i64
    %152 = arith.cmpi sle, %150, %153 : i64
    cf.cond_br %152, ^bb16, ^bb17
    ^bb16:
      %154 = llvm.load %141 : !llvm.ptr -> i64
      %155 = llvm.load %145 : !llvm.ptr -> i64
      %156 = arith.addi %154, %155 : i64
      %157 = llvm.load %145 : !llvm.ptr -> i64
      llvm.store %157, %141 : i64, !llvm.ptr
      llvm.store %156, %145 : i64, !llvm.ptr
      %158 = llvm.load %137 : !llvm.ptr -> i64
      %160 = llvm.load %145 : !llvm.ptr -> i64
      %159 = func.call @S_mod(%160) : (i64) -> i64
      %161 = arith.addi %158, %159 : i64
      %162 = llvm.mlir.addressof @MOD : !llvm.ptr
      %163 = llvm.load %162 : !llvm.ptr -> i64
      %164 = arith.remsi %161, %163 : i64
      llvm.store %164, %137 : i64, !llvm.ptr
      %165 = llvm.load %149 : !llvm.ptr -> i64
      %166 = arith.constant 1 : i32
      %168 = arith.extsi %166 : i32 to i64
      %167 = arith.addi %165, %168 : i64
      llvm.store %167, %149 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %169 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %170 = llvm.load %137 : !llvm.ptr -> i64
    %171 = llvm.call @printf(%169, %170) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %172 = arith.constant 0 : i32
    func.return %172 : i32
  }
}