# Project Euler 734
# A Bit of Prime: T(1e6,999983) mod 1e9+7.
extern {
function calloc(n: i64, size: i64) -> ptr<void>
function free(p: ptr<void>) -> void
function memset(p: ptr<void>, v: i32, n: i64) -> ptr<void>
}
const MOD: i64 = 1000000007
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 % 2 == 1 {
r = r * bb % MOD
}
bb = bb * bb % MOD
ee = ee / 2
}
return r
}
function popcount(n: i32) -> i32 {
let mut x: i32 = n
let mut c: i32 = 0
while x > 0 {
c = c + 1
x = x & (x - 1)
}
return c
}
function main() -> i32 {
let n: i32 = 1000000
let k: i64 = 999983
let mut B: i32 = 0
let mut t: i32 = n
while t > 0 {
B = B + 1
t = t >> 1
}
let size: i32 = 1 << B
let is_prime: ptr<i8> = calloc((n + 1) as i64, 1)
memset(is_prime, 1, (n + 1) as i64)
is_prime[0] = 0
is_prime[1] = 0
let mut p: i32 = 2
while (p as i64) * (p as i64) <= (n as i64) {
if is_prime[p] != 0 {
let mut j: i32 = p * p
while j <= n {
is_prime[j] = 0
j = j + p
}
}
p = p + 1
}
let a: ptr<i64> = calloc(size as i64, 8)
let s: ptr<i64> = calloc(size as i64, 8)
let mut pp: i32 = 2
while pp <= n {
if is_prime[pp] != 0 {
a[pp] = 1
if popcount(pp) % 2 == 1 {
s[pp] = 0 - 1
} else {
s[pp] = 1
}
}
pp = pp + 1
}
let mut i: i32 = 0
while i < B {
let step: i32 = 1 << i
let jump: i32 = step << 1
let mut base: i32 = 0
while base < size {
let mut m: i32 = base + step
while m < base + jump {
a[m] = a[m] + a[m - step]
m = m + 1
}
base = base + jump
}
i = i + 1
}
i = 0
while i < B {
let step: i32 = 1 << i
let jump: i32 = step << 1
let mut base: i32 = 0
while base < size {
let mut m: i32 = base
while m < base + step {
s[m] = s[m] + s[m + step]
m = m + 1
}
base = base + jump
}
i = i + 1
}
let mut res: i64 = 0
let mut m: i32 = 0
while m <= n {
if a[m] == 0 {
m = m + 1
continue
}
let val: i64 = modpow(a[m], k)
let mut coeff: i64 = s[m]
if popcount(m) % 2 == 1 {
coeff = 0 - coeff
}
res = (res + val * coeff) % MOD
m = m + 1
}
if res < 0 {
res = res + MOD
}
printf("%lld\n", res)
free(is_prime)
free(a)
free(s)
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 popcount_i32(int32_t n);
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 (FLOW_CHECKED_MOD((ee), (2)) == 1) {
r = FLOW_CHECKED_MOD(((r * bb)), (MOD));
}
bb = FLOW_CHECKED_MOD(((bb * bb)), (MOD));
ee = FLOW_CHECKED_DIV((ee), (2));
}
return r;
}
int32_t popcount_i32(int32_t n) {
int32_t x = n;
int32_t c = 0;
while (x > 0) {
c = (c + 1);
x = (x & (x - 1));
}
return c;
}
int32_t main(void) {
int32_t n = 1000000;
int64_t k = 999983;
int32_t B = 0;
int32_t t = n;
while (t > 0) {
B = (B + 1);
t = FLOW_CHECKED_SHR((t), (1));
}
int32_t size = FLOW_CHECKED_SHL((1), (B));
int8_t* is_prime = (int8_t*)(calloc(((int64_t)((n + 1))), 1));
memset(is_prime, 1, ((int64_t)((n + 1))));
is_prime[0] = 0;
is_prime[1] = 0;
int32_t p = 2;
while ((((int64_t)(p)) * ((int64_t)(p))) <= ((int64_t)(n))) {
if (is_prime[p] != 0) {
int32_t j = (p * p);
while (j <= n) {
is_prime[j] = 0;
j = (j + p);
}
}
p = (p + 1);
}
int64_t* a = (int64_t*)(calloc(((int64_t)(size)), 8));
int64_t* s = (int64_t*)(calloc(((int64_t)(size)), 8));
int32_t pp = 2;
while (pp <= n) {
if (is_prime[pp] != 0) {
a[pp] = 1;
if (FLOW_CHECKED_MOD((popcount_i32(pp)), (2)) == 1) {
s[pp] = (0 - 1);
} else {
s[pp] = 1;
}
}
pp = (pp + 1);
}
int32_t i = 0;
while (i < B) {
int32_t step = FLOW_CHECKED_SHL((1), (i));
int32_t jump = FLOW_CHECKED_SHL((step), (1));
int32_t base = 0;
while (base < size) {
int32_t m = (base + step);
while (m < (base + jump)) {
a[m] = (a[m] + a[(m - step)]);
m = (m + 1);
}
base = (base + jump);
}
i = (i + 1);
}
i = 0;
while (i < B) {
int32_t step = FLOW_CHECKED_SHL((1), (i));
int32_t jump = FLOW_CHECKED_SHL((step), (1));
int32_t base = 0;
while (base < size) {
int32_t m = base;
while (m < (base + step)) {
s[m] = (s[m] + s[(m + step)]);
m = (m + 1);
}
base = (base + jump);
}
i = (i + 1);
}
int64_t res = 0;
int32_t m = 0;
while (m <= n) {
if (a[m] == 0) {
m = (m + 1);
continue;
}
int64_t val = modpow_i64_i64(a[m], k);
int64_t coeff = s[m];
if (FLOW_CHECKED_MOD((popcount_i32(m)), (2)) == 1) {
coeff = (0 - coeff);
}
res = FLOW_CHECKED_MOD(((res + (val * coeff))), (MOD));
m = (m + 1);
}
if (res < 0) {
res = (res + MOD);
}
printf("%lld\n", res);
free(is_prime);
free(a);
free(s);
return 0;
}