Problem 916

Restricted Permutations: P(n) = Catalan(n)^2 * (1 + (3n/(n+2))^2) mod 1e9+7. n = 10^8.

Answer877789135
Output877789135
StatusPASS
Native helperno
Runtime980 ms
Peak memory1104 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 solutionPermutation enumeration or constraint search
VerdictOptimal

Flow source

# Project Euler 916
# Restricted Permutations: P(n) = Catalan(n)^2 * (1 + (3n/(n+2))^2) mod 1e9+7.
# n = 10^8.

const MOD: i64 = 1000000007

function mod_pow(base: i64, exp: i64, mod: i64) -> i64 {
    let mut result: i64 = 1
    let mut b: i64 = base % mod
    let mut e: i64 = exp
    while e > 0 {
        if e % 2 == 1 {
            result = result * b % mod
        }
        b = b * b % mod
        e = e / 2
    }
    return result
}

function main() -> i32 {
    let n: i64 = 100000000
    let mut fac_n: i64 = 1
    let mut acc: i64 = 1
    let mut i: i64 = 1
    while i <= 2 * n {
        acc = acc * (i % MOD) % MOD
        if i == n {
            fac_n = acc
        }
        i = i + 1
    }
    let fac_2n: i64 = acc

    let inv_fac_n: i64 = mod_pow(fac_n, MOD - 2, MOD)
    let binom_2n_n: i64 = fac_2n * inv_fac_n % MOD * inv_fac_n % MOD
    let catalan: i64 = binom_2n_n * mod_pow(n + 1, MOD - 2, MOD) % MOD

    let t: i64 = 3 * n % MOD * mod_pow(n + 2, MOD - 2, MOD) % MOD

    let ans: i64 = catalan * catalan % MOD * ((1 + t * t % MOD) % MOD) % MOD
    printf("%lld\n", ans)
    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 mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int32_t main(void);

static const int64_t MOD = 1000000007;

int64_t mod_pow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod) {
    int64_t result = 1;
    int64_t b = FLOW_CHECKED_MOD((base), (mod));
    int64_t e = exp;
    while (e > 0) {
        if (FLOW_CHECKED_MOD((e), (2)) == 1) {
            result = FLOW_CHECKED_MOD(((result * b)), (mod));
        }
        b = FLOW_CHECKED_MOD(((b * b)), (mod));
        e = FLOW_CHECKED_DIV((e), (2));
    }
    return result;
}

int32_t main(void) {
    int64_t n = 100000000;
    int64_t fac_n = 1;
    int64_t acc = 1;
    int64_t i = 1;
    while (i <= (2 * n)) {
        acc = FLOW_CHECKED_MOD(((acc * FLOW_CHECKED_MOD((i), (MOD)))), (MOD));
        if (i == n) {
            fac_n = acc;
        }
        i = (i + 1);
    }
    int64_t fac_2n = acc;
    int64_t inv_fac_n = mod_pow_i64_i64_i64(fac_n, (MOD - 2), MOD);
    int64_t binom_2n_n = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((fac_2n * inv_fac_n)), (MOD)) * inv_fac_n)), (MOD));
    int64_t catalan = FLOW_CHECKED_MOD(((binom_2n_n * mod_pow_i64_i64_i64((n + 1), (MOD - 2), MOD))), (MOD));
    int64_t t = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((3 * n)), (MOD)) * mod_pow_i64_i64_i64((n + 2), (MOD - 2), MOD))), (MOD));
    int64_t ans = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((catalan * catalan)), (MOD)) * FLOW_CHECKED_MOD(((1 + FLOW_CHECKED_MOD(((t * t)), (MOD)))), (MOD)))), (MOD));
    printf("%lld\n", ans);
    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 @mod_pow(%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 2 : i32
      %16 = arith.extsi %14 : i32 to i64
      %15 = arith.remsi %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 2 : i32
      %31 = arith.extsi %29 : i32 to i64
      %30 = arith.divsi %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 @main() -> i32 {
    %33 = arith.constant 100000000 : i32
    %34 = arith.extsi %33 : i32 to i64
    %35 = arith.constant 1 : i32
    %36 = arith.extsi %35 : i32 to i64
    %37 = llvm.mlir.constant(1 : i64) : i64
    %38 = llvm.alloca %37 x i64 : (i64) -> !llvm.ptr
    llvm.store %36, %38 : i64, !llvm.ptr
    %39 = arith.constant 1 : i32
    %40 = arith.extsi %39 : i32 to i64
    %41 = llvm.mlir.constant(1 : i64) : i64
    %42 = llvm.alloca %41 x i64 : (i64) -> !llvm.ptr
    llvm.store %40, %42 : i64, !llvm.ptr
    %43 = arith.constant 1 : i32
    %44 = arith.extsi %43 : i32 to i64
    %45 = llvm.mlir.constant(1 : i64) : i64
    %46 = llvm.alloca %45 x i64 : (i64) -> !llvm.ptr
    llvm.store %44, %46 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %47 = llvm.load %46 : !llvm.ptr -> i64
    %48 = arith.constant 2 : i32
    %50 = arith.extsi %48 : i32 to i64
    %49 = arith.muli %50, %34 : i64
    %51 = arith.cmpi sle, %47, %49 : i64
    cf.cond_br %51, ^bb7, ^bb8
    ^bb7:
      %52 = llvm.load %42 : !llvm.ptr -> i64
      %53 = llvm.load %46 : !llvm.ptr -> i64
      %54 = llvm.mlir.addressof @MOD : !llvm.ptr
      %55 = llvm.load %54 : !llvm.ptr -> i64
      %56 = arith.remsi %53, %55 : i64
      %57 = arith.muli %52, %56 : i64
      %58 = llvm.mlir.addressof @MOD : !llvm.ptr
      %59 = llvm.load %58 : !llvm.ptr -> i64
      %60 = arith.remsi %57, %59 : i64
      llvm.store %60, %42 : i64, !llvm.ptr
      %61 = llvm.load %46 : !llvm.ptr -> i64
      %62 = arith.cmpi eq, %61, %34 : i64
      cf.cond_br %62, ^bb9, ^bb10
      ^bb9:
        %63 = llvm.load %42 : !llvm.ptr -> i64
        llvm.store %63, %38 : i64, !llvm.ptr
        cf.br ^bb11
      ^bb10:
        cf.br ^bb11
      ^bb11:
      %64 = llvm.load %46 : !llvm.ptr -> i64
      %65 = arith.constant 1 : i32
      %67 = arith.extsi %65 : i32 to i64
      %66 = arith.addi %64, %67 : i64
      llvm.store %66, %46 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    %68 = llvm.load %42 : !llvm.ptr -> i64
    %70 = llvm.load %38 : !llvm.ptr -> i64
    %71 = llvm.mlir.addressof @MOD : !llvm.ptr
    %72 = llvm.load %71 : !llvm.ptr -> i64
    %73 = arith.constant 2 : i32
    %75 = arith.extsi %73 : i32 to i64
    %74 = arith.subi %72, %75 : i64
    %76 = llvm.mlir.addressof @MOD : !llvm.ptr
    %77 = llvm.load %76 : !llvm.ptr -> i64
    %69 = func.call @mod_pow(%70, %74, %77) : (i64, i64, i64) -> i64
    %78 = arith.muli %68, %69 : i64
    %79 = llvm.mlir.addressof @MOD : !llvm.ptr
    %80 = llvm.load %79 : !llvm.ptr -> i64
    %81 = arith.remsi %78, %80 : i64
    %82 = arith.muli %81, %69 : i64
    %83 = llvm.mlir.addressof @MOD : !llvm.ptr
    %84 = llvm.load %83 : !llvm.ptr -> i64
    %85 = arith.remsi %82, %84 : i64
    %87 = arith.constant 1 : i32
    %89 = arith.extsi %87 : i32 to i64
    %88 = arith.addi %34, %89 : i64
    %90 = llvm.mlir.addressof @MOD : !llvm.ptr
    %91 = llvm.load %90 : !llvm.ptr -> i64
    %92 = arith.constant 2 : i32
    %94 = arith.extsi %92 : i32 to i64
    %93 = arith.subi %91, %94 : i64
    %95 = llvm.mlir.addressof @MOD : !llvm.ptr
    %96 = llvm.load %95 : !llvm.ptr -> i64
    %86 = func.call @mod_pow(%88, %93, %96) : (i64, i64, i64) -> i64
    %97 = arith.muli %85, %86 : i64
    %98 = llvm.mlir.addressof @MOD : !llvm.ptr
    %99 = llvm.load %98 : !llvm.ptr -> i64
    %100 = arith.remsi %97, %99 : i64
    %101 = arith.constant 3 : i32
    %103 = arith.extsi %101 : i32 to i64
    %102 = arith.muli %103, %34 : i64
    %104 = llvm.mlir.addressof @MOD : !llvm.ptr
    %105 = llvm.load %104 : !llvm.ptr -> i64
    %106 = arith.remsi %102, %105 : i64
    %108 = arith.constant 2 : i32
    %110 = arith.extsi %108 : i32 to i64
    %109 = arith.addi %34, %110 : i64
    %111 = llvm.mlir.addressof @MOD : !llvm.ptr
    %112 = llvm.load %111 : !llvm.ptr -> i64
    %113 = arith.constant 2 : i32
    %115 = arith.extsi %113 : i32 to i64
    %114 = arith.subi %112, %115 : i64
    %116 = llvm.mlir.addressof @MOD : !llvm.ptr
    %117 = llvm.load %116 : !llvm.ptr -> i64
    %107 = func.call @mod_pow(%109, %114, %117) : (i64, i64, i64) -> i64
    %118 = arith.muli %106, %107 : i64
    %119 = llvm.mlir.addressof @MOD : !llvm.ptr
    %120 = llvm.load %119 : !llvm.ptr -> i64
    %121 = arith.remsi %118, %120 : i64
    %122 = arith.muli %100, %100 : i64
    %123 = llvm.mlir.addressof @MOD : !llvm.ptr
    %124 = llvm.load %123 : !llvm.ptr -> i64
    %125 = arith.remsi %122, %124 : i64
    %126 = arith.constant 1 : i32
    %127 = arith.muli %121, %121 : i64
    %128 = llvm.mlir.addressof @MOD : !llvm.ptr
    %129 = llvm.load %128 : !llvm.ptr -> i64
    %130 = arith.remsi %127, %129 : i64
    %132 = arith.extsi %126 : i32 to i64
    %131 = arith.addi %132, %130 : i64
    %133 = llvm.mlir.addressof @MOD : !llvm.ptr
    %134 = llvm.load %133 : !llvm.ptr -> i64
    %135 = arith.remsi %131, %134 : i64
    %136 = arith.muli %125, %135 : i64
    %137 = llvm.mlir.addressof @MOD : !llvm.ptr
    %138 = llvm.load %137 : !llvm.ptr -> i64
    %139 = arith.remsi %136, %138 : i64
    %140 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %141 = llvm.call @printf(%140, %139) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %142 = arith.constant 0 : i32
    func.return %142 : i32
  }
}