Problem 344

Silver dollar game W(10^6, 100) mod 1000036000099 via CRT.

Answer65579304332
Output65579304332
StatusPASS
Native helperno
Runtime0 ms
Peak memory1104 KB
Time complexityO(n^3) (estimated)
Space complexityO(n^2) (estimated)

Performance comparison

MetricOur solutionBest known
Time complexityO(n^3)O(n log n)
Space complexityO(n^2)O(n)
ApproachFlow solutionChinese Remainder Theorem
VerdictSuboptimal

Flow source

# Project Euler 344
# Silver dollar game W(10^6, 100) mod 1000036000099 via CRT.

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

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

function binom_mod(n: i64, k0: i64, prime: i64) -> i64 {
    if k0 < 0 || k0 > n { return 0 }
    let mut k: i64 = k0
    if k > n - k { k = n - k }
    let mut num: i64 = 1
    let mut den: i64 = 1
    let mut i: i64 = 1
    while i <= k {
        num = num * ((n - k + i) % prime) % prime
        den = den * i % prime
        i = i + 1
    }
    return num * modpow(den, prime - 2, prime) % prime
}

function build_ways(active: i64, passive: i64, mod: i64, ways: ptr<i64>) -> i32 {
    let lim: i64 = active + passive
    let mut i: i64 = 0
    while i <= lim {
        ways[i] = 0
        i = i + 1
    }
    let mut aones: i64 = 0
    while aones <= active {
        let ac: i64 = binom_mod(active, aones, mod)
        let mut pones: i64 = 0
        while pones <= passive {
            let ones: i64 = aones + pones
            ways[ones] = (ways[ones] + ac * binom_mod(passive, pones, mod)) % mod
            pones = pones + 1
        }
        aones = aones + 2
    }
    return 0
}

function count_with_ways(total_sum: i64, ways: ptr<i64>, wlen: i64, mod: i64) -> i64 {
    # Collect nonzero terms
    let terms_ones: ptr<i64> = calloc(wlen + 1, 8)
    let terms_cnt: ptr<i64> = calloc(wlen + 1, 8)
    let mut tc: i64 = 0
    let mut ones: i64 = 0
    while ones <= wlen {
        if ways[ones] != 0 {
            terms_ones[tc] = ones
            terms_cnt[tc] = ways[ones]
            tc = tc + 1
        }
        ones = ones + 1
    }
    let mut bits: i64 = 0
    let mut tmp: i64 = total_sum
    while tmp > 0 {
        bits = bits + 1
        tmp = tmp / 2
    }
    if bits < 1 { bits = 1 }

    let mut dp: ptr<i64> = calloc(2, 8)
    let mut dplen: i64 = 1
    dp[0] = 1
    let mut bit: i64 = 0
    while bit < bits {
        let target: i64 = (total_sum >> bit) & 1
        let max_carry: i64 = dplen + wlen / 2 + 2
        let next: ptr<i64> = calloc(max_carry + 1, 8)
        let mut carry: i64 = 0
        while carry < dplen {
            let value: i64 = dp[carry]
            if value != 0 {
                let mut t: i64 = 0
                while t < tc {
                    let col: i64 = carry + terms_ones[t]
                    if (col & 1) == target {
                        let nc: i64 = (col - target) / 2
                        next[nc] = (next[nc] + value * terms_cnt[t]) % mod
                    }
                    t = t + 1
                }
            }
            carry = carry + 1
        }
        free(dp)
        dp = next
        dplen = max_carry + 1
        bit = bit + 1
    }
    let ans: i64 = dp[0]
    free(dp)
    free(terms_ones)
    free(terms_cnt)
    return ans
}

function losing_count(n: i64, c: i64, mod: i64) -> i64 {
    let coin_count: i64 = c + 1
    let empty: i64 = n - coin_count
    let active: i64 = (coin_count + 1) / 2
    let passive: i64 = coin_count - active + 1
    let wlen: i64 = active + passive
    let ways: ptr<i64> = calloc(wlen + 1, 8)
    let ways2: ptr<i64> = calloc(wlen + 1, 8)
    build_ways(active, passive, mod, ways)
    build_ways(active - 1, passive, mod, ways2)
    let second: i64 = count_with_ways(empty, ways, wlen, mod)
    let other: i64 = (count_with_ways(empty + 1, ways, wlen, mod) - count_with_ways(empty + 1, ways2, wlen, mod) + mod) % mod
    free(ways)
    free(ways2)
    return (second + (coin_count - 2) % mod * other) % mod
}

function solve_prime(n: i64, c: i64, prime: i64) -> i64 {
    let coin_count: i64 = c + 1
    let total: i64 = coin_count % prime * binom_mod(n, coin_count, prime) % prime
    return (total - losing_count(n, c, prime) + prime) % prime
}

function main() -> i32 {
    let n: i64 = 1000000
    let c: i64 = 100
    let P1: i64 = 1000003
    let P2: i64 = 1000033
    let MOD: i64 = 1000036000099
    let r1: i64 = solve_prime(n, c, P1)
    let r2: i64 = solve_prime(n, c, P2)
    let t: i64 = ((r2 - r1) % P2 + P2) % P2 * modpow(P1 % P2, P2 - 2, P2) % P2
    let ans: i64 = (r1 + P1 * t) % 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 modpow_i64_i64_i64(int64_t base, int64_t exp, int64_t mod);
int64_t binom_mod_i64_i64_i64(int64_t n, int64_t k0, int64_t prime);
int32_t build_ways_i64_i64_i64_ptr_i64(int64_t active, int64_t passive, int64_t mod, int64_t* ways);
int64_t count_with_ways_i64_ptr_i64_i64_i64(int64_t total_sum, int64_t* ways, int64_t wlen, int64_t mod);
int64_t losing_count_i64_i64_i64(int64_t n, int64_t c, int64_t mod);
int64_t solve_prime_i64_i64_i64(int64_t n, int64_t c, int64_t prime);
int32_t main(void);



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

int64_t binom_mod_i64_i64_i64(int64_t n, int64_t k0, int64_t prime) {
    if ((k0 < 0 || k0 > n)) {
        return 0;
    }
    int64_t k = k0;
    if (k > (n - k)) {
        k = (n - k);
    }
    int64_t num = 1;
    int64_t den = 1;
    int64_t i = 1;
    while (i <= k) {
        num = FLOW_CHECKED_MOD(((num * FLOW_CHECKED_MOD((((n - k) + i)), (prime)))), (prime));
        den = FLOW_CHECKED_MOD(((den * i)), (prime));
        i = (i + 1);
    }
    return FLOW_CHECKED_MOD(((num * modpow_i64_i64_i64(den, (prime - 2), prime))), (prime));
}

int32_t build_ways_i64_i64_i64_ptr_i64(int64_t active, int64_t passive, int64_t mod, int64_t* ways) {
    int64_t lim = (active + passive);
    int64_t i = 0;
    while (i <= lim) {
        ways[i] = 0;
        i = (i + 1);
    }
    int64_t aones = 0;
    while (aones <= active) {
        int64_t ac = binom_mod_i64_i64_i64(active, aones, mod);
        int64_t pones = 0;
        while (pones <= passive) {
            int64_t ones = (aones + pones);
            ways[ones] = FLOW_CHECKED_MOD(((ways[ones] + (ac * binom_mod_i64_i64_i64(passive, pones, mod)))), (mod));
            pones = (pones + 1);
        }
        aones = (aones + 2);
    }
    return 0;
}

int64_t count_with_ways_i64_ptr_i64_i64_i64(int64_t total_sum, int64_t* ways, int64_t wlen, int64_t mod) {
    int64_t* terms_ones = (int64_t*)(calloc((wlen + 1), 8));
    int64_t* terms_cnt = (int64_t*)(calloc((wlen + 1), 8));
    int64_t tc = 0;
    int64_t ones = 0;
    while (ones <= wlen) {
        if (ways[ones] != 0) {
            terms_ones[tc] = ones;
            terms_cnt[tc] = ways[ones];
            tc = (tc + 1);
        }
        ones = (ones + 1);
    }
    int64_t bits = 0;
    int64_t tmp = total_sum;
    while (tmp > 0) {
        bits = (bits + 1);
        tmp = FLOW_CHECKED_DIV((tmp), (2));
    }
    if (bits < 1) {
        bits = 1;
    }
    int64_t* dp = (int64_t*)(calloc(2, 8));
    int64_t dplen = 1;
    dp[0] = 1;
    int64_t bit = 0;
    while (bit < bits) {
        int64_t target = (FLOW_CHECKED_SHR((total_sum), (bit)) & 1);
        int64_t max_carry = ((dplen + FLOW_CHECKED_DIV((wlen), (2))) + 2);
        int64_t* next = (int64_t*)(calloc((max_carry + 1), 8));
        int64_t carry = 0;
        while (carry < dplen) {
            int64_t value = dp[carry];
            if (value != 0) {
                int64_t t = 0;
                while (t < tc) {
                    int64_t col = (carry + terms_ones[t]);
                    if ((col & 1) == target) {
                        int64_t nc = FLOW_CHECKED_DIV(((col - target)), (2));
                        next[nc] = FLOW_CHECKED_MOD(((next[nc] + (value * terms_cnt[t]))), (mod));
                    }
                    t = (t + 1);
                }
            }
            carry = (carry + 1);
        }
        free(dp);
        dp = next;
        dplen = (max_carry + 1);
        bit = (bit + 1);
    }
    int64_t ans = dp[0];
    free(dp);
    free(terms_ones);
    free(terms_cnt);
    return ans;
}

int64_t losing_count_i64_i64_i64(int64_t n, int64_t c, int64_t mod) {
    int64_t coin_count = (c + 1);
    int64_t empty = (n - coin_count);
    int64_t active = FLOW_CHECKED_DIV(((coin_count + 1)), (2));
    int64_t passive = ((coin_count - active) + 1);
    int64_t wlen = (active + passive);
    int64_t* ways = (int64_t*)(calloc((wlen + 1), 8));
    int64_t* ways2 = (int64_t*)(calloc((wlen + 1), 8));
    build_ways_i64_i64_i64_ptr_i64(active, passive, mod, ways);
    build_ways_i64_i64_i64_ptr_i64((active - 1), passive, mod, ways2);
    int64_t second = count_with_ways_i64_ptr_i64_i64_i64(empty, ways, wlen, mod);
    int64_t other = FLOW_CHECKED_MOD((((count_with_ways_i64_ptr_i64_i64_i64((empty + 1), ways, wlen, mod) - count_with_ways_i64_ptr_i64_i64_i64((empty + 1), ways2, wlen, mod)) + mod)), (mod));
    free(ways);
    free(ways2);
    return FLOW_CHECKED_MOD(((second + (FLOW_CHECKED_MOD(((coin_count - 2)), (mod)) * other))), (mod));
}

int64_t solve_prime_i64_i64_i64(int64_t n, int64_t c, int64_t prime) {
    int64_t coin_count = (c + 1);
    int64_t total = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD((coin_count), (prime)) * binom_mod_i64_i64_i64(n, coin_count, prime))), (prime));
    return FLOW_CHECKED_MOD((((total - losing_count_i64_i64_i64(n, c, prime)) + prime)), (prime));
}

int32_t main(void) {
    int64_t n = 1000000;
    int64_t c = 100;
    int64_t P1 = 1000003;
    int64_t P2 = 1000033;
    int64_t MOD = 1000036000099;
    int64_t r1 = solve_prime_i64_i64_i64(n, c, P1);
    int64_t r2 = solve_prime_i64_i64_i64(n, c, P2);
    int64_t t = FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((FLOW_CHECKED_MOD(((r2 - r1)), (P2)) + P2)), (P2)) * modpow_i64_i64_i64(FLOW_CHECKED_MOD((P1), (P2)), (P2 - 2), P2))), (P2));
    int64_t ans = FLOW_CHECKED_MOD(((r1 + (P1 * t))), (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>
  func.func private @calloc(i64, i64) -> !llvm.ptr
  func.func private @free(!llvm.ptr) -> ()
  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 = arith.addi %4, %arg2 : i64
    %6 = arith.remsi %5, %arg2 : 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 2 : i32
      %18 = arith.extsi %16 : i32 to i64
      %17 = arith.remsi %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 = arith.remsi %24, %arg2 : i64
        llvm.store %25, %3 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %26 = llvm.load %8 : !llvm.ptr -> i64
      %27 = llvm.load %8 : !llvm.ptr -> i64
      %28 = arith.muli %26, %27 : i64
      %29 = arith.remsi %28, %arg2 : i64
      llvm.store %29, %8 : i64, !llvm.ptr
      %30 = llvm.load %10 : !llvm.ptr -> i64
      %31 = arith.constant 2 : i32
      %33 = arith.extsi %31 : i32 to i64
      %32 = arith.divsi %30, %33 : i64
      llvm.store %32, %10 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %34 = llvm.load %3 : !llvm.ptr -> i64
    func.return %34 : i64
  }
  func.func @binom_mod(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %35 = arith.constant 0 : i32
    %37 = arith.extsi %35 : i32 to i64
    %36 = arith.cmpi slt, %arg1, %37 : i64
    %38 = scf.if %36 -> (i1) {
      %39 = arith.constant true
      scf.yield %39 : i1
    } else {
      %40 = arith.cmpi sgt, %arg1, %arg0 : i64
      scf.yield %40 : i1
    }
    cf.cond_br %38, ^bb6, ^bb7
    ^bb6:
      %41 = arith.constant 0 : i32
      %42 = arith.extsi %41 : i32 to i64
      func.return %42 : i64
    ^bb7:
      cf.br ^bb8
    ^bb8:
    %43 = llvm.mlir.constant(1 : i64) : i64
    %44 = llvm.alloca %43 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg1, %44 : i64, !llvm.ptr
    %45 = llvm.load %44 : !llvm.ptr -> i64
    %46 = llvm.load %44 : !llvm.ptr -> i64
    %47 = arith.subi %arg0, %46 : i64
    %48 = arith.cmpi sgt, %45, %47 : i64
    cf.cond_br %48, ^bb9, ^bb10
    ^bb9:
      %49 = llvm.load %44 : !llvm.ptr -> i64
      %50 = arith.subi %arg0, %49 : i64
      llvm.store %50, %44 : i64, !llvm.ptr
      cf.br ^bb11
    ^bb10:
      cf.br ^bb11
    ^bb11:
    %51 = arith.constant 1 : i32
    %52 = arith.extsi %51 : i32 to i64
    %53 = llvm.mlir.constant(1 : i64) : i64
    %54 = llvm.alloca %53 x i64 : (i64) -> !llvm.ptr
    llvm.store %52, %54 : i64, !llvm.ptr
    %55 = arith.constant 1 : i32
    %56 = arith.extsi %55 : i32 to i64
    %57 = llvm.mlir.constant(1 : i64) : i64
    %58 = llvm.alloca %57 x i64 : (i64) -> !llvm.ptr
    llvm.store %56, %58 : i64, !llvm.ptr
    %59 = arith.constant 1 : i32
    %60 = arith.extsi %59 : i32 to i64
    %61 = llvm.mlir.constant(1 : i64) : i64
    %62 = llvm.alloca %61 x i64 : (i64) -> !llvm.ptr
    llvm.store %60, %62 : i64, !llvm.ptr
    cf.br ^bb12
    ^bb12:
    %63 = llvm.load %62 : !llvm.ptr -> i64
    %64 = llvm.load %44 : !llvm.ptr -> i64
    %65 = arith.cmpi sle, %63, %64 : i64
    cf.cond_br %65, ^bb13, ^bb14
    ^bb13:
      %66 = llvm.load %54 : !llvm.ptr -> i64
      %67 = llvm.load %44 : !llvm.ptr -> i64
      %68 = arith.subi %arg0, %67 : i64
      %69 = llvm.load %62 : !llvm.ptr -> i64
      %70 = arith.addi %68, %69 : i64
      %71 = arith.remsi %70, %arg2 : i64
      %72 = arith.muli %66, %71 : i64
      %73 = arith.remsi %72, %arg2 : i64
      llvm.store %73, %54 : i64, !llvm.ptr
      %74 = llvm.load %58 : !llvm.ptr -> i64
      %75 = llvm.load %62 : !llvm.ptr -> i64
      %76 = arith.muli %74, %75 : i64
      %77 = arith.remsi %76, %arg2 : i64
      llvm.store %77, %58 : i64, !llvm.ptr
      %78 = llvm.load %62 : !llvm.ptr -> i64
      %79 = arith.constant 1 : i32
      %81 = arith.extsi %79 : i32 to i64
      %80 = arith.addi %78, %81 : i64
      llvm.store %80, %62 : i64, !llvm.ptr
      cf.br ^bb12
    ^bb14:
    %82 = llvm.load %54 : !llvm.ptr -> i64
    %84 = llvm.load %58 : !llvm.ptr -> i64
    %85 = arith.constant 2 : i32
    %87 = arith.extsi %85 : i32 to i64
    %86 = arith.subi %arg2, %87 : i64
    %83 = func.call @modpow(%84, %86, %arg2) : (i64, i64, i64) -> i64
    %88 = arith.muli %82, %83 : i64
    %89 = arith.remsi %88, %arg2 : i64
    func.return %89 : i64
  }
  func.func @build_ways(%arg0: i64, %arg1: i64, %arg2: i64, %arg3: !llvm.ptr) -> i32 {
    %90 = arith.addi %arg0, %arg1 : i64
    %91 = arith.constant 0 : i32
    %92 = arith.extsi %91 : i32 to i64
    %93 = llvm.mlir.constant(1 : i64) : i64
    %94 = llvm.alloca %93 x i64 : (i64) -> !llvm.ptr
    llvm.store %92, %94 : i64, !llvm.ptr
    cf.br ^bb15
    ^bb15:
    %95 = llvm.load %94 : !llvm.ptr -> i64
    %96 = arith.cmpi sle, %95, %90 : i64
    cf.cond_br %96, ^bb16, ^bb17
    ^bb16:
      %97 = arith.constant 0 : i32
      %98 = llvm.load %94 : !llvm.ptr -> i64
      %99 = arith.extsi %97 : i32 to i64
      %100 = llvm.getelementptr %arg3[%98] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      llvm.store %99, %100 : i64, !llvm.ptr
      %101 = llvm.load %94 : !llvm.ptr -> i64
      %102 = arith.constant 1 : i32
      %104 = arith.extsi %102 : i32 to i64
      %103 = arith.addi %101, %104 : i64
      llvm.store %103, %94 : i64, !llvm.ptr
      cf.br ^bb15
    ^bb17:
    %105 = arith.constant 0 : i32
    %106 = arith.extsi %105 : i32 to i64
    %107 = llvm.mlir.constant(1 : i64) : i64
    %108 = llvm.alloca %107 x i64 : (i64) -> !llvm.ptr
    llvm.store %106, %108 : i64, !llvm.ptr
    cf.br ^bb18
    ^bb18:
    %109 = llvm.load %108 : !llvm.ptr -> i64
    %110 = arith.cmpi sle, %109, %arg0 : i64
    cf.cond_br %110, ^bb19, ^bb20
    ^bb19:
      %112 = llvm.load %108 : !llvm.ptr -> i64
      %111 = func.call @binom_mod(%arg0, %112, %arg2) : (i64, i64, i64) -> i64
      %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
      cf.br ^bb21
      ^bb21:
      %117 = llvm.load %116 : !llvm.ptr -> i64
      %118 = arith.cmpi sle, %117, %arg1 : i64
      cf.cond_br %118, ^bb22, ^bb23
      ^bb22:
        %119 = llvm.load %108 : !llvm.ptr -> i64
        %120 = llvm.load %116 : !llvm.ptr -> i64
        %121 = arith.addi %119, %120 : i64
        %123 = llvm.getelementptr %arg3[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %122 = llvm.load %123 : !llvm.ptr -> i64
        %125 = llvm.load %116 : !llvm.ptr -> i64
        %124 = func.call @binom_mod(%arg1, %125, %arg2) : (i64, i64, i64) -> i64
        %126 = arith.muli %111, %124 : i64
        %127 = arith.addi %122, %126 : i64
        %128 = arith.remsi %127, %arg2 : i64
        %129 = llvm.getelementptr %arg3[%121] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %128, %129 : i64, !llvm.ptr
        %130 = llvm.load %116 : !llvm.ptr -> i64
        %131 = arith.constant 1 : i32
        %133 = arith.extsi %131 : i32 to i64
        %132 = arith.addi %130, %133 : i64
        llvm.store %132, %116 : i64, !llvm.ptr
        cf.br ^bb21
      ^bb23:
      %134 = llvm.load %108 : !llvm.ptr -> i64
      %135 = arith.constant 2 : i32
      %137 = arith.extsi %135 : i32 to i64
      %136 = arith.addi %134, %137 : i64
      llvm.store %136, %108 : i64, !llvm.ptr
      cf.br ^bb18
    ^bb20:
    %138 = arith.constant 0 : i32
    func.return %138 : i32
  }
  func.func @count_with_ways(%arg0: i64, %arg1: !llvm.ptr, %arg2: i64, %arg3: i64) -> i64 {
    %140 = arith.constant 1 : i32
    %142 = arith.extsi %140 : i32 to i64
    %141 = arith.addi %arg2, %142 : i64
    %143 = arith.constant 8 : i32
    %144 = arith.extsi %143 : i32 to i64
    %139 = func.call @calloc(%141, %144) : (i64, i64) -> !llvm.ptr
    %146 = arith.constant 1 : i32
    %148 = arith.extsi %146 : i32 to i64
    %147 = arith.addi %arg2, %148 : i64
    %149 = arith.constant 8 : i32
    %150 = arith.extsi %149 : i32 to i64
    %145 = func.call @calloc(%147, %150) : (i64, i64) -> !llvm.ptr
    %151 = arith.constant 0 : i32
    %152 = arith.extsi %151 : i32 to i64
    %153 = llvm.mlir.constant(1 : i64) : i64
    %154 = llvm.alloca %153 x i64 : (i64) -> !llvm.ptr
    llvm.store %152, %154 : i64, !llvm.ptr
    %155 = arith.constant 0 : i32
    %156 = arith.extsi %155 : i32 to i64
    %157 = llvm.mlir.constant(1 : i64) : i64
    %158 = llvm.alloca %157 x i64 : (i64) -> !llvm.ptr
    llvm.store %156, %158 : i64, !llvm.ptr
    cf.br ^bb24
    ^bb24:
    %159 = llvm.load %158 : !llvm.ptr -> i64
    %160 = arith.cmpi sle, %159, %arg2 : i64
    cf.cond_br %160, ^bb25, ^bb26
    ^bb25:
      %162 = llvm.load %158 : !llvm.ptr -> i64
      %163 = llvm.getelementptr %arg1[%162] : (!llvm.ptr, i64) -> !llvm.ptr, i64
      %161 = llvm.load %163 : !llvm.ptr -> i64
      %164 = arith.constant 0 : i32
      %166 = arith.extsi %164 : i32 to i64
      %165 = arith.cmpi ne, %161, %166 : i64
      cf.cond_br %165, ^bb27, ^bb28
      ^bb27:
        %167 = llvm.load %158 : !llvm.ptr -> i64
        %168 = llvm.load %154 : !llvm.ptr -> i64
        %169 = llvm.getelementptr %139[%168] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %167, %169 : i64, !llvm.ptr
        %171 = llvm.load %158 : !llvm.ptr -> i64
        %172 = llvm.getelementptr %arg1[%171] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %170 = llvm.load %172 : !llvm.ptr -> i64
        %173 = llvm.load %154 : !llvm.ptr -> i64
        %174 = llvm.getelementptr %145[%173] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        llvm.store %170, %174 : i64, !llvm.ptr
        %175 = llvm.load %154 : !llvm.ptr -> i64
        %176 = arith.constant 1 : i32
        %178 = arith.extsi %176 : i32 to i64
        %177 = arith.addi %175, %178 : i64
        llvm.store %177, %154 : i64, !llvm.ptr
        cf.br ^bb29
      ^bb28:
        cf.br ^bb29
      ^bb29:
      %179 = llvm.load %158 : !llvm.ptr -> i64
      %180 = arith.constant 1 : i32
      %182 = arith.extsi %180 : i32 to i64
      %181 = arith.addi %179, %182 : i64
      llvm.store %181, %158 : i64, !llvm.ptr
      cf.br ^bb24
    ^bb26:
    %183 = arith.constant 0 : i32
    %184 = arith.extsi %183 : i32 to i64
    %185 = llvm.mlir.constant(1 : i64) : i64
    %186 = llvm.alloca %185 x i64 : (i64) -> !llvm.ptr
    llvm.store %184, %186 : i64, !llvm.ptr
    %187 = llvm.mlir.constant(1 : i64) : i64
    %188 = llvm.alloca %187 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %188 : i64, !llvm.ptr
    cf.br ^bb30
    ^bb30:
    %189 = llvm.load %188 : !llvm.ptr -> i64
    %190 = arith.constant 0 : i32
    %192 = arith.extsi %190 : i32 to i64
    %191 = arith.cmpi sgt, %189, %192 : i64
    cf.cond_br %191, ^bb31, ^bb32
    ^bb31:
      %193 = llvm.load %186 : !llvm.ptr -> i64
      %194 = arith.constant 1 : i32
      %196 = arith.extsi %194 : i32 to i64
      %195 = arith.addi %193, %196 : i64
      llvm.store %195, %186 : i64, !llvm.ptr
      %197 = llvm.load %188 : !llvm.ptr -> i64
      %198 = arith.constant 2 : i32
      %200 = arith.extsi %198 : i32 to i64
      %199 = arith.divsi %197, %200 : i64
      llvm.store %199, %188 : i64, !llvm.ptr
      cf.br ^bb30
    ^bb32:
    %201 = llvm.load %186 : !llvm.ptr -> i64
    %202 = arith.constant 1 : i32
    %204 = arith.extsi %202 : i32 to i64
    %203 = arith.cmpi slt, %201, %204 : i64
    cf.cond_br %203, ^bb33, ^bb34
    ^bb33:
      %205 = arith.constant 1 : i32
      %206 = arith.extsi %205 : i32 to i64
      llvm.store %206, %186 : i64, !llvm.ptr
      cf.br ^bb35
    ^bb34:
      cf.br ^bb35
    ^bb35:
    %208 = arith.constant 2 : i32
    %209 = arith.constant 8 : i32
    %210 = arith.extsi %208 : i32 to i64
    %211 = arith.extsi %209 : i32 to i64
    %207 = func.call @calloc(%210, %211) : (i64, i64) -> !llvm.ptr
    %212 = llvm.mlir.constant(1 : i64) : i64
    %213 = llvm.alloca %212 x !llvm.ptr : (i64) -> !llvm.ptr
    llvm.store %207, %213 : !llvm.ptr, !llvm.ptr
    %214 = arith.constant 1 : i32
    %215 = arith.extsi %214 : i32 to i64
    %216 = llvm.mlir.constant(1 : i64) : i64
    %217 = llvm.alloca %216 x i64 : (i64) -> !llvm.ptr
    llvm.store %215, %217 : i64, !llvm.ptr
    %218 = arith.constant 1 : i32
    %219 = llvm.load %213 : !llvm.ptr -> !llvm.ptr
    %220 = arith.constant 0 : i32
    %221 = arith.extsi %218 : i32 to i64
    %222 = arith.extsi %220 : i32 to i64
    %223 = llvm.getelementptr %219[%222] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    llvm.store %221, %223 : i64, !llvm.ptr
    %224 = arith.constant 0 : i32
    %225 = arith.extsi %224 : i32 to i64
    %226 = llvm.mlir.constant(1 : i64) : i64
    %227 = llvm.alloca %226 x i64 : (i64) -> !llvm.ptr
    llvm.store %225, %227 : i64, !llvm.ptr
    cf.br ^bb36
    ^bb36:
    %228 = llvm.load %227 : !llvm.ptr -> i64
    %229 = llvm.load %186 : !llvm.ptr -> i64
    %230 = arith.cmpi slt, %228, %229 : i64
    cf.cond_br %230, ^bb37, ^bb38
    ^bb37:
      %231 = llvm.load %227 : !llvm.ptr -> i64
      %232 = arith.shrsi %arg0, %231 : i64
      %233 = arith.constant 1 : i32
      %235 = arith.extsi %233 : i32 to i64
      %234 = arith.andi %232, %235 : i64
      %236 = llvm.load %217 : !llvm.ptr -> i64
      %237 = arith.constant 2 : i32
      %239 = arith.extsi %237 : i32 to i64
      %238 = arith.divsi %arg2, %239 : i64
      %240 = arith.addi %236, %238 : i64
      %241 = arith.constant 2 : i32
      %243 = arith.extsi %241 : i32 to i64
      %242 = arith.addi %240, %243 : i64
      %245 = arith.constant 1 : i32
      %247 = arith.extsi %245 : i32 to i64
      %246 = arith.addi %242, %247 : i64
      %248 = arith.constant 8 : i32
      %249 = arith.extsi %248 : i32 to i64
      %244 = func.call @calloc(%246, %249) : (i64, i64) -> !llvm.ptr
      %250 = arith.constant 0 : i32
      %251 = arith.extsi %250 : i32 to i64
      %252 = llvm.mlir.constant(1 : i64) : i64
      %253 = llvm.alloca %252 x i64 : (i64) -> !llvm.ptr
      llvm.store %251, %253 : i64, !llvm.ptr
      cf.br ^bb39
      ^bb39:
      %254 = llvm.load %253 : !llvm.ptr -> i64
      %255 = llvm.load %217 : !llvm.ptr -> i64
      %256 = arith.cmpi slt, %254, %255 : i64
      cf.cond_br %256, ^bb40, ^bb41
      ^bb40:
        %258 = llvm.load %213 : !llvm.ptr -> !llvm.ptr
        %259 = llvm.load %253 : !llvm.ptr -> i64
        %260 = llvm.getelementptr %258[%259] : (!llvm.ptr, i64) -> !llvm.ptr, i64
        %257 = llvm.load %260 : !llvm.ptr -> i64
        %261 = arith.constant 0 : i32
        %263 = arith.extsi %261 : i32 to i64
        %262 = arith.cmpi ne, %257, %263 : i64
        cf.cond_br %262, ^bb42, ^bb43
        ^bb42:
          %264 = arith.constant 0 : i32
          %265 = arith.extsi %264 : i32 to i64
          %266 = llvm.mlir.constant(1 : i64) : i64
          %267 = llvm.alloca %266 x i64 : (i64) -> !llvm.ptr
          llvm.store %265, %267 : i64, !llvm.ptr
          cf.br ^bb45
          ^bb45:
          %268 = llvm.load %267 : !llvm.ptr -> i64
          %269 = llvm.load %154 : !llvm.ptr -> i64
          %270 = arith.cmpi slt, %268, %269 : i64
          cf.cond_br %270, ^bb46, ^bb47
          ^bb46:
            %271 = llvm.load %253 : !llvm.ptr -> i64
            %273 = llvm.load %267 : !llvm.ptr -> i64
            %274 = llvm.getelementptr %139[%273] : (!llvm.ptr, i64) -> !llvm.ptr, i64
            %272 = llvm.load %274 : !llvm.ptr -> i64
            %275 = arith.addi %271, %272 : i64
            %276 = arith.constant 1 : i32
            %278 = arith.extsi %276 : i32 to i64
            %277 = arith.andi %275, %278 : i64
            %279 = arith.cmpi eq, %277, %234 : i64
            cf.cond_br %279, ^bb48, ^bb49
            ^bb48:
              %280 = arith.subi %275, %234 : i64
              %281 = arith.constant 2 : i32
              %283 = arith.extsi %281 : i32 to i64
              %282 = arith.divsi %280, %283 : i64
              %285 = llvm.getelementptr %244[%282] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              %284 = llvm.load %285 : !llvm.ptr -> i64
              %287 = llvm.load %267 : !llvm.ptr -> i64
              %288 = llvm.getelementptr %145[%287] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              %286 = llvm.load %288 : !llvm.ptr -> i64
              %289 = arith.muli %257, %286 : i64
              %290 = arith.addi %284, %289 : i64
              %291 = arith.remsi %290, %arg3 : i64
              %292 = llvm.getelementptr %244[%282] : (!llvm.ptr, i64) -> !llvm.ptr, i64
              llvm.store %291, %292 : i64, !llvm.ptr
              cf.br ^bb50
            ^bb49:
              cf.br ^bb50
            ^bb50:
            %293 = llvm.load %267 : !llvm.ptr -> i64
            %294 = arith.constant 1 : i32
            %296 = arith.extsi %294 : i32 to i64
            %295 = arith.addi %293, %296 : i64
            llvm.store %295, %267 : i64, !llvm.ptr
            cf.br ^bb45
          ^bb47:
          cf.br ^bb44
        ^bb43:
          cf.br ^bb44
        ^bb44:
        %297 = llvm.load %253 : !llvm.ptr -> i64
        %298 = arith.constant 1 : i32
        %300 = arith.extsi %298 : i32 to i64
        %299 = arith.addi %297, %300 : i64
        llvm.store %299, %253 : i64, !llvm.ptr
        cf.br ^bb39
      ^bb41:
      %302 = llvm.load %213 : !llvm.ptr -> !llvm.ptr
      func.call @free(%302) : (!llvm.ptr) -> ()
      llvm.store %244, %213 : !llvm.ptr, !llvm.ptr
      %303 = arith.constant 1 : i32
      %305 = arith.extsi %303 : i32 to i64
      %304 = arith.addi %242, %305 : i64
      llvm.store %304, %217 : i64, !llvm.ptr
      %306 = llvm.load %227 : !llvm.ptr -> i64
      %307 = arith.constant 1 : i32
      %309 = arith.extsi %307 : i32 to i64
      %308 = arith.addi %306, %309 : i64
      llvm.store %308, %227 : i64, !llvm.ptr
      cf.br ^bb36
    ^bb38:
    %311 = llvm.load %213 : !llvm.ptr -> !llvm.ptr
    %312 = arith.constant 0 : i32
    %313 = arith.extsi %312 : i32 to i64
    %314 = llvm.getelementptr %311[%313] : (!llvm.ptr, i64) -> !llvm.ptr, i64
    %310 = llvm.load %314 : !llvm.ptr -> i64
    %316 = llvm.load %213 : !llvm.ptr -> !llvm.ptr
    func.call @free(%316) : (!llvm.ptr) -> ()
    func.call @free(%139) : (!llvm.ptr) -> ()
    func.call @free(%145) : (!llvm.ptr) -> ()
    func.return %310 : i64
  }
  func.func @losing_count(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %319 = arith.constant 1 : i32
    %321 = arith.extsi %319 : i32 to i64
    %320 = arith.addi %arg1, %321 : i64
    %322 = arith.subi %arg0, %320 : i64
    %323 = arith.constant 1 : i32
    %325 = arith.extsi %323 : i32 to i64
    %324 = arith.addi %320, %325 : i64
    %326 = arith.constant 2 : i32
    %328 = arith.extsi %326 : i32 to i64
    %327 = arith.divsi %324, %328 : i64
    %329 = arith.subi %320, %327 : i64
    %330 = arith.constant 1 : i32
    %332 = arith.extsi %330 : i32 to i64
    %331 = arith.addi %329, %332 : i64
    %333 = arith.addi %327, %331 : i64
    %335 = arith.constant 1 : i32
    %337 = arith.extsi %335 : i32 to i64
    %336 = arith.addi %333, %337 : i64
    %338 = arith.constant 8 : i32
    %339 = arith.extsi %338 : i32 to i64
    %334 = func.call @calloc(%336, %339) : (i64, i64) -> !llvm.ptr
    %341 = arith.constant 1 : i32
    %343 = arith.extsi %341 : i32 to i64
    %342 = arith.addi %333, %343 : i64
    %344 = arith.constant 8 : i32
    %345 = arith.extsi %344 : i32 to i64
    %340 = func.call @calloc(%342, %345) : (i64, i64) -> !llvm.ptr
    %346 = func.call @build_ways(%327, %331, %arg2, %334) : (i64, i64, i64, !llvm.ptr) -> i32
    %348 = arith.constant 1 : i32
    %350 = arith.extsi %348 : i32 to i64
    %349 = arith.subi %327, %350 : i64
    %347 = func.call @build_ways(%349, %331, %arg2, %340) : (i64, i64, i64, !llvm.ptr) -> i32
    %351 = func.call @count_with_ways(%322, %334, %333, %arg2) : (i64, !llvm.ptr, i64, i64) -> i64
    %353 = arith.constant 1 : i32
    %355 = arith.extsi %353 : i32 to i64
    %354 = arith.addi %322, %355 : i64
    %352 = func.call @count_with_ways(%354, %334, %333, %arg2) : (i64, !llvm.ptr, i64, i64) -> i64
    %357 = arith.constant 1 : i32
    %359 = arith.extsi %357 : i32 to i64
    %358 = arith.addi %322, %359 : i64
    %356 = func.call @count_with_ways(%358, %340, %333, %arg2) : (i64, !llvm.ptr, i64, i64) -> i64
    %360 = arith.subi %352, %356 : i64
    %361 = arith.addi %360, %arg2 : i64
    %362 = arith.remsi %361, %arg2 : i64
    func.call @free(%334) : (!llvm.ptr) -> ()
    func.call @free(%340) : (!llvm.ptr) -> ()
    %365 = arith.constant 2 : i32
    %367 = arith.extsi %365 : i32 to i64
    %366 = arith.subi %320, %367 : i64
    %368 = arith.remsi %366, %arg2 : i64
    %369 = arith.muli %368, %362 : i64
    %370 = arith.addi %351, %369 : i64
    %371 = arith.remsi %370, %arg2 : i64
    func.return %371 : i64
  }
  func.func @solve_prime(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %372 = arith.constant 1 : i32
    %374 = arith.extsi %372 : i32 to i64
    %373 = arith.addi %arg1, %374 : i64
    %375 = arith.remsi %373, %arg2 : i64
    %376 = func.call @binom_mod(%arg0, %373, %arg2) : (i64, i64, i64) -> i64
    %377 = arith.muli %375, %376 : i64
    %378 = arith.remsi %377, %arg2 : i64
    %379 = func.call @losing_count(%arg0, %arg1, %arg2) : (i64, i64, i64) -> i64
    %380 = arith.subi %378, %379 : i64
    %381 = arith.addi %380, %arg2 : i64
    %382 = arith.remsi %381, %arg2 : i64
    func.return %382 : i64
  }
  func.func @main() -> i32 {
    %383 = arith.constant 1000000 : i32
    %384 = arith.extsi %383 : i32 to i64
    %385 = arith.constant 100 : i32
    %386 = arith.extsi %385 : i32 to i64
    %387 = arith.constant 1000003 : i32
    %388 = arith.extsi %387 : i32 to i64
    %389 = arith.constant 1000033 : i32
    %390 = arith.extsi %389 : i32 to i64
    %391 = arith.constant 995741032803 : i32
    %392 = arith.extsi %391 : i32 to i64
    %393 = func.call @solve_prime(%384, %386, %388) : (i64, i64, i64) -> i64
    %394 = func.call @solve_prime(%384, %386, %390) : (i64, i64, i64) -> i64
    %395 = arith.subi %394, %393 : i64
    %396 = arith.remsi %395, %390 : i64
    %397 = arith.addi %396, %390 : i64
    %398 = arith.remsi %397, %390 : i64
    %400 = arith.remsi %388, %390 : i64
    %401 = arith.constant 2 : i32
    %403 = arith.extsi %401 : i32 to i64
    %402 = arith.subi %390, %403 : i64
    %399 = func.call @modpow(%400, %402, %390) : (i64, i64, i64) -> i64
    %404 = arith.muli %398, %399 : i64
    %405 = arith.remsi %404, %390 : i64
    %406 = arith.muli %388, %405 : i64
    %407 = arith.addi %393, %406 : i64
    %408 = arith.remsi %407, %392 : i64
    %409 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %410 = llvm.call @printf(%409, %408) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %411 = arith.constant 0 : i32
    func.return %411 : i32
  }
}