Problem 788

Dominating Numbers: D(2022) mod 10^9+7. A dominating number has more than half its digits equal. Sum over n=1..2022 and m = majority digit count.

Answer471745499
Output471745499
StatusPASS
Native helperno
Runtime20 ms
Peak memory1136 KB
Time complexityO(n^2) (estimated)
Space complexityO(n) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^2)O(n log n)
Space complexityO(n)O(n)
ApproachFlow solutionModular DP or matrix exponentiation
VerdictSuboptimal

Flow source

# Project Euler 788
# Dominating Numbers: D(2022) mod 10^9+7.
# A dominating number has more than half its digits equal.
# Sum over n=1..2022 and m = majority digit count.

const MOD: i64 = 1000000007

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

function modpow(b: i64, e: i64) -> i64 {
    let mut r: i64 = 1
    let mut bb: i64 = b % MOD
    let mut ee: i64 = e
    while ee > 0 {
        if (ee & 1) == 1 {
            r = r * bb % MOD
        }
        bb = bb * bb % MOD
        ee = ee >> 1
    }
    return r
}

function main() -> i32 {
    let N: i64 = 2022
    let fac: ptr<i64> = calloc(N + 1, 8)
    fac[0] = 1
    let mut x: i64 = 1
    while x <= N {
        fac[x] = fac[x - 1] * x % MOD
        x = x + 1
    }

    let invfac: ptr<i64> = calloc(N + 1, 8)
    invfac[N] = modpow(fac[N], MOD - 2)
    let mut x2: i64 = N
    while x2 >= 1 {
        invfac[x2 - 1] = invfac[x2] * x2 % MOD
        x2 = x2 - 1
    }

    let mut total: i64 = 0
    let mut n: i64 = 1
    while n <= N {
        let mut m: i64 = n / 2 + 1
        while m <= n {
            let c: i64 = modpow(9, n - m + 1) * fac[n] % MOD * invfac[n - m] % MOD * invfac[m] % MOD
            total = (total + c) % MOD
            m = m + 1
        }
        n = n + 1
    }

    free(fac)
    free(invfac)
    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(int64_t b, int64_t e);
int32_t main(void);

static const int64_t MOD = 1000000007;



int64_t modpow_i64_i64(int64_t b, int64_t e) {
    int64_t r = 1;
    int64_t bb = FLOW_CHECKED_MOD((b), (MOD));
    int64_t ee = e;
    while (ee > 0) {
        if ((ee & 1) == 1) {
            r = FLOW_CHECKED_MOD(((r * bb)), (MOD));
        }
        bb = FLOW_CHECKED_MOD(((bb * bb)), (MOD));
        ee = FLOW_CHECKED_SHR((ee), (1));
    }
    return r;
}

int32_t main(void) {
    int64_t N = 2022;
    int64_t* fac = (int64_t*)(calloc((N + 1), 8));
    fac[0] = 1;
    int64_t x = 1;
    while (x <= N) {
        fac[x] = FLOW_CHECKED_MOD(((fac[(x - 1)] * x)), (MOD));
        x = (x + 1);
    }
    int64_t* invfac = (int64_t*)(calloc((N + 1), 8));
    invfac[N] = modpow_i64_i64(fac[N], (MOD - 2));
    int64_t x2 = N;
    while (x2 >= 1) {
        invfac[(x2 - 1)] = FLOW_CHECKED_MOD(((invfac[x2] * x2)), (MOD));
        x2 = (x2 - 1);
    }
    int64_t total = 0;
    int64_t n = 1;
    while (n <= N) {
        int64_t m = (FLOW_CHECKED_DIV((n), (2)) + 1);
        while (m <= n) {
            int64_t c = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((modpow_i64_i64(9, ((n - m) + 1)) * fac[n])), (MOD)) * invfac[(n - m)])), (MOD)) * invfac[m])), (MOD));
            total = FLOW_CHECKED_MOD(((total + c)), (MOD));
            m = (m + 1);
        }
        n = (n + 1);
    }
    free(fac);
    free(invfac);
    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 private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  func.func @modpow(%arg0: i64, %arg1: 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 = llvm.mlir.addressof @MOD : !llvm.ptr
    %5 = llvm.load %4 : !llvm.ptr -> i64
    %6 = arith.remsi %arg0, %5 : i64
    %7 = llvm.mlir.constant(1 : i64) : i64
    %8 = llvm.alloca %7 x i64 : (i64) -> !llvm.ptr
    llvm.store %6, %8 : i64, !llvm.ptr
    %9 = llvm.mlir.constant(1 : i64) : i64
    %10 = llvm.alloca %9 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %10 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %11 = llvm.load %10 : !llvm.ptr -> i64
    %12 = arith.constant 0 : i32
    %14 = arith.extsi %12 : i32 to i64
    %13 = arith.cmpi sgt, %11, %14 : i64
    cf.cond_br %13, ^bb1, ^bb2
    ^bb1:
      %15 = llvm.load %10 : !llvm.ptr -> i64
      %16 = arith.constant 1 : i32
      %18 = arith.extsi %16 : i32 to i64
      %17 = arith.andi %15, %18 : i64
      %19 = arith.constant 1 : i32
      %21 = arith.extsi %19 : i32 to i64
      %20 = arith.cmpi eq, %17, %21 : i64
      cf.cond_br %20, ^bb3, ^bb4
      ^bb3:
        %22 = llvm.load %3 : !llvm.ptr -> i64
        %23 = llvm.load %8 : !llvm.ptr -> i64
        %24 = arith.muli %22, %23 : i64
        %25 = llvm.mlir.addressof @MOD : !llvm.ptr
        %26 = llvm.load %25 : !llvm.ptr -> i64
        %27 = arith.remsi %24, %26 : i64
        llvm.store %27, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %28 = llvm.load %8 : !llvm.ptr -> i64
      %29 = llvm.load %8 : !llvm.ptr -> i64
      %30 = arith.muli %28, %29 : i64
      %31 = llvm.mlir.addressof @MOD : !llvm.ptr
      %32 = llvm.load %31 : !llvm.ptr -> i64
      %33 = arith.remsi %30, %32 : i64
      llvm.store %33, %8 : i64, !llvm.ptr
      %34 = llvm.load %10 : !llvm.ptr -> i64
      %35 = arith.constant 1 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.shrsi %34, %37 : i64
      llvm.store %36, %10 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %38 = llvm.load %3 : !llvm.ptr -> i64
    func.return %38 : i64
  }
  func.func @main() -> i32 {
    %39 = arith.constant 2022 : i32
    %40 = arith.extsi %39 : i32 to i64
    %42 = arith.constant 1 : i32
    %44 = arith.extsi %42 : i32 to i64
    %43 = arith.addi %40, %44 : i64
    %45 = arith.constant 8 : i32
    %46 = arith.extsi %45 : i32 to i64
    %41 = func.call @calloc(%43, %46) : (i64, i64) -> !llvm.ptr
    %47 = arith.constant 1 : i32
    %48 = arith.constant 0 : i32
    %49 = arith.extsi %47 : i32 to i64
    %50 = arith.extsi %48 : i32 to i64
    %51 = llvm.getelementptr %41[%50] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %49, %51 : i64, !llvm.ptr
    %52 = arith.constant 1 : i32
    %53 = arith.extsi %52 : i32 to i64
    %54 = llvm.mlir.constant(1 : i64) : i64
    %55 = llvm.alloca %54 x i64 : (i64) -> !llvm.ptr
    llvm.store %53, %55 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %56 = llvm.load %55 : !llvm.ptr -> i64
    %57 = arith.cmpi sle, %56, %40 : i64
    cf.cond_br %57, ^bb7, ^bb8
    ^bb7:
      %59 = llvm.load %55 : !llvm.ptr -> i64
      %60 = arith.constant 1 : i32
      %62 = arith.extsi %60 : i32 to i64
      %61 = arith.subi %59, %62 : i64
      %63 = llvm.getelementptr %41[%61] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %58 = llvm.load %63 : !llvm.ptr -> i64
      %64 = llvm.load %55 : !llvm.ptr -> i64
      %65 = arith.muli %58, %64 : i64
      %66 = llvm.mlir.addressof @MOD : !llvm.ptr
      %67 = llvm.load %66 : !llvm.ptr -> i64
      %68 = arith.remsi %65, %67 : i64
      %69 = llvm.load %55 : !llvm.ptr -> i64
      %70 = llvm.getelementptr %41[%69] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %68, %70 : i64, !llvm.ptr
      %71 = llvm.load %55 : !llvm.ptr -> i64
      %72 = arith.constant 1 : i32
      %74 = arith.extsi %72 : i32 to i64
      %73 = arith.addi %71, %74 : i64
      llvm.store %73, %55 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %76 = arith.constant 1 : i32
    %78 = arith.extsi %76 : i32 to i64
    %77 = arith.addi %40, %78 : i64
    %79 = arith.constant 8 : i32
    %80 = arith.extsi %79 : i32 to i64
    %75 = func.call @calloc(%77, %80) : (i64, i64) -> !llvm.ptr
    %83 = llvm.getelementptr %41[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %82 = llvm.load %83 : !llvm.ptr -> i64
    %84 = llvm.mlir.addressof @MOD : !llvm.ptr
    %85 = llvm.load %84 : !llvm.ptr -> i64
    %86 = arith.constant 2 : i32
    %88 = arith.extsi %86 : i32 to i64
    %87 = arith.subi %85, %88 : i64
    %81 = func.call @modpow(%82, %87) : (i64, i64) -> i64
    %89 = llvm.getelementptr %75[%40] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %81, %89 : i64, !llvm.ptr
    %90 = llvm.mlir.constant(1 : i64) : i64
    %91 = llvm.alloca %90 x i64 : (i64) -> !llvm.ptr
    llvm.store %40, %91 : i64, !llvm.ptr
    cf.br ^bb9
    ^bb9:
    %92 = llvm.load %91 : !llvm.ptr -> i64
    %93 = arith.constant 1 : i32
    %95 = arith.extsi %93 : i32 to i64
    %94 = arith.cmpi sge, %92, %95 : i64
    cf.cond_br %94, ^bb10, ^bb11
    ^bb10:
      %97 = llvm.load %91 : !llvm.ptr -> i64
      %98 = llvm.getelementptr %75[%97] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %96 = llvm.load %98 : !llvm.ptr -> i64
      %99 = llvm.load %91 : !llvm.ptr -> i64
      %100 = arith.muli %96, %99 : i64
      %101 = llvm.mlir.addressof @MOD : !llvm.ptr
      %102 = llvm.load %101 : !llvm.ptr -> i64
      %103 = arith.remsi %100, %102 : i64
      %104 = llvm.load %91 : !llvm.ptr -> i64
      %105 = arith.constant 1 : i32
      %107 = arith.extsi %105 : i32 to i64
      %106 = arith.subi %104, %107 : i64
      %108 = llvm.getelementptr %75[%106] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %103, %108 : i64, !llvm.ptr
      %109 = llvm.load %91 : !llvm.ptr -> i64
      %110 = arith.constant 1 : i32
      %112 = arith.extsi %110 : i32 to i64
      %111 = arith.subi %109, %112 : i64
      llvm.store %111, %91 : i64, !llvm.ptr
      cf.br ^bb9
    ^bb11:
    %113 = arith.constant 0 : i32
    %114 = arith.extsi %113 : i32 to i64
    %115 = llvm.mlir.constant(1 : i64) : i64
    %116 = llvm.alloca %115 x i64 : (i64) -> !llvm.ptr
    llvm.store %114, %116 : i64, !llvm.ptr
    %117 = arith.constant 1 : i32
    %118 = arith.extsi %117 : i32 to i64
    %119 = llvm.mlir.constant(1 : i64) : i64
    %120 = llvm.alloca %119 x i64 : (i64) -> !llvm.ptr
    llvm.store %118, %120 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %121 = llvm.load %120 : !llvm.ptr -> i64
    %122 = arith.cmpi sle, %121, %40 : i64
    cf.cond_br %122, ^bb13, ^bb14
    ^bb13:
      %123 = llvm.load %120 : !llvm.ptr -> i64
      %124 = arith.constant 2 : i32
      %126 = arith.extsi %124 : i32 to i64
      %125 = arith.divsi %123, %126 : i64
      %127 = arith.constant 1 : i32
      %129 = arith.extsi %127 : i32 to i64
      %128 = arith.addi %125, %129 : i64
      %130 = llvm.mlir.constant(1 : i64) : i64
      %131 = llvm.alloca %130 x i64 : (i64) -> !llvm.ptr
      llvm.store %128, %131 : i64, !llvm.ptr
      cf.br ^bb15
      ^bb15:
      %132 = llvm.load %131 : !llvm.ptr -> i64
      %133 = llvm.load %120 : !llvm.ptr -> i64
      %134 = arith.cmpi sle, %132, %133 : i64
      cf.cond_br %134, ^bb16, ^bb17
      ^bb16:
        %136 = arith.constant 9 : i32
        %137 = llvm.load %120 : !llvm.ptr -> i64
        %138 = llvm.load %131 : !llvm.ptr -> i64
        %139 = arith.subi %137, %138 : i64
        %140 = arith.constant 1 : i32
        %142 = arith.extsi %140 : i32 to i64
        %141 = arith.addi %139, %142 : i64
        %143 = arith.extsi %136 : i32 to i64
        %135 = func.call @modpow(%143, %141) : (i64, i64) -> i64
        %145 = llvm.load %120 : !llvm.ptr -> i64
        %146 = llvm.getelementptr %41[%145] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %144 = llvm.load %146 : !llvm.ptr -> i64
        %147 = arith.muli %135, %144 : i64
        %148 = llvm.mlir.addressof @MOD : !llvm.ptr
        %149 = llvm.load %148 : !llvm.ptr -> i64
        %150 = arith.remsi %147, %149 : i64
        %152 = llvm.load %120 : !llvm.ptr -> i64
        %153 = llvm.load %131 : !llvm.ptr -> i64
        %154 = arith.subi %152, %153 : i64
        %155 = llvm.getelementptr %75[%154] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %151 = llvm.load %155 : !llvm.ptr -> i64
        %156 = arith.muli %150, %151 : i64
        %157 = llvm.mlir.addressof @MOD : !llvm.ptr
        %158 = llvm.load %157 : !llvm.ptr -> i64
        %159 = arith.remsi %156, %158 : i64
        %161 = llvm.load %131 : !llvm.ptr -> i64
        %162 = llvm.getelementptr %75[%161] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %160 = llvm.load %162 : !llvm.ptr -> i64
        %163 = arith.muli %159, %160 : i64
        %164 = llvm.mlir.addressof @MOD : !llvm.ptr
        %165 = llvm.load %164 : !llvm.ptr -> i64
        %166 = arith.remsi %163, %165 : i64
        %167 = llvm.load %116 : !llvm.ptr -> i64
        %168 = arith.addi %167, %166 : i64
        %169 = llvm.mlir.addressof @MOD : !llvm.ptr
        %170 = llvm.load %169 : !llvm.ptr -> i64
        %171 = arith.remsi %168, %170 : i64
        llvm.store %171, %116 : i64, !llvm.ptr
        %172 = llvm.load %131 : !llvm.ptr -> i64
        %173 = arith.constant 1 : i32
        %175 = arith.extsi %173 : i32 to i64
        %174 = arith.addi %172, %175 : i64
        llvm.store %174, %131 : i64, !llvm.ptr
        cf.br ^bb15
      ^bb17:
      %176 = llvm.load %120 : !llvm.ptr -> i64
      %177 = arith.constant 1 : i32
      %179 = arith.extsi %177 : i32 to i64
      %178 = arith.addi %176, %179 : i64
      llvm.store %178, %120 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    func.call @free(%41) : (!llvm.ptr) -> ()
    func.call @free(%75) : (!llvm.ptr) -> ()
    %182 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %183 = llvm.load %116 : !llvm.ptr -> i64
    %184 = llvm.call @printf(%182, %183) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %185 = arith.constant 0 : i32
    func.return %185 : i32
  }
}