Problem 716

C(H,W) mod 10^9+7 closed form for directed grid graphs.

Answer238948623
Output238948623
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(e log v)
Space complexityO(1)O(v)
ApproachFlow solutionKruskal or Prim algorithm
VerdictUnknown

Flow source

# Project Euler 716
# C(H,W) mod 10^9+7 closed form for directed grid graphs.

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 { r = (r * b) % mod }
        b = (b * b) % mod
        e = e >> 1
    }
    return r
}

function C(h: i64, w: i64) -> i64 {
    let mod: i64 = 1000000007
    let pow2h: i64 = modpow(2, h, mod)
    let pow2w: i64 = modpow(2, w, mod)
    let pow2hw: i64 = (pow2h * pow2w) % mod
    let term1: i64 = (9 * pow2hw) % mod
    let mut term2: i64 = (2 * (h % mod) % mod * (w % mod)) % mod
    term2 = (term2 * ((pow2h + pow2w + 1) % mod)) % mod
    let mut term3: i64 = (8 * (((w % mod) * pow2h + (h % mod) * pow2w) % mod)) % mod
    term3 = (mod - term3) % mod
    let mut term4: i64 = (10 * ((pow2h + pow2w) % mod)) % mod
    term4 = (mod - term4) % mod
    let term5: i64 = (10 * ((h + w + 1) % mod)) % mod
    return (((((term1 + term2) % mod) + term3) % mod + term4) % mod + term5) % mod
}

function main() -> i32 {
    printf("%lld\n", C(10000, 20000))
    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 C_i64_i64(int64_t h, int64_t w);
int32_t main(void);

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) {
            r = FLOW_CHECKED_MOD(((r * b)), (mod));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (mod));
        e = FLOW_CHECKED_SHR((e), (1));
    }
    return r;
}

int64_t C_i64_i64(int64_t h, int64_t w) {
    int64_t mod = 1000000007;
    int64_t pow2h = modpow_i64_i64_i64(2, h, mod);
    int64_t pow2w = modpow_i64_i64_i64(2, w, mod);
    int64_t pow2hw = FLOW_CHECKED_MOD(((pow2h * pow2w)), (mod));
    int64_t term1 = FLOW_CHECKED_MOD(((9 * pow2hw)), (mod));
    int64_t term2 = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((2 * FLOW_CHECKED_MOD((h), (mod)))), (mod)) * FLOW_CHECKED_MOD((w), (mod)))), (mod));
    term2 = FLOW_CHECKED_MOD(((term2 * FLOW_CHECKED_MOD((((pow2h + pow2w) + 1)), (mod)))), (mod));
    int64_t term3 = FLOW_CHECKED_MOD(((8 * FLOW_CHECKED_MOD((((FLOW_CHECKED_MOD((w), (mod)) * pow2h) + (FLOW_CHECKED_MOD((h), (mod)) * pow2w))), (mod)))), (mod));
    term3 = FLOW_CHECKED_MOD(((mod - term3)), (mod));
    int64_t term4 = FLOW_CHECKED_MOD(((10 * FLOW_CHECKED_MOD(((pow2h + pow2w)), (mod)))), (mod));
    term4 = FLOW_CHECKED_MOD(((mod - term4)), (mod));
    int64_t term5 = FLOW_CHECKED_MOD(((10 * FLOW_CHECKED_MOD((((h + w) + 1)), (mod)))), (mod));
    return FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((term1 + term2)), (mod)) + term3)), (mod)) + term4)), (mod)) + term5)), (mod));
}

int32_t main(void) {
    printf("%lld\n", C_i64_i64(10000, 20000));
    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 @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 = llvm.load %6 : !llvm.ptr -> i64
        %22 = arith.muli %20, %21 : i64
        %23 = arith.remsi %22, %arg2 : i64
        llvm.store %23, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %24 = llvm.load %6 : !llvm.ptr -> i64
      %25 = llvm.load %6 : !llvm.ptr -> i64
      %26 = arith.muli %24, %25 : i64
      %27 = arith.remsi %26, %arg2 : i64
      llvm.store %27, %6 : i64, !llvm.ptr
      %28 = llvm.load %8 : !llvm.ptr -> i64
      %29 = arith.constant 1 : i32
      %31 = arith.extsi %29 : i32 to i64
      %30 = arith.shrsi %28, %31 : i64
      llvm.store %30, %8 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %32 = llvm.load %3 : !llvm.ptr -> i64
    func.return %32 : i64
  }
  func.func @C(%arg0: i64, %arg1: i64) -> i64 {
    %33 = arith.constant 1000000007 : i32
    %34 = arith.extsi %33 : i32 to i64
    %36 = arith.constant 2 : i32
    %37 = arith.extsi %36 : i32 to i64
    %35 = func.call @modpow(%37, %arg0, %34) : (i64, i64, i64) -> i64
    %39 = arith.constant 2 : i32
    %40 = arith.extsi %39 : i32 to i64
    %38 = func.call @modpow(%40, %arg1, %34) : (i64, i64, i64) -> i64
    %41 = arith.muli %35, %38 : i64
    %42 = arith.remsi %41, %34 : i64
    %43 = arith.constant 9 : i32
    %45 = arith.extsi %43 : i32 to i64
    %44 = arith.muli %45, %42 : i64
    %46 = arith.remsi %44, %34 : i64
    %47 = arith.constant 2 : i32
    %48 = arith.remsi %arg0, %34 : i64
    %50 = arith.extsi %47 : i32 to i64
    %49 = arith.muli %50, %48 : i64
    %51 = arith.remsi %49, %34 : i64
    %52 = arith.remsi %arg1, %34 : i64
    %53 = arith.muli %51, %52 : i64
    %54 = arith.remsi %53, %34 : i64
    %55 = llvm.mlir.constant(1 : i64) : i64
    %56 = llvm.alloca %55 x i64 : (i64) -> !llvm.ptr
    llvm.store %54, %56 : i64, !llvm.ptr
    %57 = llvm.load %56 : !llvm.ptr -> i64
    %58 = arith.addi %35, %38 : i64
    %59 = arith.constant 1 : i32
    %61 = arith.extsi %59 : i32 to i64
    %60 = arith.addi %58, %61 : i64
    %62 = arith.remsi %60, %34 : i64
    %63 = arith.muli %57, %62 : i64
    %64 = arith.remsi %63, %34 : i64
    llvm.store %64, %56 : i64, !llvm.ptr
    %65 = arith.constant 8 : i32
    %66 = arith.remsi %arg1, %34 : i64
    %67 = arith.muli %66, %35 : i64
    %68 = arith.remsi %arg0, %34 : i64
    %69 = arith.muli %68, %38 : i64
    %70 = arith.addi %67, %69 : i64
    %71 = arith.remsi %70, %34 : i64
    %73 = arith.extsi %65 : i32 to i64
    %72 = arith.muli %73, %71 : i64
    %74 = arith.remsi %72, %34 : i64
    %75 = llvm.mlir.constant(1 : i64) : i64
    %76 = llvm.alloca %75 x i64 : (i64) -> !llvm.ptr
    llvm.store %74, %76 : i64, !llvm.ptr
    %77 = llvm.load %76 : !llvm.ptr -> i64
    %78 = arith.subi %34, %77 : i64
    %79 = arith.remsi %78, %34 : i64
    llvm.store %79, %76 : i64, !llvm.ptr
    %80 = arith.constant 10 : i32
    %81 = arith.addi %35, %38 : i64
    %82 = arith.remsi %81, %34 : i64
    %84 = arith.extsi %80 : i32 to i64
    %83 = arith.muli %84, %82 : i64
    %85 = arith.remsi %83, %34 : i64
    %86 = llvm.mlir.constant(1 : i64) : i64
    %87 = llvm.alloca %86 x i64 : (i64) -> !llvm.ptr
    llvm.store %85, %87 : i64, !llvm.ptr
    %88 = llvm.load %87 : !llvm.ptr -> i64
    %89 = arith.subi %34, %88 : i64
    %90 = arith.remsi %89, %34 : i64
    llvm.store %90, %87 : i64, !llvm.ptr
    %91 = arith.constant 10 : i32
    %92 = arith.addi %arg0, %arg1 : i64
    %93 = arith.constant 1 : i32
    %95 = arith.extsi %93 : i32 to i64
    %94 = arith.addi %92, %95 : i64
    %96 = arith.remsi %94, %34 : i64
    %98 = arith.extsi %91 : i32 to i64
    %97 = arith.muli %98, %96 : i64
    %99 = arith.remsi %97, %34 : i64
    %100 = llvm.load %56 : !llvm.ptr -> i64
    %101 = arith.addi %46, %100 : i64
    %102 = arith.remsi %101, %34 : i64
    %103 = llvm.load %76 : !llvm.ptr -> i64
    %104 = arith.addi %102, %103 : i64
    %105 = arith.remsi %104, %34 : i64
    %106 = llvm.load %87 : !llvm.ptr -> i64
    %107 = arith.addi %105, %106 : i64
    %108 = arith.remsi %107, %34 : i64
    %109 = arith.addi %108, %99 : i64
    %110 = arith.remsi %109, %34 : i64
    func.return %110 : i64
  }
  func.func @main() -> i32 {
    %111 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %113 = arith.constant 10000 : i32
    %114 = arith.constant 20000 : i32
    %115 = arith.extsi %113 : i32 to i64
    %116 = arith.extsi %114 : i32 to i64
    %112 = func.call @C(%115, %116) : (i64, i64) -> i64
    %117 = llvm.call @printf(%111, %112) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %118 = arith.constant 0 : i32
    func.return %118 : i32
  }
}