You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 lines
4.9 KiB

#include"a.h" // ngn/k, (c) 2019-2021 ngn, GNU AGPLv3 - https://codeberg.org/ngn/k/blob/master/LICENSE
#define h(T) S V i##T(V*p,Nn)_(T*a=p;AL(a)i(PD(n,a),a[i]=i))\
S V r##T(V*p,V*q)_(T*a=p,*b=q;AL(a)W(a<b,SWP(*a,*b);a++;b--))\
S V w##T(L*RE p,Nn,V*RE r)_(T*a=r;AL(p)AL(a)i(n,j(p[i],*a++=i)))
h(B)h(H)h(I)h(L)
#define c_(T,R,p) S V c##T##R(OV*RE v,V*RE w,Nn)_(O T*RE a=v;R*RE r=w;i(PD(n,p),r[i]=a[i]))
#define cN(T,R,p) S V c##T##R(OV*RE v,V*RE w,Nn)_(O T*RE a=v;R*RE r=w;i(PD(n,p),r[i]=a[i]==N##T?N##R:a[i]))
c_(C,H,r)c_(B,H,r)c_(H,C,a)c_(C,I,r)c_(B,I,r)c_(I,C,a)c_(I,B,a)c_(L,C,a)
c_(H,I,r)c_(I,H,a)c_(H,L,r)c_(L,H,a)c_(I,L,r)c_(L,I,a)cN(L,D,a)cN(D,L,a)
A1(flp,XM(AT(tm,mut(x)))Xm(Ay=xy;Er(!ytA||!yn,x)i(yn,Az=ya;En(!ztT,x))Lm=_n(yx);i(yn,Az=ya;El(zn-m,x))AT(tM,mut(x)))
Xt(enl(enl(x)))P(!xtA||!xn,enl(x))K("{(,/n#'x)(n*!#x)+/:!n:|/#'x}",x))
A1(til,Xz(Ln=gl(x);P(n<0,n-NL?add(az(n),til(az(-n))):ed0())Ez(n>1<<30)x=atn(tZ(n-1),n);CH(xt-tB,&iB,iH,iI,iL)(xC,n);x)
XZ(K("{x((*a)#&#)'1_a:|*\\|x,1}",x))Xm(x(_R(xx)))Xo(val(x))Xs(til(N(enl(x))))XS(til(N(val(x))))Et(!xtA||xn,x)x)
A1(whr,XA(K("{$[`A~@x;(,&#'*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y))
A1(gI,XiI(x)Xz(ai(gl(x)))XdD(gI(N(gL(x))))Xcz(ai(gl(x)))XCZ(Ay=aI(xn);x2(CH(xt-tC,&cCI,cBI,cHI,0,cLI)(xC,yC,yn);y))et1(x))
A1(gL,XlL(x)Xz(al(gl(x)))Xt(gt(x,gL))XD(Ay=aL(xn);x2(cDL(xC,yC,yn);y))x=N(gI(x));Ay=aL(xn);x2(cIL(xC,yC,yn);y))
A1(gD,XdD(x)Xz(ad(gl(x)))Xt(gt(x,gD))x=N(gL(x));Ay=aD(xn);x2(cLD(xC,yC,yn);y))
A1(gS,XsS(x)XC(x=str0(x);x(as(syP(xC))))Xc(as(gi(x)))XmMA(eac1(x,gS))et1(x))
A1(pI,XmMA(eac1(x,pI))Xc(pI(enl(x)))XC(x=str0(x);Qs=xC;P(!*s,x(_R(cn[tl])))Lv=pl(&s);x(*s?_R(cn[tl]):az(v)))et1(x))
TY(ct)ct={[tA]=blw,gC,gB,gH,gI,gL,gD,gS};
A2(cst,P(xtS||ytmMA,eac2(x,y,cv('$')))P(xtz&&ytcC,K("{y@(!x)+(x<0)*#y}",x,enla(y)))
Xs(Ss("c","b","h","i","l","d","","s","I")Sf(x,y,s,gC,gB,gH,gI,gL,gD,gS,gS,pI))en2(x,y))
A1(sqz,P(!xtA||!xn,x)Ay=oA;i(xn,yq(_R(xa)))x(y))
A1(blw,XA(x)Xt(a1(x))Et(xtm,x)Nn=xN;Ay=aA(n);i(yn,ya=get(x,i))x(y))
A1(fir,Xt(x)Xm(fir(val(x)))x(getr(x,0)))
A1(las,Xt(x)Xm(las(val(x)))x(getr(x,xN-1)))
L tru(Ax/*1*/)_(Lv=c3(tc,xt,ts)?gl_(x):xtF?!xtu||xv:xn;x(0);v)
I mtc_(Ax,Ay/*00*/)_(P(x==y,1)P(xtZ&&ytZ&&xn==yn,gl(rdc(cv('*'),A(eql(xR,yR)),1)))P(xtz&&ytz,gl_(x)==gl_(y))
P(xt-yt||xtP||(xtr&&xv-yv)||xn-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-n,x)Im=n/2;OA*v=xA;Ay=aI(n),z=aI(n-m);I*a=yI,*b=zI;iI(a,n);
ascA(v,a+m,n-m,b);ascA(v,a,m,a+n-m);mrg(v,a+n-m,a+n,b,b+n-m,a);x(z(y)))
XS(asc(N(eac1(x,str))))XD(asc(blw(x)))Xm(Ay=kv(&x);idx(x,N(asc(y))))Et(!(xts||xti),x)opn(x))
A1(dsc,Xz(cls(x))Xm(Ay=kv(&x);idx(x,Nx(dsc(y))))Et(!xtT,x)x=rev(asc(rev(x)));sub(ai(xN-1),x))
A1(grp,Xz(K("{x=/:x:!x}",x))Xt(et1(x))Xm(Ay=grp(kv(&x));yy=idx(x,yy);y)K("{x[*'g]!g@:<g:(&(~x~':x i),!0)_i:<x}",x))
A2(bin,Xmt(er2(x,y))YMT(K("{x''y}",x,y))Li=-1,j=xn;W(i+1<j,Lm=(i+j)/2;Az=get(x,m);cA(y,z)<0?j=m:(i=m);z(0))x(y(az(i))))