Problem 942

Mersenne's Square Root — Gauss sum mod 10^9+7 for R(q), q=1122659.

Answer557539756
Output557539756
StatusPASS
Native helperno
Runtime350 ms
Peak memory73600 KB
Time complexityO(n) (estimated)
Space complexityO(n) (estimated)

Performance comparison

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

Flow source

# Project Euler 942
# Mersenne's Square Root — Gauss sum mod 10^9+7 for R(q), q=1122659.

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

const MOD: i64 = 1000000007

function gauss_sum_mod(q: i64) -> i64 {
    let qr: ptr<i8> = calloc(q, 1)
    let half: i64 = (q - 1) / 2
    let mut sq: i64 = 1
    let mut delta: i64 = 3
    let mut i: i64 = 0
    while i < half {
        qr[sq] = 1
        sq = sq + delta
        if sq >= q { sq = sq - q }
        delta = delta + 2
        i = i + 1
    }
    let mut s: i64 = 0
    let mut pow2: i64 = 2
    let mut a: i64 = 1
    while a < q {
        if qr[a] != 0 {
            s = s + pow2
            if s >= MOD { s = s - MOD }
        } else {
            s = s - pow2
            if s < 0 { s = s + MOD }
        }
        pow2 = pow2 * 2
        if pow2 >= MOD { pow2 = pow2 - MOD }
        a = a + 1
    }
    free(qr)
    return s
}

function modpow2(q: i64) -> i64 {
    let mut r: i64 = 1
    let mut b: i64 = 2
    let mut e: i64 = q
    while e > 0 {
        if (e & 1) == 1 { r = (r * b) % MOD }
        b = (b * b) % MOD
        e = e / 2
    }
    return r
}

function r_mod(q: i64) -> i64 {
    let g: i64 = gauss_sum_mod(q)
    let p_mod: i64 = (modpow2(q) - 1 + MOD) % MOD
    let r: i64 = q & 7
    if r == 1 || r == 3 {
        return (p_mod - g + MOD) % MOD
    }
    return g
}

function main() -> i32 {
    # q from problem statement (Mersenne prime exponent)
    printf("%lld\n", r_mod(74207281))
    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 gauss_sum_mod_i64(int64_t q);
int64_t modpow2_i64(int64_t q);
int64_t r_mod_i64(int64_t q);
int32_t main(void);

static const int64_t MOD = 1000000007;



int64_t gauss_sum_mod_i64(int64_t q) {
    int8_t* qr = (int8_t*)(calloc(q, 1));
    int64_t half = FLOW_CHECKED_DIV(((q - 1)), (2));
    int64_t sq = 1;
    int64_t delta = 3;
    int64_t i = 0;
    while (i < half) {
        qr[sq] = 1;
        sq = (sq + delta);
        if (sq >= q) {
            sq = (sq - q);
        }
        delta = (delta + 2);
        i = (i + 1);
    }
    int64_t s = 0;
    int64_t pow2 = 2;
    int64_t a = 1;
    while (a < q) {
        if (qr[a] != 0) {
            s = (s + pow2);
            if (s >= MOD) {
                s = (s - MOD);
            }
        } else {
            s = (s - pow2);
            if (s < 0) {
                s = (s + MOD);
            }
        }
        pow2 = (pow2 * 2);
        if (pow2 >= MOD) {
            pow2 = (pow2 - MOD);
        }
        a = (a + 1);
    }
    free(qr);
    return s;
}

int64_t modpow2_i64(int64_t q) {
    int64_t r = 1;
    int64_t b = 2;
    int64_t e = q;
    while (e > 0) {
        if ((e & 1) == 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 r_mod_i64(int64_t q) {
    int64_t g = gauss_sum_mod_i64(q);
    int64_t p_mod = FLOW_CHECKED_MOD((((modpow2_i64(q) - 1) + MOD)), (MOD));
    int64_t r = (q & 7);
    if ((r == 1 || r == 3)) {
        return FLOW_CHECKED_MOD((((p_mod - g) + MOD)), (MOD));
    }
    return g;
}

int32_t main(void) {
    printf("%lld\n", r_mod_i64(74207281));
    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) -> ()
  // Constant: MOD
  llvm.mlir.global internal constant @MOD(1000000007 : i64) : i64
  func.func @gauss_sum_mod(%arg0: i64) -> i64 {
    %1 = arith.constant 1 : i32
    %2 = arith.extsi %1 : i32 to i64
    %0 = func.call @calloc(%arg0, %2) : (i64, i64) -> !llvm.ptr
    %3 = arith.constant 1 : i32
    %5 = arith.extsi %3 : i32 to i64
    %4 = arith.subi %arg0, %5 : i64
    %6 = arith.constant 2 : i32
    %8 = arith.extsi %6 : i32 to i64
    %7 = arith.divsi %4, %8 : i64
    %9 = arith.constant 1 : i32
    %10 = arith.extsi %9 : i32 to i64
    %11 = llvm.mlir.constant(1 : i64) : i64
    %12 = llvm.alloca %11 x i64 : (i64) -> !llvm.ptr
    llvm.store %10, %12 : i64, !llvm.ptr
    %13 = arith.constant 3 : i32
    %14 = arith.extsi %13 : i32 to i64
    %15 = llvm.mlir.constant(1 : i64) : i64
    %16 = llvm.alloca %15 x i64 : (i64) -> !llvm.ptr
    llvm.store %14, %16 : i64, !llvm.ptr
    %17 = arith.constant 0 : i32
    %18 = arith.extsi %17 : i32 to i64
    %19 = llvm.mlir.constant(1 : i64) : i64
    %20 = llvm.alloca %19 x i64 : (i64) -> !llvm.ptr
    llvm.store %18, %20 : i64, !llvm.ptr
    cf.br ^bb0
    ^bb0:
    %21 = llvm.load %20 : !llvm.ptr -> i64
    %22 = arith.cmpi slt, %21, %7 : i64
    cf.cond_br %22, ^bb1, ^bb2
    ^bb1:
      %23 = arith.constant 1 : i32
      %24 = llvm.load %12 : !llvm.ptr -> i64
      %25 = arith.trunci %23 : i32 to i8
      %26 = llvm.getelementptr %0[%24] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      llvm.store %25, %26 : i8, !llvm.ptr
      %27 = llvm.load %12 : !llvm.ptr -> i64
      %28 = llvm.load %16 : !llvm.ptr -> i64
      %29 = arith.addi %27, %28 : i64
      llvm.store %29, %12 : i64, !llvm.ptr
      %30 = llvm.load %12 : !llvm.ptr -> i64
      %31 = arith.cmpi sge, %30, %arg0 : i64
      cf.cond_br %31, ^bb3, ^bb4
      ^bb3:
        %32 = llvm.load %12 : !llvm.ptr -> i64
        %33 = arith.subi %32, %arg0 : i64
        llvm.store %33, %12 : i64, !llvm.ptr
        cf.br ^bb5
      ^bb4:
        cf.br ^bb5
      ^bb5:
      %34 = llvm.load %16 : !llvm.ptr -> i64
      %35 = arith.constant 2 : i32
      %37 = arith.extsi %35 : i32 to i64
      %36 = arith.addi %34, %37 : i64
      llvm.store %36, %16 : i64, !llvm.ptr
      %38 = llvm.load %20 : !llvm.ptr -> i64
      %39 = arith.constant 1 : i32
      %41 = arith.extsi %39 : i32 to i64
      %40 = arith.addi %38, %41 : i64
      llvm.store %40, %20 : i64, !llvm.ptr
      cf.br ^bb0
    ^bb2:
    %42 = arith.constant 0 : i32
    %43 = arith.extsi %42 : i32 to i64
    %44 = llvm.mlir.constant(1 : i64) : i64
    %45 = llvm.alloca %44 x i64 : (i64) -> !llvm.ptr
    llvm.store %43, %45 : i64, !llvm.ptr
    %46 = arith.constant 2 : i32
    %47 = arith.extsi %46 : i32 to i64
    %48 = llvm.mlir.constant(1 : i64) : i64
    %49 = llvm.alloca %48 x i64 : (i64) -> !llvm.ptr
    llvm.store %47, %49 : i64, !llvm.ptr
    %50 = arith.constant 1 : i32
    %51 = arith.extsi %50 : i32 to i64
    %52 = llvm.mlir.constant(1 : i64) : i64
    %53 = llvm.alloca %52 x i64 : (i64) -> !llvm.ptr
    llvm.store %51, %53 : i64, !llvm.ptr
    cf.br ^bb6
    ^bb6:
    %54 = llvm.load %53 : !llvm.ptr -> i64
    %55 = arith.cmpi slt, %54, %arg0 : i64
    cf.cond_br %55, ^bb7, ^bb8
    ^bb7:
      %57 = llvm.load %53 : !llvm.ptr -> i64
      %58 = llvm.getelementptr %0[%57] : (!llvm.ptr, i64) -> !llvm.ptr, i8
      %56 = llvm.load %58 : !llvm.ptr -> i8
      %59 = arith.constant 0 : i32
      %61 = arith.extsi %56 : i8 to i32
      %60 = arith.cmpi ne, %61, %59 : i32
      cf.cond_br %60, ^bb9, ^bb10
      ^bb9:
        %62 = llvm.load %45 : !llvm.ptr -> i64
        %63 = llvm.load %49 : !llvm.ptr -> i64
        %64 = arith.addi %62, %63 : i64
        llvm.store %64, %45 : i64, !llvm.ptr
        %65 = llvm.load %45 : !llvm.ptr -> i64
        %66 = llvm.mlir.addressof @MOD : !llvm.ptr
        %67 = llvm.load %66 : !llvm.ptr -> i64
        %68 = arith.cmpi sge, %65, %67 : i64
        cf.cond_br %68, ^bb12, ^bb13
        ^bb12:
          %69 = llvm.load %45 : !llvm.ptr -> i64
          %70 = llvm.mlir.addressof @MOD : !llvm.ptr
          %71 = llvm.load %70 : !llvm.ptr -> i64
          %72 = arith.subi %69, %71 : i64
          llvm.store %72, %45 : i64, !llvm.ptr
          cf.br ^bb14
        ^bb13:
          cf.br ^bb14
        ^bb14:
        cf.br ^bb11
      ^bb10:
        %73 = llvm.load %45 : !llvm.ptr -> i64
        %74 = llvm.load %49 : !llvm.ptr -> i64
        %75 = arith.subi %73, %74 : i64
        llvm.store %75, %45 : i64, !llvm.ptr
        %76 = llvm.load %45 : !llvm.ptr -> i64
        %77 = arith.constant 0 : i32
        %79 = arith.extsi %77 : i32 to i64
        %78 = arith.cmpi slt, %76, %79 : i64
        cf.cond_br %78, ^bb15, ^bb16
        ^bb15:
          %80 = llvm.load %45 : !llvm.ptr -> i64
          %81 = llvm.mlir.addressof @MOD : !llvm.ptr
          %82 = llvm.load %81 : !llvm.ptr -> i64
          %83 = arith.addi %80, %82 : i64
          llvm.store %83, %45 : i64, !llvm.ptr
          cf.br ^bb17
        ^bb16:
          cf.br ^bb17
        ^bb17:
        cf.br ^bb11
      ^bb11:
      %84 = llvm.load %49 : !llvm.ptr -> i64
      %85 = arith.constant 2 : i32
      %87 = arith.extsi %85 : i32 to i64
      %86 = arith.muli %84, %87 : i64
      llvm.store %86, %49 : i64, !llvm.ptr
      %88 = llvm.load %49 : !llvm.ptr -> i64
      %89 = llvm.mlir.addressof @MOD : !llvm.ptr
      %90 = llvm.load %89 : !llvm.ptr -> i64
      %91 = arith.cmpi sge, %88, %90 : i64
      cf.cond_br %91, ^bb18, ^bb19
      ^bb18:
        %92 = llvm.load %49 : !llvm.ptr -> i64
        %93 = llvm.mlir.addressof @MOD : !llvm.ptr
        %94 = llvm.load %93 : !llvm.ptr -> i64
        %95 = arith.subi %92, %94 : i64
        llvm.store %95, %49 : i64, !llvm.ptr
        cf.br ^bb20
      ^bb19:
        cf.br ^bb20
      ^bb20:
      %96 = llvm.load %53 : !llvm.ptr -> i64
      %97 = arith.constant 1 : i32
      %99 = arith.extsi %97 : i32 to i64
      %98 = arith.addi %96, %99 : i64
      llvm.store %98, %53 : i64, !llvm.ptr
      cf.br ^bb6
    ^bb8:
    func.call @free(%0) : (!llvm.ptr) -> ()
    %101 = llvm.load %45 : !llvm.ptr -> i64
    func.return %101 : i64
  }
  func.func @modpow2(%arg0: i64) -> i64 {
    %102 = arith.constant 1 : i32
    %103 = arith.extsi %102 : i32 to i64
    %104 = llvm.mlir.constant(1 : i64) : i64
    %105 = llvm.alloca %104 x i64 : (i64) -> !llvm.ptr
    llvm.store %103, %105 : i64, !llvm.ptr
    %106 = arith.constant 2 : i32
    %107 = arith.extsi %106 : i32 to i64
    %108 = llvm.mlir.constant(1 : i64) : i64
    %109 = llvm.alloca %108 x i64 : (i64) -> !llvm.ptr
    llvm.store %107, %109 : i64, !llvm.ptr
    %110 = llvm.mlir.constant(1 : i64) : i64
    %111 = llvm.alloca %110 x i64 : (i64) -> !llvm.ptr
    llvm.store %arg0, %111 : i64, !llvm.ptr
    cf.br ^bb21
    ^bb21:
    %112 = llvm.load %111 : !llvm.ptr -> i64
    %113 = arith.constant 0 : i32
    %115 = arith.extsi %113 : i32 to i64
    %114 = arith.cmpi sgt, %112, %115 : i64
    cf.cond_br %114, ^bb22, ^bb23
    ^bb22:
      %116 = llvm.load %111 : !llvm.ptr -> i64
      %117 = arith.constant 1 : i32
      %119 = arith.extsi %117 : i32 to i64
      %118 = arith.andi %116, %119 : i64
      %120 = arith.constant 1 : i32
      %122 = arith.extsi %120 : i32 to i64
      %121 = arith.cmpi eq, %118, %122 : i64
      cf.cond_br %121, ^bb24, ^bb25
      ^bb24:
        %123 = llvm.load %105 : !llvm.ptr -> i64
        %124 = llvm.load %109 : !llvm.ptr -> i64
        %125 = arith.muli %123, %124 : i64
        %126 = llvm.mlir.addressof @MOD : !llvm.ptr
        %127 = llvm.load %126 : !llvm.ptr -> i64
        %128 = arith.remsi %125, %127 : i64
        llvm.store %128, %105 : i64, !llvm.ptr
        cf.br ^bb26
      ^bb25:
        cf.br ^bb26
      ^bb26:
      %129 = llvm.load %109 : !llvm.ptr -> i64
      %130 = llvm.load %109 : !llvm.ptr -> i64
      %131 = arith.muli %129, %130 : i64
      %132 = llvm.mlir.addressof @MOD : !llvm.ptr
      %133 = llvm.load %132 : !llvm.ptr -> i64
      %134 = arith.remsi %131, %133 : i64
      llvm.store %134, %109 : i64, !llvm.ptr
      %135 = llvm.load %111 : !llvm.ptr -> i64
      %136 = arith.constant 2 : i32
      %138 = arith.extsi %136 : i32 to i64
      %137 = arith.divsi %135, %138 : i64
      llvm.store %137, %111 : i64, !llvm.ptr
      cf.br ^bb21
    ^bb23:
    %139 = llvm.load %105 : !llvm.ptr -> i64
    func.return %139 : i64
  }
  func.func @r_mod(%arg0: i64) -> i64 {
    %140 = func.call @gauss_sum_mod(%arg0) : (i64) -> i64
    %141 = func.call @modpow2(%arg0) : (i64) -> i64
    %142 = arith.constant 1 : i32
    %144 = arith.extsi %142 : i32 to i64
    %143 = arith.subi %141, %144 : i64
    %145 = llvm.mlir.addressof @MOD : !llvm.ptr
    %146 = llvm.load %145 : !llvm.ptr -> i64
    %147 = arith.addi %143, %146 : i64
    %148 = llvm.mlir.addressof @MOD : !llvm.ptr
    %149 = llvm.load %148 : !llvm.ptr -> i64
    %150 = arith.remsi %147, %149 : i64
    %151 = arith.constant 7 : i32
    %153 = arith.extsi %151 : i32 to i64
    %152 = arith.andi %arg0, %153 : i64
    %154 = arith.constant 1 : i32
    %156 = arith.extsi %154 : i32 to i64
    %155 = arith.cmpi eq, %152, %156 : i64
    %157 = scf.if %155 -> (i1) {
      %158 = arith.constant true
      scf.yield %158 : i1
    } else {
      %159 = arith.constant 3 : i32
      %161 = arith.extsi %159 : i32 to i64
      %160 = arith.cmpi eq, %152, %161 : i64
      scf.yield %160 : i1
    }
    cf.cond_br %157, ^bb27, ^bb28
    ^bb27:
      %162 = arith.subi %150, %140 : i64
      %163 = llvm.mlir.addressof @MOD : !llvm.ptr
      %164 = llvm.load %163 : !llvm.ptr -> i64
      %165 = arith.addi %162, %164 : i64
      %166 = llvm.mlir.addressof @MOD : !llvm.ptr
      %167 = llvm.load %166 : !llvm.ptr -> i64
      %168 = arith.remsi %165, %167 : i64
      func.return %168 : i64
    ^bb28:
      cf.br ^bb29
    ^bb29:
    func.return %140 : i64
  }
  func.func @main() -> i32 {
    %169 = llvm.mlir.addressof @str_0 : !llvm.ptr
    %171 = arith.constant 74207281 : i32
    %172 = arith.extsi %171 : i32 to i64
    %170 = func.call @r_mod(%172) : (i64) -> i64
    %173 = llvm.call @printf(%169, %170) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr, i64) -> i32
    %174 = arith.constant 0 : i32
    func.return %174 : i32
  }
}