blob: 908d2423ad083ae1212802cdc39668d5b904e5d9 [file] [log] [blame]
var bigInt=function(e){"use strict";function o(e,t){return typeof e=="undefined"?o[0]:typeof t!="undefined"?+t===10?Y(e):$(e,t):Y(e)}function u(e,t){this.value=e,this.sign=t,this.isSmall=!1}function a(e){this.value=e,this.sign=e<0,this.isSmall=!0}function f(e){return-r<e&&e<r}function l(e){return e<1e7?[e]:e<1e14?[e%1e7,Math.floor(e/1e7)]:[e%1e7,Math.floor(e/1e7)%1e7,Math.floor(e/1e14)]}function c(e){h(e);var n=e.length;if(n<4&&_(e,i)<0)switch(n){case 0:return 0;case 1:return e[0];case 2:return e[0]+e[1]*t;default:return e[0]+(e[1]+e[2]*t)*t}return e}function h(e){var t=e.length;while(e[--t]===0);e.length=t+1}function p(e){var t=new Array(e),n=-1;while(++n<e)t[n]=0;return t}function d(e){return e>0?Math.floor(e):Math.ceil(e)}function v(e,n){var r=e.length,i=n.length,s=new Array(r),o=0,u=t,a,f;for(f=0;f<i;f++)a=e[f]+n[f]+o,o=a>=u?1:0,s[f]=a-o*u;while(f<r)a=e[f]+o,o=a===u?1:0,s[f++]=a-o*u;return o>0&&s.push(o),s}function m(e,t){return e.length>=t.length?v(e,t):v(t,e)}function g(e,n){var r=e.length,i=new Array(r),s=t,o,u;for(u=0;u<r;u++)o=e[u]-s+n,n=Math.floor(o/s),i[u]=o-n*s,n+=1;while(n>0)i[u++]=n%s,n=Math.floor(n/s);return i}function y(e,n){var r=e.length,i=n.length,s=new Array(r),o=0,u=t,a,f;for(a=0;a<i;a++)f=e[a]-o-n[a],f<0?(f+=u,o=1):o=0,s[a]=f;for(a=i;a<r;a++){f=e[a]-o;if(!(f<0)){s[a++]=f;break}f+=u,s[a]=f}for(;a<r;a++)s[a]=e[a];return h(s),s}function b(e,t,n){var r,i;return _(e,t)>=0?r=y(e,t):(r=y(t,e),n=!n),r=c(r),typeof r=="number"?(n&&(r=-r),new a(r)):new u(r,n)}function w(e,n,r){var i=e.length,s=new Array(i),o=-n,f=t,l,h;for(l=0;l<i;l++)h=e[l]+o,o=Math.floor(h/f),h%=f,s[l]=h<0?h+f:h;return s=c(s),typeof s=="number"?(r&&(s=-s),new a(s)):new u(s,r)}function E(e,n){var r=e.length,i=n.length,s=r+i,o=p(s),u=t,a,f,l,c,d;for(l=0;l<r;++l){c=e[l];for(var v=0;v<i;++v)d=n[v],a=c*d+o[l+v],f=Math.floor(a/u),o[l+v]=a-f*u,o[l+v+1]+=f}return h(o),o}function S(e,n){var r=e.length,i=new Array(r),s=t,o=0,u,a;for(a=0;a<r;a++)u=e[a]*n+o,o=Math.floor(u/s),i[a]=u-o*s;while(o>0)i[a++]=o%s,o=Math.floor(o/s);return i}function x(e,t){var n=[];while(t-->0)n.push(0);return n.concat(e)}function T(e,t){var n=Math.max(e.length,t.length);if(n<=30)return E(e,t);n=Math.ceil(n/2);var r=e.slice(n),i=e.slice(0,n),s=t.slice(n),o=t.slice(0,n),u=T(i,o),a=T(r,s),f=T(m(i,r),m(o,s)),l=m(m(u,x(y(y(f,u),a),n)),x(a,2*n));return h(l),l}function N(e,t){return-0.012*e-.012*t+15e-6*e*t>0}function C(e,n,r){return e<t?new u(S(n,e),r):new u(E(n,l(e)),r)}function k(e){var n=e.length,r=p(n+n),i=t,s,o,u,a,f;for(u=0;u<n;u++){a=e[u];for(var l=0;l<n;l++)f=e[l],s=a*f+r[u+l],o=Math.floor(s/i),r[u+l]=s-o*i,r[u+l+1]+=o}return h(r),r}function L(e,n){var r=e.length,i=n.length,s=t,o=p(n.length),u=n[i-1],a=Math.ceil(s/(2*u)),f=S(e,a),l=S(n,a),h,d,v,m,g,y,b;f.length<=r&&f.push(0),l.push(0),u=l[i-1];for(d=r-i;d>=0;d--){h=s-1,f[d+i]!==u&&(h=Math.floor((f[d+i]*s+f[d+i-1])/u)),v=0,m=0,y=l.length;for(g=0;g<y;g++)v+=h*l[g],b=Math.floor(v/s),m+=f[d+g]-(v-b*s),v=b,m<0?(f[d+g]=m+s,m=-1):(f[d+g]=m,m=0);while(m!==0){h-=1,v=0;for(g=0;g<y;g++)v+=f[d+g]-s+l[g],v<0?(f[d+g]=v+s,v=0):(f[d+g]=v,v=1);m+=v}o[d]=h}return f=O(f,a)[0],[c(o),c(f)]}function A(e,n){var r=e.length,i=n.length,s=[],o=[],u=t,a,f,l,h,p;while(r){o.unshift(e[--r]);if(_(o,n)<0){s.push(0);continue}f=o.length,l=o[f-1]*u+o[f-2],h=n[i-1]*u+n[i-2],f>i&&(l=(l+1)*u),a=Math.ceil(l/h);do{p=S(n,a);if(_(p,o)<=0)break;a--}while(a);s.push(a),o=y(o,p)}return s.reverse(),[c(s),c(o)]}function O(e,n){var r=e.length,i=p(r),s=t,o,u,a,f;a=0;for(o=r-1;o>=0;--o)f=a*s+e[o],u=d(f/n),a=f-u*n,i[o]=u|0;return[i,a|0]}function M(e,n){var r,i=Y(n),s=e.value,f=i.value,h;if(f===0)throw new Error("Cannot divide by zero");if(e.isSmall)return i.isSmall?[new a(d(s/f)),new a(s%f)]:[o[0],e];if(i.isSmall){if(f===1)return[e,o[0]];if(f==-1)return[e.negate(),o[0]];var p=Math.abs(f);if(p<t){r=O(s,p),h=c(r[0]);var v=r[1];return e.sign&&(v=-v),typeof h=="number"?(e.sign!==i.sign&&(h=-h),[new a(h),new a(v)]):[new u(h,e.sign!==i.sign),new a(v)]}f=l(p)}var m=_(s,f);if(m===-1)return[o[0],e];if(m===0)return[o[e.sign===i.sign?1:-1],o[0]];s.length+f.length<=200?r=L(s,f):r=A(s,f),h=r[0];var g=e.sign!==i.sign,y=r[1],b=e.sign;return typeof h=="number"?(g&&(h=-h),h=new a(h)):h=new u(h,g),typeof y=="number"?(b&&(y=-y),y=new a(y)):y=new u(y,b),[h,y]}function _(e,t){if(e.length!==t.length)return e.length>t.length?1:-1;for(var n=e.length-1;n>=0;n--)if(e[n]!==t[n])return e[n]>t[n]?1:-1;return 0}function D(e){var t=e.abs();if(t.isUnit())return!1;if(t.equals(2)||t.equals(3)||t.equals(5))return!0;if(t.isEven()||t.isDivisibleBy(3)||t.isDivisibleBy(5))return!1;if(t.lesser(25))return!0}function j(e){return(typeof e=="number"||typeof e=="string")&&+Math.abs(e)<=t||e instanceof u&&e.value.length<=1}function F(e,t,n){t=Y(t);var r=e.isNegative(),i=t.isNegative(),s=r?e.not():e,o=i?t.not():t,u=[],a=[],f=!1,l=!1;while(!f||!l)s.isZero()?(f=!0,u.push(r?1:0)):r?u.push(s.isEven()?1:0):u.push(s.isEven()?0:1),o.isZero()?(l=!0,a.push(i?1:0)):i?a.push(o.isEven()?1:0):a.push(o.isEven()?0:1),s=s.over(2),o=o.over(2);var c=[];for(var h=0;h<u.length;h++)c.push(n(u[h],a[h]));var p=bigInt(c.pop()).negate().times(bigInt(2).pow(c.length));while(c.length)p=p.add(bigInt(c.pop()).times(bigInt(2).pow(c.length)));return p}function R(e){var n=e.value,r=typeof n=="number"?n|I:n[0]+n[1]*t|q;return r&-r}function U(e,t){return e=Y(e),t=Y(t),e.greater(t)?e:t}function z(e,t){return e=Y(e),t=Y(t),e.lesser(t)?e:t}function W(e,t){e=Y(e).abs(),t=Y(t).abs();if(e.equals(t))return e;if(e.isZero())return t;if(t.isZero())return e;var n=o[1],r,i;while(e.isEven()&&t.isEven())r=Math.min(R(e),R(t)),e=e.divide(r),t=t.divide(r),n=n.multiply(r);while(e.isEven())e=e.divide(R(e));do{while(t.isEven())t=t.divide(R(t));e.greater(t)&&(i=t,t=e,e=i),t=t.subtract(e)}while(!t.isZero());return n.isUnit()?e:e.multiply(n)}function X(e,t){return e=Y(e).abs(),t=Y(t).abs(),e.divide(W(e,t)).multiply(t)}function V(e,n){e=Y(e),n=Y(n);var r=z(e,n),i=U(e,n),s=i.subtract(r);if(s.isSmall)return r.add(Math.round(Math.random()*s));var o=s.value.length-1,f=[],l=!0;for(var h=o;h>=0;h--){var p=l?s.value[h]:t,v=d(Math.random()*p);f.unshift(v),v<p&&(l=!1)}return f=c(f),r.add(typeof f=="number"?new a(f):new u(f,!1))}function J(e){var t=e.value;return typeof t=="number"&&(t=[t]),t.length===1&&t[0]<=35?"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t[0]):"<"+t+">"}function K(e,t){t=bigInt(t);if(t.isZero()){if(e.isZero())return"0";throw new Error("Cannot convert nonzero numbers to base 0.")}if(t.equals(-1))return e.isZero()?"0":e.isNegative()?(new Array(1-e)).join("10"):"1"+(new Array(+e)).join("01");var n="";e.isNegative()&&t.isPositive()&&(n="-",e=e.abs());if(t.equals(1))return e.isZero()?"0":n+(new Array(+e+1)).join(1);var r=[],i=e,s;while(i.isNegative()||i.compareAbs(t)>=0){s=i.divmod(t),i=s.quotient;var o=s.remainder;o.isNegative()&&(o=t.minus(o).abs(),i=i.next()),r.push(J(o))}return r.push(J(i)),n+r.reverse().join("")}function Q(e){if(f(+e)){var t=+e;if(t===d(t))return new a(t);throw"Invalid integer: "+e}var r=e[0]==="-";r&&(e=e.slice(1));var i=e.split(/e/i);if(i.length>2)throw new Error("Invalid integer: "+i.join("e"));if(i.length===2){var s=i[1];s[0]==="+"&&(s=s.slice(1)),s=+s;if(s!==d(s)||!f(s))throw new Error("Invalid integer: "+s+" is not a valid exponent.");var o=i[0],l=o.indexOf(".");l>=0&&(s-=o.length-l-1,o=o.slice(0,l)+o.slice(l+1));if(s<0)throw new Error("Cannot include negative exponent part for integers");o+=(new Array(s+1)).join("0"),e=o}var c=/^([0-9][0-9]*)$/.test(e);if(!c)throw new Error("Invalid integer: "+e);var p=[],v=e.length,m=n,g=v-m;while(v>0)p.push(+e.slice(g,v)),g-=m,g<0&&(g=0),v-=m;return h(p),new u(p,r)}function G(e){if(f(e)){if(e!==d(e))throw new Error(e+" is not an integer.");return new a(e)}return Q(e.toString())}function Y(e){return typeof e=="number"?G(e):typeof e=="string"?Q(e):e}var t=1e7,n=7,r=9007199254740992,i=l(r),s=Math.log(r);u.prototype=Object.create(o.prototype),a.prototype=Object.create(o.prototype),u.prototype.add=function(e){var t,n=Y(e);if(this.sign!==n.sign)return this.subtract(n.negate());var r=this.value,i=n.value;return n.isSmall?new u(g(r,Math.abs(i)),this.sign):new u(m(r,i),this.sign)},u.prototype.plus=u.prototype.add,a.prototype.add=function(e){var t=Y(e),n=this.value;if(n<0!==t.sign)return this.subtract(t.negate());var r=t.value;if(t.isSmall){if(f(n+r))return new a(n+r);r=l(Math.abs(r))}return new u(g(r,Math.abs(n)),n<0)},a.prototype.plus=a.prototype.add,u.prototype.subtract=function(e){var t=Y(e);if(this.sign!==t.sign)return this.add(t.negate());var n=this.value,r=t.value;return t.isSmall?w(n,Math.abs(r),this.sign):b(n,r,this.sign)},u.prototype.minus=u.prototype.subtract,a.prototype.subtract=function(e){var t=Y(e),n=this.value;if(n<0!==t.sign)return this.add(t.negate());var r=t.value;return t.isSmall?new a(n-r):w(r,Math.abs(n),n>=0)},a.prototype.minus=a.prototype.subtract,u.prototype.negate=function(){return new u(this.value,!this.sign)},a.prototype.negate=function(){var e=this.sign,t=new a(-this.value);return t.sign=!e,t},u.prototype.abs=function(){return new u(this.value,!1)},a.prototype.abs=function(){return new a(Math.abs(this.value))},u.prototype.multiply=function(e){var n,r=Y(e),i=this.value,s=r.value,a=this.sign!==r.sign,f;if(r.isSmall){if(s===0)return o[0];if(s===1)return this;if(s===-1)return this.negate();f=Math.abs(s);if(f<t)return new u(S(i,f),a);s=l(f)}return N(i.length,s.length)?new u(T(i,s),a):new u(E(i,s),a)},u.prototype.times=u.prototype.multiply,a.prototype._multiplyBySmall=function(e){return f(e.value*this.value)?new a(e.value*this.value):C(Math.abs(e.value),l(Math.abs(this.value)),this.sign!==e.sign)},u.prototype._multiplyBySmall=function(e){return e.value===0?o[0]:e.value===1?this:e.value===-1?this.negate():C(Math.abs(e.value),this.value,this.sign!==e.sign)},a.prototype.multiply=function(e){return Y(e)._multiplyBySmall(this)},a.prototype.times=a.prototype.multiply,u.prototype.square=function(){return new u(k(this.value),!1)},a.prototype.square=function(){var e=this.value*this.value;return f(e)?new a(e):new u(k(l(Math.abs(this.value))),!1)},u.prototype.divmod=function(e){var t=M(this,e);return{quotient:t[0],remainder:t[1]}},a.prototype.divmod=u.prototype.divmod,u.prototype.divide=function(e){return M(this,e)[0]},a.prototype.over=a.prototype.divide=u.prototype.over=u.prototype.divide,u.prototype.mod=function(e){return M(this,e)[1]},a.prototype.remainder=a.prototype.mod=u.prototype.remainder=u.prototype.mod,u.prototype.pow=function(e){var t=Y(e),n=this.value,r=t.value,i,s,u;if(r===0)return o[1];if(n===0)return o[0];if(n===1)return o[1];if(n===-1)return t.isEven()?o[1]:o[-1];if(t.sign)return o[0];if(!t.isSmall)throw new Error("The exponent "+t.toString()+" is too large.");if(this.isSmall&&f(i=Math.pow(n,r)))return new a(d(i));s=this,u=o[1];for(;;){r&!0&&(u=u.times(s),--r);if(r===0)break;r/=2,s=s.square()}return u},a.prototype.pow=u.prototype.pow,u.prototype.modPow=function(e,t){e=Y(e),t=Y(t);if(t.isZero())throw new Error("Cannot take modPow with modulus 0");var n=o[1],r=this.mod(t);while(e.isPositive()){if(r.isZero())return o[0];e.isOdd()&&(n=n.multiply(r).mod(t)),e=e.divide(2),r=r.square().mod(t)}return n},a.prototype.modPow=u.prototype.modPow,u.prototype.compareAbs=function(e){var t=Y(e),n=this.value,r=t.value;return t.isSmall?1:_(n,r)},a.prototype.compareAbs=function(e){var t=Y(e),n=Math.abs(this.value),r=t.value;return t.isSmall?(r=Math.abs(r),n===r?0:n>r?1:-1):-1},u.prototype.compare=function(e){if(e===Infinity)return-1;if(e===-Infinity)return 1;var t=Y(e),n=this.value,r=t.value;return this.sign!==t.sign?t.sign?1:-1:t.isSmall?this.sign?-1:1:_(n,r)*(this.sign?-1:1)},u.prototype.compareTo=u.prototype.compare,a.prototype.compare=function(e){if(e===Infinity)return-1;if(e===-Infinity)return 1;var t=Y(e),n=this.value,r=t.value;return t.isSmall?n==r?0:n>r?1:-1:n<0!==t.sign?n<0?-1:1:n<0?1:-1},a.prototype.compareTo=a.prototype.compare,u.prototype.equals=function(e){return this.compare(e)===0},a.prototype.eq=a.prototype.equals=u.prototype.eq=u.prototype.equals,u.prototype.notEquals=function(e){return this.compare(e)!==0},a.prototype.neq=a.prototype.notEquals=u.prototype.neq=u.prototype.notEquals,u.prototype.greater=function(e){return this.compare(e)>0},a.prototype.gt=a.prototype.greater=u.prototype.gt=u.prototype.greater,u.prototype.lesser=function(e){return this.compare(e)<0},a.prototype.lt=a.prototype.lesser=u.prototype.lt=u.prototype.lesser,u.prototype.greaterOrEquals=function(e){return this.compare(e)>=0},a.prototype.geq=a.prototype.greaterOrEquals=u.prototype.geq=u.prototype.greaterOrEquals,u.prototype.lesserOrEquals=function(e){return this.compare(e)<=0},a.prototype.leq=a.prototype.lesserOrEquals=u.prototype.leq=u.prototype.lesserOrEquals,u.prototype.isEven=function(){return(this.value[0]&1)===0},a.prototype.isEven=function(){return(this.value&1)===0},u.prototype.isOdd=function(){return(this.value[0]&1)===1},a.prototype.isOdd=function(){return(this.value&1)===1},u.prototype.isPositive=function(){return!this.sign},a.prototype.isPositive=function(){return this.value>0},u.prototype.isNegative=function(){return this.sign},a.prototype.isNegative=function(){return this.value<0},u.prototype.isUnit=function(){return!1},a.prototype.isUnit=function(){return Math.abs(this.value)===1},u.prototype.isZero=function(){return!1},a.prototype.isZero=function(){return this.value===0},u.prototype.isDivisibleBy=function(e){var t=Y(e),n=t.value;return n===0?!1:n===1?!0:n===2?this.isEven():this.mod(t).equals(o[0])},a.prototype.isDivisibleBy=u.prototype.isDivisibleBy,u.prototype.isPrime=function(){var t=D(this);if(t!==e)return t;var n=this.abs(),r=n.prev(),i=[2,3,5,7,11,13,17,19],s=r,u,a,f,l;while(s.isEven())s=s.divide(2);for(f=0;f<i.length;f++){l=bigInt(i[f]).modPow(s,n);if(l.equals(o[1])||l.equals(r))continue;for(a=!0,u=s;a&&u.lesser(r);u=u.multiply(2))l=l.square().mod(n),l.equals(r)&&(a=!1);if(a)return!1}return!0},a.prototype.isPrime=u.prototype.isPrime,u.prototype.isProbablePrime=function(t){var n=D(this);if(n!==e)return n;var r=this.abs(),i=t===e?5:t;for(var s=0;s<i;s++){var o=bigInt.randBetween(2,r.minus(2));if(!o.modPow(r.prev(),r).isUnit())return!1}return!0},a.prototype.isProbablePrime=u.prototype.isProbablePrime,u.prototype.modInv=function(e){var t=bigInt.zero,n=bigInt.one,r=Y(e),i=this.abs(),s,o,u;while(!i.equals(bigInt.zero))s=r.divide(i),o=t,u=r,t=n,r=i,n=o.subtract(s.multiply(n)),i=u.subtract(s.multiply(i));if(!r.equals(1))throw new Error(this.toString()+" and "+e.toString()+" are not co-prime");return t.compare(0)===-1&&(t=t.add(e)),t},a.prototype.modInv=u.prototype.modInv,u.prototype.next=function(){var e=this.value;return this.sign?w(e,1,this.sign):new u(g(e,1),this.sign)},a.prototype.next=function(){var e=this.value;return e+1<r?new a(e+1):new u(i,!1)},u.prototype.prev=function(){var e=this.value;return this.sign?new u(g(e,1),!0):w(e,1,this.sign)},a.prototype.prev=function(){var e=this.value;return e-1>-r?new a(e-1):new u(i,!0)};var P=[1];while(P[P.length-1]<=t)P.push(2*P[P.length-1]);var H=P.length,B=P[H-1];u.prototype.shiftLeft=function(e){if(!j(e))throw new Error(String(e)+" is too large for shifting.");e=+e;if(e<0)return this.shiftRight(-e);var t=this;while(e>=H)t=t.multiply(B),e-=H-1;return t.multiply(P[e])},a.prototype.shiftLeft=u.prototype.shiftLeft,u.prototype.shiftRight=function(e){var t;if(!j(e))throw new Error(String(e)+" is too large for shifting.");e=+e;if(e<0)return this.shiftLeft(-e);var n=this;while(e>=H){if(n.isZero())return n;t=M(n,B),n=t[1].isNegative()?t[0].prev():t[0],e-=H-1}return t=M(n,P[e]),t[1].isNegative()?t[0].prev():t[0]},a.prototype.shiftRight=u.prototype.shiftRight,u.prototype.not=function(){return this.negate().prev()},a.prototype.not=u.prototype.not,u.prototype.and=function(e){return F(this,e,function(e,t){return e&t})},a.prototype.and=u.prototype.and,u.prototype.or=function(e){return F(this,e,function(e,t){return e|t})},a.prototype.or=u.prototype.or,u.prototype.xor=function(e){return F(this,e,function(e,t){return e^t})},a.prototype.xor=u.prototype.xor;var I=1<<30,q=(t&-t)*(t&-t)|I,$=function(e,t){var n=o[0],r=o[1],i=e.length;if(2<=t&&t<=36&&i<=s/Math.log(t))return new a(parseInt(e,t));t=Y(t);var u=[],f,l=e[0]==="-";for(f=l?1:0;f<e.length;f++){var c=e[f].toLowerCase(),h=c.charCodeAt(0);if(48<=h&&h<=57)u.push(Y(c));else if(97<=h&&h<=122)u.push(Y(c.charCodeAt(0)-87));else{if(c!=="<")throw new Error(c+" is not a valid character");var p=f;do f++;while(e[f]!==">");u.push(Y(e.slice(p+1,f)))}}u.reverse();for(f=0;f<u.length;f++)n=n.add(u[f].times(r)),r=r.times(t);return l?n.negate():n};u.prototype.toString=function(t){t===e&&(t=10);if(t!==10)return K(this,t);var n=this.value,r=n.length,i=String(n[--r]),s="0000000",o;while(--r>=0)o=String(n[r]),i+=s.slice(o.length)+o;var u=this.sign?"-":"";return u+i},a.prototype.toString=function(t){return t===e&&(t=10),t!=10?K(this,t):String(this.value)},u.prototype.valueOf=function(){return+this.toString()},u.prototype.toJSNumber=u.prototype.valueOf,a.prototype.valueOf=function(){return this.value},a.prototype.toJSNumber=a.prototype.valueOf;for(var Z=0;Z<1e3;Z++)o[Z]=new a(Z),Z>0&&(o[-Z]=new a(-Z));return o.one=o[1],o.zero=o[0],o.minusOne=o[-1],o.max=U,o.min=z,o.gcd=W,o.lcm=X,o.isInstance=function(e){return e instanceof u||e instanceof a},o.randBetween=V,o}();typeof module!="undefined"&&module.hasOwnProperty("exports")&&(module.exports=bigInt);