// Exact periodic interval-count histograms without storing the whole tile. // Project-local wheel construction follows return233; the queue sweep is new. #include #include #include #include #include #include #include using U=uint64_t; struct Counter { U W,D,H,M,h,base,count,prev=0; std::deque pending; std::vector prefix,hist; Counter(U w,U d,U length):W(w),D(d),H(length),M(w/6),h((length/6)%M), base((length/6/M)*d),count(base),hist(length/6+2,0){} void advance(U e){ if(eM)throw std::runtime_error("event order"); while(!pending.empty()&&pending.front()<=e){ U t=pending.front();pending.pop_front(); if(t primes={2,3,5,7,11,13,17,19,23,29,31}; U W=1,D=1;for(int p:primes)if(p<=z){W*=p;if(p>2)D*=p-2;} Counter square(W,D,6*((U(z)*z+5)/6)),cube(W,D,6*((U(z)*z*z+5)/6)); std::vector residues;for(int r=0;r<210;++r)if(std::gcd(r,210)==1&&std::gcd(r+2,210)==1)residues.push_back(r); if(residues.size()!=15)throw std::runtime_error("wheel"); struct Mark{int q,i,start;};std::vector marks; for(int q:primes)if(q>7&&q<=z){int inv=1;while(210*inv%q!=1)++inv; for(int i=0;i<15;++i)for(int shift:{0,2})marks.push_back({q,i,((q-(residues[i]+shift)%q)*inv)%q});} const U periods=W/210;U seen=0,last=0,next_progress=periods/10; for(U lo=0;lo live(15*(hi-lo),1); for(auto m:marks){U k=lo+(m.start+m.q-lo%m.q)%m.q;for(;k=31&&hi>=next_progress){std::cerr<<"z="<1?std::stoi(argv[1]):31;U block=argc>2?std::stoull(argv[2]):65536; if(block<1||block>1000000)throw std::runtime_error("block range"); if(z==0){for(int p:{7,11,13,17,19,23,29})run(p,block);} else if(z==7||z==11||z==13||z==17||z==19||z==23||z==29||z==31)run(z,block); else throw std::runtime_error("unsupported prime"); }catch(const std::exception&e){std::cerr<