Problem 288

N(61, 10^7) mod 61^10.

Answer605857431263981935
Output605857431263981935
StatusPASS
Native helperno
Runtime70 ms
Peak memory1104 KB
Time complexityO(n) (estimated)
Space complexityO(1) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n)O(n log n)
Space complexityO(1)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictOptimal

Flow source

# Project Euler 288
# N(61, 10^7) mod 61^10.

function count_factors_t(t: i64, prime: i64) -> i64 {
    let mut result: i64 = 0
    let mut power: i64 = prime
    while power <= t {
        result = result + t / power
        if power > t / prime { break }
        power = power * prime
    }
    return result
}

# valuation of (t * prime^k)! for the prime
function count_factors_pow(t: i64, k: i64, prime: i64) -> i64 {
    if t == 0 { return 0 }
    # sum_{i=1..k} t * prime^{k-i} + count_factors(t)
    let mut result: i64 = 0
    let mut pp: i64 = 1
    let mut j: i64 = 0
    while j < k {
        result = result + t * pp
        pp = pp * prime
        j = j + 1
    }
    return result + count_factors_t(t, prime)
}

function main() -> i32 {
    let prime: i64 = 61
    let iterations: i64 = 10000000
    let exponent: i64 = 10
    let mut modulo: i64 = 1
    let mut e: i64 = 0
    while e < exponent {
        modulo = modulo * prime
        e = e + 1
    }
    let mut k: i64 = 0
    let mut s: i64 = 290797
    let mut result: i64 = 0
    let mut i: i64 = 0
    while i <= iterations {
        let t: i64 = s % prime
        result = (result + count_factors_pow(t, k, prime)) % modulo
        if k < exponent {
            k = k + 1
        }
        s = (s * s) % 50515093
        i = i + 1
    }
    printf("%lld\n", result)
    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 count_factors_t_i64_i64(int64_t t, int64_t prime);
int64_t count_factors_pow_i64_i64_i64(int64_t t, int64_t k, int64_t prime);
int32_t main(void);

int64_t count_factors_t_i64_i64(int64_t t, int64_t prime) {
    int64_t result = 0;
    int64_t power = prime;
    while (power <= t) {
        result = (result + FLOW_CHECKED_DIV((t), (power)));
        if (power > FLOW_CHECKED_DIV((t), (prime))) {
            break;
        }
        power = (power * prime);
    }
    return result;
}

int64_t count_factors_pow_i64_i64_i64(int64_t t, int64_t k, int64_t prime) {
    if (t == 0) {
        return 0;
    }
    int64_t result = 0;
    int64_t pp = 1;
    int64_t j = 0;
    while (j < k) {
        result = (result + (t * pp));
        pp = (pp * prime);
        j = (j + 1);
    }
    return (result + count_factors_t_i64_i64(t, prime));
}

int32_t main(void) {
    int64_t prime = 61;
    int64_t iterations = 10000000;
    int64_t exponent = 10;
    int64_t modulo = 1;
    int64_t e = 0;
    while (e < exponent) {
        modulo = (modulo * prime);
        e = (e + 1);
    }
    int64_t k = 0;
    int64_t s = 290797;
    int64_t result = 0;
    int64_t i = 0;
    while (i <= iterations) {
        int64_t t = FLOW_CHECKED_MOD((s), (prime));
        result = FLOW_CHECKED_MOD(((result + count_factors_pow_i64_i64_i64(t, k, prime))), (modulo));
        if (k < exponent) {
            k = (k + 1);
        }
        s = FLOW_CHECKED_MOD(((s * s)), (50515093));
        i = (i + 1);
    }
    printf("%lld\n", result);
    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 @count_factors_t(%arg0: i64, %arg1: i64) -> i64 {
    %0 = arith.constant 0 : 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 = llvm.mlir.constant(1 : i64) : i64
    %5 = llvm.alloca %4 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %5 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %6 = llvm.load %5 : !llvm.ptr -> i64
    %7 = arith.cmpi sle, %6, %arg0 : i64
    cf.cond_br %7, ^bb1, ^bb2
    ^bb1:
      %8 = llvm.load %3 : !llvm.ptr -> i64
      %9 = llvm.load %5 : !llvm.ptr -> i64
      %10 = arith.divsi %arg0, %9 : i64
      %11 = arith.addi %8, %10 : i64
      llvm.store %11, %3 : i64, !llvm.ptr
      %12 = llvm.load %5 : !llvm.ptr -> i64
      %13 = arith.divsi %arg0, %arg1 : i64
      %14 = arith.cmpi sgt, %12, %13 : i64
      cf.cond_br %14, ^bb3, ^bb4
      ^bb3:
        cf.br ^bb2
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %15 = llvm.load %5 : !llvm.ptr -> i64
      %16 = arith.muli %15, %arg1 : i64
      llvm.store %16, %5 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %17 = llvm.load %3 : !llvm.ptr -> i64
    func.return %17 : i64
  }
  func.func @count_factors_pow(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %18 = arith.constant 0 : i32
    %20 = arith.extsi %18 : i32 to i64
    %19 = arith.cmpi eq, %arg0, %20 : i64
    cf.cond_br %19, ^bb6, ^bb7
    ^bb6:
      %21 = arith.constant 0 : i32
      %22 = arith.extsi %21 : i32 to i64
      func.return %22 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %23 = arith.constant 0 : i32
    %24 = arith.extsi %23 : i32 to i64
    %25 = llvm.mlir.constant(1 : i64) : i64
    %26 = llvm.alloca %25 x i64 : (i64) -> !llvm.ptr
    llvm.store %24, %26 : i64, !llvm.ptr
    %27 = arith.constant 1 : i32
    %28 = arith.extsi %27 : i32 to i64
    %29 = llvm.mlir.constant(1 : i64) : i64
    %30 = llvm.alloca %29 x i64 : (i64) -> !llvm.ptr
    llvm.store %28, %30 : i64, !llvm.ptr
    %31 = arith.constant 0 : i32
    %32 = arith.extsi %31 : i32 to i64
    %33 = llvm.mlir.constant(1 : i64) : i64
    %34 = llvm.alloca %33 x i64 : (i64) -> !llvm.ptr
    llvm.store %32, %34 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %35 = llvm.load %34 : !llvm.ptr -> i64
    %36 = arith.cmpi slt, %35, %arg1 : i64
    cf.cond_br %36, ^bb10, ^bb11
    ^bb10:
      %37 = llvm.load %26 : !llvm.ptr -> i64
      %38 = llvm.load %30 : !llvm.ptr -> i64
      %39 = arith.muli %arg0, %38 : i64
      %40 = arith.addi %37, %39 : i64
      llvm.store %40, %26 : i64, !llvm.ptr
      %41 = llvm.load %30 : !llvm.ptr -> i64
      %42 = arith.muli %41, %arg2 : i64
      llvm.store %42, %30 : i64, !llvm.ptr
      %43 = llvm.load %34 : !llvm.ptr -> i64
      %44 = arith.constant 1 : i32
      %46 = arith.extsi %44 : i32 to i64
      %45 = arith.addi %43, %46 : i64
      llvm.store %45, %34 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %47 = llvm.load %26 : !llvm.ptr -> i64
    %48 = func.call @count_factors_t(%arg0, %arg2) : (i64, i64) -> i64
    %49 = arith.addi %47, %48 : i64
    func.return %49 : i64
  }
  func.func @main() -> i32 {
    %50 = arith.constant 61 : i32
    %51 = arith.extsi %50 : i32 to i64
    %52 = arith.constant 10000000 : i32
    %53 = arith.extsi %52 : i32 to i64
    %54 = arith.constant 10 : i32
    %55 = arith.extsi %54 : i32 to i64
    %56 = arith.constant 1 : i32
    %57 = arith.extsi %56 : i32 to i64
    %58 = llvm.mlir.constant(1 : i64) : i64
    %59 = llvm.alloca %58 x i64 : (i64) -> !llvm.ptr
    llvm.store %57, %59 : i64, !llvm.ptr
    %60 = arith.constant 0 : i32
    %61 = arith.extsi %60 : i32 to i64
    %62 = llvm.mlir.constant(1 : i64) : i64
    %63 = llvm.alloca %62 x i64 : (i64) -> !llvm.ptr
    llvm.store %61, %63 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %64 = llvm.load %63 : !llvm.ptr -> i64
    %65 = arith.cmpi slt, %64, %55 : i64
    cf.cond_br %65, ^bb13, ^bb14
    ^bb13:
      %66 = llvm.load %59 : !llvm.ptr -> i64
      %67 = arith.muli %66, %51 : i64
      llvm.store %67, %59 : i64, !llvm.ptr
      %68 = llvm.load %63 : !llvm.ptr -> i64
      %69 = arith.constant 1 : i32
      %71 = arith.extsi %69 : i32 to i64
      %70 = arith.addi %68, %71 : i64
      llvm.store %70, %63 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %72 = arith.constant 0 : i32
    %73 = arith.extsi %72 : i32 to i64
    %74 = llvm.mlir.constant(1 : i64) : i64
    %75 = llvm.alloca %74 x i64 : (i64) -> !llvm.ptr
    llvm.store %73, %75 : i64, !llvm.ptr
    %76 = arith.constant 290797 : i32
    %77 = arith.extsi %76 : i32 to i64
    %78 = llvm.mlir.constant(1 : i64) : i64
    %79 = llvm.alloca %78 x i64 : (i64) -> !llvm.ptr
    llvm.store %77, %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
    %84 = arith.constant 0 : i32
    %85 = arith.extsi %84 : i32 to i64
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
    llvm.store %85, %87 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %88 = llvm.load %87 : !llvm.ptr -> i64
    %89 = arith.cmpi sle, %88, %53 : i64
    cf.cond_br %89, ^bb16, ^bb17
    ^bb16:
      %90 = llvm.load %79 : !llvm.ptr -> i64
      %91 = arith.remsi %90, %51 : i64
      %92 = llvm.load %83 : !llvm.ptr -> i64
      %94 = llvm.load %75 : !llvm.ptr -> i64
      %93 = func.call @count_factors_pow(%91, %94, %51) : (i64, i64, i64) -> i64
      %95 = arith.addi %92, %93 : i64
      %96 = llvm.load %59 : !llvm.ptr -> i64
      %97 = arith.remsi %95, %96 : i64
      llvm.store %97, %83 : i64, !llvm.ptr
      %98 = llvm.load %75 : !llvm.ptr -> i64
      %99 = arith.cmpi slt, %98, %55 : i64
      cf.cond_br %99, ^bb18, ^bb19
      ^bb18:
        %100 = llvm.load %75 : !llvm.ptr -> i64
        %101 = arith.constant 1 : i32
        %103 = arith.extsi %101 : i32 to i64
        %102 = arith.addi %100, %103 : i64
        llvm.store %102, %75 : i64, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %104 = llvm.load %79 : !llvm.ptr -> i64
      %105 = llvm.load %79 : !llvm.ptr -> i64
      %106 = arith.muli %104, %105 : i64
      %107 = arith.constant 50515093 : i32
      %109 = arith.extsi %107 : i32 to i64
      %108 = arith.remsi %106, %109 : i64
      llvm.store %108, %79 : i64, !llvm.ptr
      %110 = llvm.load %87 : !llvm.ptr -> i64
      %111 = arith.constant 1 : i32
      %113 = arith.extsi %111 : i32 to i64
      %112 = arith.addi %110, %113 : i64
      llvm.store %112, %87 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %114 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %115 = llvm.load %83 : !llvm.ptr -> i64
    %116 = llvm.call @printf(%114, %115) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %117 = arith.constant 0 : i32
    func.return %117 : i32
  }
}