varbigInt=function(undefined){"use strict";varBASE=1e7,LOG_BASE=7,MAX_INT=9007199254740992,MAX_INT_ARR=smallToArray(MAX_INT),DEFAULT_ALPHABET="0123456789abcdefghijklmnopqrstuvwxyz";varsupportsNativeBigInt=typeofBigInt==="function";functionInteger(v,radix,alphabet,caseSensitive){if(typeofv==="undefined")returnInteger[0];if(typeofradix!=="undefined")return+radix===10&&!alphabet?parseValue(v):parseBase(v,radix,alphabet,caseSensitive);returnparseValue(v)}functionBigInteger(value,sign){this.value=value;this.sign=sign;this.isSmall=false}BigInteger.prototype=Object.create(Integer.prototype);functionSmallInteger(value){this.value=value;this.sign=value<0;this.isSmall=true}SmallInteger.prototype=Object.create(Integer.prototype);functionNativeBigInt(value){this.value=value}NativeBigInt.prototype=Object.create(Integer.prototype);functionisPrecise(n){return-MAX_INT<n&&n<MAX_INT}functionsmallToArray(n){if(n<1e7)return[n];if(n<1e14)return[n%1e7,Math.floor(n/1e7)];return[n%1e7,Math.floor(n/1e7)%1e7,Math.floor(n/1e14)]}functionarrayToSmall(arr){trim(arr);varlength=arr.length;if(length<4&&compareAbs(arr,MAX_INT_ARR)<0){switch(length){case0:return0;case1:returnarr[0];case2:returnarr[0]+arr[1]*BASE;default:returnarr[0]+(arr[1]+arr[2]*BASE)*BASE}}returnarr}functiontrim(v){vari=v.length;while(v[--i]===0);v.length=i+1}functioncreateArray(length){varx=newArray(length);vari=-1;while(++i<length){x[i]=0}returnx}functiontruncate(n){if(n>0)returnMath.floor(n);returnMath.ceil(n)}functionadd(a,b){varl_a=a.length,l_b=b.length,r=newArray(l_a),carry=0,base=BASE,sum,i;for(i=0;i<l_b;i++){sum=a[i]+b[i]+carry;carry=sum>=base?1:0;r[i]=sum-carry*base}while(i<l_a){sum=a[i]+carry;carry=sum===base?1:0;r[i++]=sum-carry*base}if(carry>0)r.push(carry);returnr}functionaddAny(a,b){if(a.length>=b.length)returnadd(a,b);returnadd(b,a)}functionaddSmall(a,carry){varl=a.length,r=newArray(l),base=BASE,sum,i;for(i=0;i<l;i++){sum=a[i]-base+carry;carry=Math.floor(sum/base);r[i]=sum-carry*base;carry+=1}while(carry>0){r[i++]=carry%base;carry=Math.floor(carry/base)}returnr}BigInteger.prototype.add=function(v){varn=parseValue(v);if(this.sign!==n.sign){returnthis.subtract(n.negate())}vara=this.value,b=n.value;if(n.isSmall){returnnewBigInteger(addSmall(a,Math.abs(b)),this.sign)}returnnewBigInteger(addAny(a,b),this.sign)};BigInteger.prototype.plus=BigInteger.prototype.add;SmallInteger.prototype.add=function(v){varn=parseValue(v);vara=this.value;if(a<0!==n.sign){returnthis.subtract(n.negate())}varb=n.value;if(n.isSmall){if(isPrecise(a+b))returnnewSmallInteger(a+b);b=smallToArray(Math.abs(b))}returnnewBigInteger(addSmall(b,Math.abs(a)),a<0)};SmallInteger.prototype.plus=SmallInteger.prototype.add;NativeBigInt.prototype.add=function(v){returnnewNativeBigInt(this.value+parseValue(v).value)};NativeBigInt.prototype.plus=NativeBigInt.prototype.add;functionsubtract(a,b){vara_l=a.length,b_l=b.length,r=newArray(a_l),borrow=0,base=BASE,i,difference;for(i=0;i<b_l;i++){difference=a[i]-borrow-b[i];if(difference<0){difference+=base;borrow=1}elseborrow=0;r[i]=difference}for(i=b_l;i<a_l;i++){difference=a[i]-borrow;if(difference<0)difference+=base;else{r[i++]=difference;break}r[i]=difference}for(;i<a_l;i++){r[i]=a[i]}trim(r);returnr}functionsubtractAny(a,b,sign){varvalue;if(compareAbs(a,b)>=0){value=subtract(a,b)}else{value=subtract(b,a);sign=!sign}value=arrayToSmall(value);if(typeofvalue==="number"){if(sign)value=-value;returnnewSmallInteger(value)}returnnewBigInteger(value,sign)}functionsubtractSmall(a,b,sign){varl=a.length,r=newArray(l),carry=-b,base=BASE,i,difference;for(i=0;i<l;i++){difference=a[i]+carry;carry=Math.floor(difference/base);difference%=base;r[i]=difference<0?difference+base:difference}r=arrayToSmall(r);if(typeofr==="number"){if(sign)r=-r;returnnewSmallInteger(r)}returnnewBigInteger(r,sign)}BigInteger.prototype.subtract=function(v){varn=parseValue(v);if(this.sign!==n.sign){returnthis.add(n.negate())}vara=this.value,b=n.value;if(n.isSmall)returnsubtractSmall(a,Math.abs(b),this.sign);returnsubtractAny(a,b,this.sign)};BigInteger.prototype.minus=BigInteger.prototype.subtract;SmallInteger.prototype.subtract=function(v){varn=parseValue(v);vara=this.value;if(a<0!==n.sign){returnthis.add(n.negate())}varb=n.value;if(n.isSmall){returnnewSmallInteger(a-b)}returnsubtractSmall(b,Math.abs(a),a>=0)};SmallInteger.prototype.minus=SmallInteger.prototype.subtract;NativeBigInt.prototype.subtract=function(v){returnnewNativeBigInt(this.value-parseValue(v).value)};NativeBigInt.prototype.minus=NativeBigInt.prototype.subtract;BigInteger.prototype.negate=function(){returnnewBigInteger(this.value,!this.sign)};SmallInteger.prototype.negate=function(){varsign=this.sign;varsmall=newSmallInteger(-this.value);small.sign=!sign;returnsmall};NativeBigInt.prototype.negate=function(){returnnewNativeBigInt(-this.value)};BigInteger.prototype.abs=function(){returnnewBigInteger(this.value,false)};SmallInteger.prototype.abs=function(){returnnewSmallInteger(Math.abs(this.value))};NativeBigInt.prototype.abs=function(){returnnewNativeBigInt(this.value>=0?this.value:-this.value)};functionmultiplyLong(a,b){vara_l=a.length,b_l=b.length,l=a_l+b_l,r=createArray(l),base=BASE,product,carry,i,a_i,b_j;for(i=0;i<a_l;++i){a_i=a[i];for(varj=0;j<b_l;++j){b_j=b[j];product=a_i*b_j+r[i+j];carry=Math.floor(product/base);r[i+j]=product-carry*base;r[i+j+1]+=carry}}trim(r);returnr}functionmultiplySmall(a,b){varl=a.length,r=newArray(l),base=BASE,carry=0,product,i;for(i=0;i<l;i++){product=a[i]*b+carry;carry=Math.floor(product/base);r[i]=product-carry*base}while(carry>0){r[i++]=carry%base;carry=Math.floor(carry/base)}returnr}functionshiftLeft(x,n){varr=[];while(n-->0)r.push(0);returnr.concat(x)}functionmultiplyKaratsuba(x,y){varn=Math.max(x.length,y.length);if(n<=30)returnmultiplyLong(x,y);n=Math.ceil(n/2);varb=x.slice(n),a=x.slice(0,n),d=y.slice(n),c=y.slice(0,n);varac=multiplyKaratsuba(a,c),bd=multiplyKaratsuba(b,d),abcd=multiplyKaratsuba(addAny(a,b),addAny(c,d));varproduct=addAny(addAny(ac,shiftLeft(subtract(subtract(abcd,ac),bd),n)),shiftLeft(bd,2*n));trim(product);returnproduct}functionuseKaratsuba(l1,l2){return-.012*l1-.012*l2+15e-6*l1*l2>0}BigInteger.prototype.multiply=function(v){varn=parseValue(v),a=this.value,b=n.value,sign=this.sign!==n.sign,abs;if(n.isSmall){if(b===0)returnInteger[0];if(b===1)returnthis;if(b===-1)returnthis.negate();abs=Math.abs(b);if(abs<BASE){returnnewBigInteger(multiplySmall(a,abs),sign)}b=smallToArray(abs)}if(useKaratsuba(a.length,b.length))returnnewBigInteger(multiplyKaratsuba(a,b),sign);returnnewBigInteger(multiplyLong(a,b),sign)};BigInteger.prototype.times=BigInteger.prototype.multiply;functionmultiplySmallAndArray(a,b,sign){if(a<BASE){returnnewBigInteger(multiplySmall(b,a),sign)}returnnewBigInteger(multiplyLong(b,smallToArray(a)),sign)}SmallInteger.prototype._multiplyBySmall=function(a){if(isPrecise(a.value*this.value)){returnnewSmallInteger(a.value*this.value)}returnmultiplySmallAndArray(Math.abs(a.value),smallToArray(Math.abs(this.value)),this.sign!==a.sign)};BigInteger.prototype._multiplyBySmall=function(a){if(a.value===0)returnInteger[0];if(a.value===1)returnthis;if(a.value===-1)returnthis.negate();returnmultiplySmallAndArray(Math.abs(a.value),this.value,this.sign!==a.sign)};SmallInteger.prototype.multiply=function(v){returnparseValue(v)._multiplyBySmall(this)};SmallInteger.prototype.times=SmallInteger.prototype.multiply;NativeBigInt.prototype.multiply=function(v){returnnewNativeBigInt(this.value*parseValue(v).value)};NativeBigInt.prototype.times=NativeBigInt.prototype.multiply;functionsquare(a){varl=a.length,r=createArray(l+l),base=BASE,product,carry,i,a_i,a_j;for(i=0;i<l;i++){a_i=a[i];carry=0-a_i*a_i;for(varj=i;j<l;j++){a_j=a[j];product=2*(a_i*a_j)+r[i+j]+carry;carry=Math.floor(product/base);r[i+j]=product-carry*base}r[i+l]=carry}trim(r);returnr}BigInteger.prototype.square=function(){returnnewBigInteger(square(this.value),false)};SmallInteger.prototype.square=function(){varvalue=this.value*this.value;if(isPrecise(value))returnnewSmallInteger(value);returnnewBigInteger(square(smallToArray(Math.abs(this.value))),false)};NativeBigInt.prototype.square=function(v){returnnewNativeBigInt(this.value*this.value)};functiondivMod1(a,b){vara_l=a.length,b_l=b.length,base=BASE,result=createArray(b.length),divisorMostSignificantDigit=b[b_l-1],lambda=Math.ceil(base/(2*divisorMostSignificantDigit)),remainder=multiplySmall(a,lambda),divisor=multiplySmall(b,lambda),quotientDigit,shift,carry,borrow,i,l,q;if(remainder.length<=a_l)remainder.push(0);divisor.push(0);divisorMostSignificantDigit=divisor[b_l-1];for(shift=a_l-b_l;shift>=0;shift--){quotientDigit=base-1;if(remainder[shift+b_l]!==divisorMostSignificantDigit){quotientDigit=Math.floor((remainder[shift+b_l]*base+remainder[shift+b_l-1])/divisorMostSignificantDigit)}carry=0;borrow=0;l=divisor.length;for(i=0;i<l;i++){carry+=quotientDigit*divisor[i];q=Math.floor(carry/base);borrow+=remainder[shift+i]-(carry-q*base);carry=q;if(borrow<0){remainder[shift+i]=borrow+base;borrow=-1}else{remainder[shift+i]=borrow;borrow=0}}while(borrow!==0){quotientDigit-=1;carry=0;for(i=0;i<l;i++){carry+=remainder[shift+i]-base+divisor[i];if(carry<0){remainder[shift+i]=carry+base;carry=0}else{remainder[shift+i]=carry;carry=1}}borrow+=carry}result[shift]=quotientDigit}remainder=divModSmall(remainder,lambda)[0];return[arrayToSmall(result),arrayToSmall(remainder)]}functiondivMod2(a,b){vara_l=a.length,b_l=b.length,result=[],part=[],base=BASE,guess,xlen,highx,highy,check;while(a_l){part.unshift(a[--a_l]);trim(part);if(compareAbs(part,b)<0){result.push(0);continue}xlen=part.length;highx=part[xlen-1]*base+part[xlen-2];highy=b[b_l-1]*base+b[b_l-2];if(xlen>b_l){highx=(highx+1)*base}guess=Math.ceil(highx/highy);do{check=multiplySmall(b,guess);if(compareAbs(check,part)<=0)break;guess--}while(guess);result.push(guess);part=subtract(part,check)}result.reverse();return[arrayToSmall(result),arrayToSmall(part)]}functiondivModSmall(value,lambda){varlength=value.length,quotient=createArray(length),base=BASE,i,q,remainder,divisor;remainder=0;for(i=length-1;i>=0;--i){divisor=remainder*base+value[i];q=truncate(divisor/lambda);remainder=divisor-q*lambda;quotient[i]=q|0}return[quotient,remainder|0]}functiondivModAny(self,v){varvalue,n=parseValue(v);if(supportsNativeBigInt){return[newNativeBigInt(self.value/n.value),newNativeBigInt(self.value%n.value)]}vara=self.value,b=n.value;varquotient;if(b===0)thrownewError("Cannot divide by zero");if(self.isSmall){if(n.isSmall){return[newSmallInteger(truncate(a/b)),newSmallInteger(a%b)]}return[Integer[0],self]}if(n.isSmall){if(b===1)return[self,Integer[0]];if(b==-1)return[self.negate(),Integer[0]];varabs=Math.abs(b);if(abs<BASE){value=divModSmall(a,abs);quotient=arrayToSmall(value[0]);varremainder=value[1];if(self.sign)remainder=-remainder;if(typeofquotient==="number"){if(self.sign!==n.sign)quotient=-quotient;return[newSmallInteger(quotient),newSmallInteger(remainder)]}return[newBigInteger(quotient,self.sign!==n.sign),newSmallInteger(remainder)]}b=smallToArray(abs)}varcomparison=compareAbs(a,b);if(comparison===-1)return[Integer[0],self];if(comparison===0)return[Integer[self.sign===n.sign?1:-1],Integer[0]];if(a.length+b.length<=200)value=divMod1(a,b);elsevalue=divMod2(a,b);quotient=value[0];varqSign=self.sign!==n.sign,mod=value[1],mSign=self.sign;if(typeofquotient==="number"){if(qSign)quotient=-quotient;quotient=newSmallInteger(quotient)}elsequotient=newBigInteger(quotient,qSign);if(typeofmod==="number"){if(mSign)mod=-mod;mod=newSmallInteger(mod)}elsemod=newBigInteger(mod,mSign);return[quotient,mod]}BigInteger.prototype.divmod=function(v){varresult=divModAny(this,v);return{quotient:result[0],remainder:result[1]}};NativeBigInt.prototype.divmod=SmallInteger.prototype.divmod=BigInteger.prototype.divmod;BigInteger.prototype.divide=function(v){returndivModAny(this,v)[0]};NativeBigInt.prototype.over=NativeBigInt.prototype.divide=function(v){returnnewNativeBigInt(this.value/parseValue(v).value)};SmallInteger.prototype.over=SmallInteger.prototype.divide=BigInteger.prototype.over=BigInteger.prototype.divide;BigInteger.prototype.mod=function(v){returndivModAny(this,v)[1]};NativeBigInt.prototype.mod=NativeBigInt.prototype.remainder=function(v){returnnewNativeBigInt(this.value%parseValue(v).value)};SmallInteger.prototype.remainder=SmallInteger.prototype.mod=BigInteger.prototype.remainder=BigInteger.prototype.mod;BigInteger.prototype.pow=function(v){varn=parseValue(v),a=this.value,b=n.value,value,x,y;if(b===0)returnInteger[1];if(a===0)returnInteger[0];if(a===1)returnInteger[1];if(a===-1)returnn.isEven()?Integer[1]:Integer[-1];if(n.sign){returnInteger[0]}if(!n.isSmall)thrownewError("The exponent "+n.toString()+" is too large.");if(this.isSmall){if(isPrecise(value=Math.pow(a,b)))returnnewSmallInteger(truncate(value))}x=this;y=Integer[1];while(true){if(b&1===1){y=y.times(x);--b}if(b===0)break;b/=2;x=x.square()}returny};SmallInteger.prototype.pow=BigInteger.prototype.pow;NativeBigInt.prototype.pow=function(v){varn=parseValue(v);vara=this.value,b=n.value;var_0=BigInt(0),_1=BigInt(1),_2=BigInt(2);if(b===_0)returnInteger[1];if(a===_0)returnInteger[0];if(a===_1)returnInteger[1];if(a===BigInt(-1))returnn.isEven()?Integer[1]:Integer[-1];if(n.isNegative())returnnewNativeBigInt(_0);varx=this;vary=Integer[1];while(true){if((b&_1)===_1){y=y.times(x);--b}if(b===_0)break;b/=_2;x=x.square()}returny};BigInteger.prototype.modPow=function(exp,mod){exp=parseValue(exp);mod=parseValue(mod);if(mod.isZero())thrownewError("Cannot take modPow with modulus 0");varr=Integer[1],base=this.mod(mod);if(exp.isNegative()){exp=exp.multiply(Integer[-1]);base=base.modInv(mod)}while(exp.isPositive()){if(base.isZero())returnInteger[0];if(exp.isOdd())r=r.multiply(base).mod(mod);exp=exp.divide(2);base=base.square().mod(mod)}returnr};NativeBigInt.prototype.modPow=SmallInteger.prototype.modPow=BigInteger.prototype.modPow;functioncompareAbs(a,b){if(a.length!==b.length){returna.length>b.length?1:-1}for(vari=a.length-1;i>=0;i--){if(a[i]!==b[i])returna[i]>b[i]?1:-1}return0}BigInteger.prototype.compareAbs=function(v){varn=parseValue(v),a=this.value,b=n.value;if(n.isSmall)return1;returncompareAbs(a,b)};SmallInteger.prototype.compareAbs=function(v){varn=parseValue(v),a=Math.abs(this.value),b=n.value;if(n.isSmall){b=Math.abs(b);returna===b?0:a>b?1:-1}return-1};NativeBigInt.prototype.compareAbs=function(v){vara=this.value;varb=parseValue(v).value;a=a>=0?a:-a;b=b>=0?b:-b;returna===b?0:a>b?1:-1};BigInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return1}varn=parseValue(v),a=this.value,b=n.value;if(this.sign!==n.sign){returnn.sign?1:-1}if(n.isSmall){returnthis.sign?-1:1}returncompareAbs(a,b)*(this.sign?-1:1)};BigInteger.prototype.compareTo=BigInteger.prototype.compare;SmallInteger.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return1}varn=parseValue(v),a=this.value,b=n.value;if(n.isSmall){returna==b?0:a>b?1:-1}if(a<0!==n.sign){returna<0?-1:1}returna<0?1:-1};SmallInteger.prototype.compareTo=SmallInteger.prototype.compare;NativeBigInt.prototype.compare=function(v){if(v===Infinity){return-1}if(v===-Infinity){return1}vara=this.value;varb=parseValue(v).value;returna===b?0:a>b?1:-1};NativeBigInt.prototype.compareTo=NativeBigInt.prototype.compare;BigInteger.prototype.equals=function(v){returnthis.compare(v)===0};NativeBigInt.prototype.eq=NativeBigInt.prototype.equals=SmallInteger.prototype.eq=SmallInteger.prototype.equals=BigInteger.prototype.eq=BigInteger.prototype.equals;BigInteger.prototype.notEquals=function(v){returnthis.compare(v)!==0};NativeBigInt.prototype.neq=NativeBigInt.prototype.notEquals=SmallInteger.prototype.neq=SmallInteger.prototype.notEquals=BigInteger.prototype.neq=BigInteger.prototype.notEquals;BigInteger.prototype.greater=function(v){returnthis.compare(v)>0};NativeBigInt.prototype.gt=NativeBigInt.prototype.greater=SmallInteger.prototype.gt=SmallInteger.prototype.greater=BigInteger.prototype.gt=BigInteger.prototype.greater;BigInteger.prototype.lesser=function(v){returnthis.compare(v)<0};NativeBigInt.prototype.lt=NativeBigInt.prototype.lesser=SmallInteger.prototype.lt=SmallInteger.prototype.lesser=BigInteger.prototype.lt=BigInteger.prototype.lesser;BigInteger.prototype.greaterOrEquals=function(v){returnthis.compare(v)>=0};NativeBigInt.prototype.geq=NativeBigInt.prototype.greaterOrEquals=SmallInteger.prototype.geq=SmallInteger.prototype.greaterOrEquals=BigInteger.prototype.geq=BigInteger.prototype.greaterOrEquals;BigInteger.prototype.lesserOrEquals=function(v){returnthis.compare(v)<=0};NativeBigInt.prototype.leq=NativeBigInt.prototype.lesserOrEquals=SmallInteger.prototype.leq=SmallInteger.prototype.lesserOrEquals=BigInteger.prototype.leq=BigInteger.prototype.lesserOrEquals;BigInteger.prototype.isEven=function(){return(this.value[0]&1)===0};SmallInteger.prototype.isEven=function(){return(this.value&1)===0};NativeBigInt.prototype.isEven=function(){return(this.value&BigInt(1))===BigInt(0)};BigInteger.prototype.isOdd=function(){return(this.value[0]&1)===1};SmallInteger.prototype.isOdd=function(){return(this.value&1)===1};NativeBigInt.prototype.isOdd=function(){return(this.value&BigInt(1))===BigInt(1)};BigInteger.prototype.isPositive=function(){return!this.sign};SmallInteger.prototype.isPositive=function(){returnthis.value>0};NativeBigInt.prototype.isPositive=SmallInteger.prototype.isPositive;BigInteger.prototype.isNegative=function(){returnthis.sign};SmallInteger.prototype.isNegative=function(){returnthis.value<0};NativeBigInt.prototype.isNegative=SmallInteger.prototype.isNegative;BigInteger.prototype.isUnit=function(){returnfalse};SmallInteger.prototype.isUnit=function(){returnMath.abs(this.value)===1};NativeBigInt.prototype.isUnit=function(){returnthis.abs().value===BigInt(1)};BigInteger.prototype.isZero=function(){returnfalse};SmallInteger.prototype.isZero=function(){returnthis.value===0};NativeBigInt.prototype.isZero=function(){returnthis.value===BigInt(0)};BigInteger.prototype.isDivisibleBy=function(v){varn=parseValue(v);if(n.isZero())returnfalse;if(n.isUnit())returntrue;if(n.compareAbs(2)===0)returnthis.isEven();returnthis.mod(n).isZero()};NativeBigInt.prototype.isDivisibleBy=SmallInteger.prototype.isDivisibleBy=BigInteger.prototype.isDivisibleBy;functionisBasicPrime(v){varn=v.abs();if(n.isUnit())returnfalse;if(n.equals(2)||n.equals(3)||n.equals(5))returntrue;if(n.isEven()||n.isDivisibleBy(3)||n.isDivisibleBy(5))returnfalse;if(n.lesser(49))returntrue}functionmillerRabinTest(n,a){varnPrev=n.prev(),b=nPrev,r=0,d,t,i,x;while(b.isEven())b=b.divide(2),r++;next:for(i=0;i<a.length;i++){if(n.lesser(a[i]))continue;x=bigInt(a[i]).modPow(b,n);if(x.isUnit()||x.equals(nPrev))continue;for(d=r-1;d!=0;d--){x=x.square().mod(n);if(x.isUnit())returnfalse;if(x.equals(nPrev))continuenext}returnfalse}returntrue}BigInteger.prototype.isPrime=function(strict){varisPrime=isBasicPrime(this);if(isPrime!==undefined)returnisPrime;varn=this.abs();varbits=n.bitLength();if(bits<=64)returnmillerRabinTest(n,[2,3,5,7,11,13,17,19,23,29,31,37]);varlogN=Math.log(2)*bits.toJSNumber();vart=Math.ceil(strict===true?2*Math.pow(logN,2):logN);for(vara=[],i=0;i<t;i++){a.push(bigInt(i+2))}returnmillerRabinTest(n,a)};NativeBigInt.prototype.isPrime=SmallInteger.prototype.isPrime=BigInteger.prototype.isPrime;BigInteger.prototype.isProbablePrime=function(iterations,rng){varisPrime=isBasicPrime(this);if(isPrime!==undefined)returnisPrime;varn=this.abs();vart=iterations===undefined?5:iterations;for(vara=[],i=0;i<t;i++){a.push(bigInt.randBetween(2,n.minus(2),rng))}returnmillerRabinTest(n,a)};NativeBigInt.prototype.isProbablePrime=SmallInteger.prototype.isProbablePrime=BigInteger.prototype.isProbablePrime;BigInteger.prototype.modInv=function(n){vart=bigInt.zero,newT=bigInt.one,r=parseValue(n),newR=this.abs(),q,lastT,lastR;while(!newR.isZero()){q=r.divide(newR);lastT=t;lastR=r;t=newT;r=newR;newT=lastT.subtract(q.multiply(newT));newR=lastR.subtract(q.multiply(newR))}if(!r.isUnit())thrownewError(this.toString()+" and "+n.toString()+" are not co-prime");if(t.compare(0)===-1){t=t.add(n)}if(this.isNegative()){returnt.negate()}returnt};NativeBigInt.prototype.modInv=SmallInteger.prototype.modInv=BigInteger.prototype.modInv;BigInteger.prototype.next=function(){varvalue=this.value;if(this.sign){returnsubtractSmall(value,1,this.sign)}returnnewBigInteger(addSmall(value,1),this.sign)};SmallInteger.prototype.next=function(){varvalue=this.value;if(value+1<MAX_INT)returnnewSmallInteger(value+1);returnnewBigInteger(MAX_INT_ARR,false)};NativeBigInt.prototype.next=function(){returnnewNativeBigInt(this.value+BigInt(1))};BigInteger.prototype.prev=function(){varvalue=this.value;if(this.sign){returnnewBigInteger(addSmall(value,1),true)}returnsubtractSmall(value,1,this.sign)};SmallInteger.prototype.prev=function(){varvalue=this.value;if(value-1>-MAX_INT)returnnewSmallInteger(value-1);returnnewBigInteger(MAX_INT_ARR,true)};NativeBigInt.prototype.prev=function(){returnnewNativeBigInt(this.value-BigInt(1))};varpowersOfTwo=[1];while(2*powersOfTwo[powersOfTwo.length-1]<=BASE)powersOfTwo.push(2*powersOfTwo[powersOfTwo.length-1]);varpowers2Length=powersOfTwo.length,highestPower2=powersOfTwo[powers2Length-1];functionshift_isSmall(n){returnMath.abs(n)<=BASE}BigInteger.prototype.shiftLeft=function(v){varn=parseValue(v).toJSNumber();if(!shift_isSmall(n)){thrownewError(String(n)+" is too large for shifting.")}if(n<0)returnthis.shiftRight(-n);varresult=this;if(result.isZero())returnresult;while(n>=powers2Length){result=result.multiply(highestPower2);n-=powers2Length-1}returnresult.multiply(powersOfTwo[n])};NativeBigInt.prototype.shiftLeft=SmallInteger.prototype.shiftLeft=BigInteger.prototype.shiftLeft;BigInteger.prototype.shiftRight=function(v){varremQuo;varn=parseValue(v).toJSNumber();if(!shift_isSmall(n)){thrownewError(String(n)+" is too large for shifting.")}if(n<0)returnthis.shiftLeft(-n);varresult=this;while(n>=powers2Length){if(result.isZero()||result.isNegative()&&result.isUnit())returnresult;remQuo=divModAny(result,highestPower2);result=remQuo[1].isNegative()?remQuo[0].prev():remQuo[0];n-=powers2Length-1}remQuo=divModAny(result,powersOfTwo[n]);returnremQuo[1].isNegative()?remQuo[0].prev():remQuo[0]};NativeBigInt.prototype.shiftRight=SmallInteger.prototype.shiftRight=BigInteger.prototype.shiftRight;functionbitwise(x,y,fn){y=parseValue(y);varxSign=x.isNegative(),ySign=y.isNegative();varxRem=xSign?x.not():x,yRem=ySign?y.not():y;varxDigit=0,yDigit=0;varxDivMod=null,yDivMod=null;varresult=[];while(!xRem.isZero()||!yRem.isZero()){xDivMod=divModAny(xRem,highestPower2);xDigit=xDivMod[1].toJSNumber();if(xSign){xDigit=highestPower2-1-xDigit}yDivMod=divModAny(yRem,highestPower2);yDigit=yDivMod[1].toJSNumber();if(ySign){yDigit=highestPower2-1-yDigit}xRem=xDivMod[0];yRem=yDivMod[0];result.push(fn(xDigit,yDigit))}varsum=fn(xSign?1:0,ySign?1:0)!==0?bigInt(-1):bigInt(0);for(vari=result.length-1;i>=0;i-=1){sum=sum.multiply(highestPower2).add(bigInt(result[i]))}returnsum}BigInteger.prototype.not=function(){returnthis.negate().prev()};NativeBigInt.prototype.not=SmallInteger.prototype.not=BigInteger.prototype.not;BigInteger.prototype.and=function(n){returnbitwise(this,n,function(a,b){returna&b})};NativeBigInt.prototype.and=SmallInteger.prototype.and=BigInteger.prototype.and;BigInteger.prototype.or=function(n){returnbitwise(this,n,function(a,b){returna|b})};NativeBigInt.prototype.or=SmallInteger.prototype.or=BigInteger.prototype.or;BigInteger.prototype.xor=function(n){returnbitwise(this,n,function(a,b){returna^b})};NativeBigInt.prototype.xor=SmallInteger.prototype.xor=BigInteger.prototype.xor;varLOBMASK_I=1<<30,LOBMASK_BI=(BASE&-BASE)*(BASE&-BASE)|LOBMASK_I;functionroughLOB(n){varv=n.value,x=typeofv==="number"?v|LOBMASK_I:typeofv==="bigint"?v|BigInt(LOBMASK_I):v[0]+v[1]*BASE|LOBMASK_BI;returnx&-x}functionintegerLogarithm(value,base){if(base.compareTo(value)<=0){vartmp=integerLogarithm(value,base.square(base));varp=tmp.p;vare=tmp.e;vart=p.multiply(base);returnt.compareTo(value)<=0?{p:t,e:e*2+1}:{p:p,e:e*2}}return{p:bigInt(1),e:0}}BigInteger.prototype.bitLength=function(){varn=this;if(n.compareTo(bigInt(0))<0){n=n.negate().subtract(bigInt(1))}if(n.compareTo(bigInt(0))===0){returnbigInt(0)}returnbigInt(integerLogarithm(n,bigInt(2)).e).add(bigInt(1))};NativeBigInt.prototype.bitLength=SmallInteger.prototype.bitLength=BigInteger.prototype.bitLength;functionmax(a,b){a=parseValue(a);b=parseValue(b);returna.greater(b)?a:b}functionmin(a,b){a=parseValue(a);b=parseValue(b);returna.lesser(b)?a:b}functiongcd(a,b){a=parseValue(a).abs();b=parseValue(b).abs();if(a.equals(b))returna;if(a.isZero())returnb;if(b.isZero())returna;varc=Integer[1],d,t;while(a.isEven()&&b.isEven()){d=min(roughLOB(a),roughLOB(b));a=a.divide(d);b=b.divide(d);c=c.multiply(d)}while(a.isEven()){a=a.divide(roughLOB(a))}do{while(b.isEven()){b=b.divide(roughLOB(b))}if(a.greater(b)){t=b;b=a;a=t}b=b.subtract(a)}while(!b.isZero());returnc.isUnit()?a:a.multiply(c)}functionlcm(a,b){a=parseValue(a).abs();b=parseValue(b).abs();returna.divide(gcd(a,b)).multiply(b)}functionrandBetween(a,b,rng){a=parseValue(a);b=parseValue(b);varusedRNG=rng||Math.random;varlow=min(a,b),high=max(a,b);varrange=high.subtract(low).add(1);if(range.isSmall)returnlow.add(Math.floor(usedRNG()*range));vardigits=toBase(range,BASE).value;varresult=[],restricted=true;for(vari=0;i<digits.length;i++){vartop=restricted?digits[i]:BASE;vardigit=truncate(usedRNG()*top);result.push(digit);if(digit<top)restricted=false}returnlow.add(Integer.fromArray(result,BASE,false))}varparseBase=function(text,base,alphabet,caseSensitive){alphabet=alphabet||DEFAULT_ALPHABET;text=String(text);if(!caseSensitive){text=text.toLowerCase();alphabet=alphabet.toLowerCase()}varlength=text.length;vari;varabsBase=Math.abs(base);varalphabetValues={};for(i=0;i<alphabet.length;i++){alphabetValues[alphabet[i]]=i}for(i=0;i<length;i++){varc=text[i];if(c==="-")continue;if(cinalphabetValues){if(alphabetValues[c]>=absBase){if(c==="1"&&absBase===1)continue;thrownewError(c+" is not a valid digit in base "+base+".")}}}base=parseValue(base);vardigits=[];varisNegative=text[0]==="-";for(i=isNegative?1:0;i<text.length;i++){varc=text[i];if(cinalphabetValues)digits.push(parseValue(alphabetValues[c]));elseif(c==="<"){varstart=i;do{i++}while(text[i]!==">"&&i<text.length);digits.push(parseValue(text.slice(start+1,i)))}elsethrownewError(c+" is not a valid character")}returnparseBaseFromArray(digits,base,isNegative)};functionparseBaseFromArray(digits,base,isNegative){varval=Integer[0],pow=Integer[1],i;for(i=digits.length-1;i>=0;i--){val=val.add(digits[i].times(pow));pow=pow.times(base)}returnisNegative?val.negate():val}functionstringify(digit,alphabet){alphabet=alphabet||DEFAULT_ALPHABET;if(digit<alphabet.length){returnalphabet[digit]}return"<"+digit+">"}functiontoBase(n,base){base=bigInt(base);if(base.isZero()){if(n.isZero())return{value:[0],isNegative:false};thrownewError("Cannot convert nonzero numbers to base 0.")}if(base.equals(-1)){if(n.isZero())return{value:[0],isNegative:false};if(n.isNegative())return{value:[].concat.apply([],Array.apply(null,Array(-n.toJSNumber())).map(Array.prototype.valueOf,[1,0])),isNegative:false};vararr=Array.apply(null,Array(n.toJSNumber()-1)).map(Array.prototype.valueOf,[0,1]);arr.unshift([1]);return{value:[].concat.apply([],arr),isNegative:false}}varneg=false;if(n.isNegative()&&base.isPositive()){neg=true;n=n.abs()}if(base.isUnit()){if(n.isZero())return{value:[0],isNegative:false};return{value:Array.apply(null,Array(n.toJSNumber())).map(Number.prototype.valueOf,1),isNegative:neg}}varout=[];varleft=n,divmod;while(left.isNegative()||left.compareAbs(base)>=0){divmod=left.divmod(base);left=divmod.quotient;vardigit=divmod.remainder;if(digit.isNegative()){digit=base.minus(digit).abs();left=left.next()}out.push(digit.toJSNumber())}out.push(left.toJSNumber());return{value:out.reverse(),isNegative:neg}}functiontoBaseString(n,base,alphabet){vararr=toBase(n,base);return(arr.isNegative?"-":"")+arr.value.map(function(x){returnstringify(x,alphabet)}).join("")}BigInteger.prototype.toArray=function(radix){returntoBase(this,radix)};SmallInteger.prototype.toArray=function(radix){returntoBase(this,radix)};NativeBigInt.prototype.toArray=function(radix){returntoBase(this,radix)};BigInteger.prototype.toString=function(radix,alphabet){if(radix===undefined)radix=10;if(radix!==10)returntoBaseString(this,radix,alphabet);varv=this.value,l=v.length,str=String(v[--l]),zeros="0000000",digit;while(--l>=0){digit=String(v[l]);str+=zeros.slice(digit.length)+digit}varsign=this.sign?"-":"";returnsign+str};SmallInteger.prototype.toString=function(radix,alphabet){if(radix===undefined)radix=10;if(radix!=10)returntoBaseString(this,radix,alphabet);returnString(this.value)};NativeBigInt.prototype.toString=SmallInteger.prototype.toString;NativeBigInt.prototype.toJSON=BigInteger.prototype.toJSON=SmallInteger.prototype.toJSON=function(){returnthis.toString()};BigInteger.prototype.valueOf=function(){returnparseInt(this.toString(),10)};BigInteger.prototype.toJSNumber=BigInteger.prototype.valueOf;SmallInteger.prototype.valueOf=function(){returnthis.value};SmallInteger.prototype.toJSNumber=SmallInteger.prototype.valueOf;NativeBigInt.prototype.valueOf=NativeBigInt.prototype.toJSNumber=function(){returnparseInt(this.toString(),10)};functionparseStringValue(v){if(isPrecise(+v)){varx=+v;if(x===truncate(x))returnsupportsNativeBigInt?newNativeBigInt(BigInt(x)):newSmallInteger(x);thrownewError("Invalid integer: "+v)}varsign=v[0]==="-";if(sign)v=v.slice(1);varsplit=v.split(/e/i);if(split.length>2)thrownewError("Invalid integer: "+split.join("e"));if(split.length===2){varexp=split[1];if(exp[0]==="+")exp=exp.slice(1);exp=+exp;if(exp!==truncate(exp)||!isPrecise(exp))thrownewError("Invalid integer: "+exp+" is not a valid exponent.");vartext=split[0];vardecimalPlace=text.indexOf(".");if(decimalPlace>=0){exp-=text.length-decimalPlace-1;text=text.slice(0,decimalPlace)+text.slice(decimalPlace+1)}if(exp<0)thrownewError("Cannot include negative exponent part for integers");text+=newArray(exp+1).join("0");v=text}varisValid=/^([0-9][0-9]*)$/.test(v);if(!isValid)thrownewError("Invalid integer: "+v);if(supportsNativeBigInt){returnnewNativeBigInt(BigInt(sign?"-"+v:v))}varr=[],max=v.length,l=LOG_BASE,min=max-l;while(max>0){r.push(+v.slice(min,max));min-=l;if(min<0)min=0;max-=l}trim(r);returnnewBigInteger(r,sign)}functionparseNumberValue(v){if(supportsNativeBigInt){returnnewNativeBigInt(BigInt(v))}if(isPrecise(v)){if(v!==truncate(v))thrownewError(v+" is not an integer.");returnnewSmallInteger(v)}returnparseStringValue(v.toString())}functionparseValue(v){if(typeofv==="number"){returnparseNumberValue(v)}if(typeofv==="string"){returnparseStringValue(v)}if(typeofv==="bigint"){returnnewNativeBigInt(v)}returnv}for(vari=0;i<1e3;i++){Integer[i]=parseValue(i);if(i>0)Integer[-i]=parseValue(-i)}Integer.one=Integer[1];Integer.zero=Integer[0];Integer.minusOne=Integer[-1];Integer.max=max;Integer.min=min;Integer.gcd=gcd;Integer.lcm=lcm;Integer.isInstance=function(x){returnxinstanceofBigInteger||xinstanceofSmallInteger||xinstanceofNativeBigInt};Integer.randBetween=randBetween;Integer.fromArray=function(digits,base,isNegative){returnparseBaseFromArray(digits.map(parseValue),parseValue(base||10),isNegative)};returnInteger}();if(typeofmodule!=="undefined"&&module.hasOwnProperty("exports")){module.exports=bigInt}if(typeofdefine==="function"&&define.amd){define(function(){returnbigInt})}