"use strict";var bigInt=function(){function r(e,t){this.value=e,this.sign=t}function i(e){var t=[],n=0;for(var r=e.length-1;r>=0;r--){if(r>n&&e[r]===0)continue;n=r,t[r]=e[r]}return t}function s(e,t){return e=d(e).abs(),t=d(t).abs(),e.equals(t)?e:e.equals(c)?t:t.equals(c)?e:e.isEven()?t.isOdd()?s(e.divide(2),t):s(e.divide(2),t.divide(2)).multiply(2):t.isEven()?s(e,t.divide(2)):e.greater(t)?s(e.subtract(t).divide(2),t):s(t.subtract(e).divide(2),e)}function o(e,t){return e=d(e).abs(),t=d(t).abs(),e.multiply(t).divide(s(e,t))}function u(e,t){return e=d(e),t=d(t),e.greater(t)?e:t}function a(e,t){return e=d(e),t=d(t),e.lesser(t)?e:t}function f(t,n){t=d(t),n=d(n);var i=a(t,n),s=u(t,n),o=s.subtract(i),f=o.value.length-1,l=[],c=!0;for(var h=f;h>=0;h--){var p=c?o.value[h]:e,v=Math.floor(Math.random()*p);l.unshift(v),v
2)throw new Error("Invalid integer: "+i.join("e"));if(i[1]){var u=i[1];u[0]==="+"&&(u=u.slice(1)),u=d(u);if(u.lesser(0))throw new Error("Cannot include negative exponent part for integers");while(u.notEquals(0))i[0]+="0",u=u.prev()}i=i[0],i==="-0"&&(i="0");var a=/^([0-9][0-9]*)$/.test(i);if(!a)throw new Error("Invalid integer: "+i);while(i.length){var f=i.length>t?i.length-t:0;s.push(+i.slice(f)),i=i.slice(0,f)}return new r(s,o)}function m(e){var t=e.value;return t.length===1&&t[0]<=36?"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t[0]):"<"+t+">"}function g(e,t){t=bigInt(t);if(t.equals(0)){if(e.equals(0))return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(t.equals(-1))return e.equals(0)?"0":e.lesser(0)?Array(1-e).join("10"):"1"+Array(+e).join("01");var n="";e.isNegative()&&t.isPositive()&&(n="-",e=e.abs());if(t.equals(1))return e.equals(0)?"0":n+Array(+e+1).join(1);var r=[],i=e,s;while(i.lesser(0)||i.compareAbs(t)>=0){s=i.divmod(t),i=s.quotient;var o=s.remainder;o.lesser(0)&&(o=t.minus(o).abs(),i=i.next()),r.push(m(o))}return r.push(m(i)),n+r.reverse().join("")}var e=1e7,t=7,n={positive:!1,negative:!0};r.prototype.negate=function(){return new r(this.value,!this.sign)},r.prototype.abs=function(){return new r(this.value,n.positive)},r.prototype.add=function(t){t=d(t);if(this.sign!==t.sign)return this.sign===n.positive?this.abs().subtract(t.abs()):t.abs().subtract(this.abs());var s=this.value,o=t.value,u=[],a=0,f=Math.max(s.length,o.length);for(var l=0;l0;l++){var c=(s[l]||0)+(o[l]||0)+a;a=c>=e?1:0,c-=a*e,u.push(c)}return new r(i(u),this.sign)},r.prototype.plus=function(e){return this.add(e)},r.prototype.subtract=function(t){t=d(t);if(this.sign!==t.sign)return this.add(t.negate());if(this.sign===n.negative)return t.negate().subtract(this.negate());if(this.compare(t)<0)return t.subtract(this).negate();var s=this.value,o=t.value,u=[],a=0,f=Math.max(s.length,o.length);for(var l=0;l0;l++){var p=o[l],v=p?h*p+c:c;c=v>e?Math.floor(v/e):0,v-=c*e,a[f].push(v)}}var m=-1;for(var f=0;fm&&(m=g)}var y=[],c=0;for(var f=0;f0;f++){var b=c;for(var l=0;le?Math.floor(b/e):0,b-=c*e,y.push(b)}return new r(i(y),n)},r.prototype.times=function(e){return this.multiply(e)},r.prototype.divmod=function(e){e=d(e);var t=this.sign!==e.sign;if(this.equals(0))return{quotient:new r([0],n.positive),remainder:new r([0],n.positive)};if(e.equals(0))throw new Error("Cannot divide by zero");var s=this.value,o=e.value,u=[],a=[];for(var f=s.length-1;f>=0;f--){var c=[s[f]].concat(a),h=l(o,c);u.push(h.result),a=h.remainder}return u.reverse(),{quotient:new r(i(u),t),remainder:new r(i(a),this.sign)}},r.prototype.divide=function(e){return this.divmod(e).quotient},r.prototype.over=function(e){return this.divide(e)},r.prototype.mod=function(e){return this.divmod(e).remainder},r.prototype.remainder=function(e){return this.mod(e)},r.prototype.pow=function(e){e=d(e);var t=this,n=e,r=h;if(n.equals(c))return r;if(t.equals(c)||n.lesser(c))return c;for(;;){n.isOdd()&&(r=r.times(t)),n=n.divide(2);if(n.equals(c))break;t=t.times(t)}return r},r.prototype.modPow=function(e,t){e=d(e),t=d(t);if(t.equals(c))throw new Error("Cannot take modPow with modulus 0");var n=h,r=this.mod(t);if(r.equals(c))return c;while(e.greater(0))e.isOdd()&&(n=n.multiply(r).mod(t)),e=e.divide(2),r=r.square().mod(t);return n},r.prototype.square=function(){return this.multiply(this)},r.prototype.next=function(){return this.add(1)},r.prototype.prev=function(){return this.subtract(1)},r.prototype.compare=function(e){var t=this,r=d(e);if(t.value.length===1&&r.value.length===1&&t.value[0]===0&&r.value[0]===0)return 0;if(r.sign!==t.sign)return t.sign===n.positive?1:-1;var i=t.sign===n.positive?1:-1,s=t.value,o=r.value,u=Math.max(s.length,o.length)-1;for(var a=u;a>=0;a--){var f=s[a]||0,l=o[a]||0;if(f>l)return 1*i;if(l>f)return-1*i}return 0},r.prototype.compareTo=function(e){return this.compare(e)},r.prototype.compareAbs=function(e){return this.abs().compare(e.abs())},r.prototype.equals=function(e){return this.compare(e)===0},r.prototype.notEquals=function(e){return!this.equals(e)},r.prototype.lesser=function(e){return this.compare(e)<0},r.prototype.greater=function(e){return this.compare(e)>0},r.prototype.greaterOrEquals=function(e){return this.compare(e)>=0},r.prototype.lesserOrEquals=function(e){return this.compare(e)<=0},r.prototype.isPositive=function(){return this.sign===n.positive},r.prototype.isNegative=function(){return this.sign===n.negative},r.prototype.isEven=function(){return this.value[0]%2===0},r.prototype.isOdd=function(){return this.value[0]%2===1},r.prototype.isUnit=function(){return this.value.length===1&&this.value[0]===1},r.prototype.isDivisibleBy=function(e){return this.mod(e).equals(c)},r.prototype.isPrime=function(){var e=this.abs(),t=e.prev();if(e.isUnit())return!1;if(e.equals(2)||e.equals(3)||e.equals(5))return!0;if(e.isEven()||e.isDivisibleBy(3)||e.isDivisibleBy(5))return!1;if(e.lesser(25))return!0;var n=[2,3,5,7,11,13,17,19],r=t,i,s,o,u;while(r.isEven())r=r.divide(2);for(o=0;o");r.push(d(e.slice(n+1,i)))}}t=d(t);var n=c,r=[],i,s=!1;for(i=0;i