From 45ca8ee3241dc25c090b3df536eb5262864daa4c Mon Sep 17 00:00:00 2001 From: Nikos M Date: Mon, 30 Jan 2017 18:26:58 +0200 Subject: [PATCH] v.0.8.2 * fibonacci, bell, catalan numbers computations * fix tuples counting method (used arguments in wrong order) * use additive recursive relation to compute binomial --- README.md | 2 +- src/js/Abacus.js | 62 +++++++++++++++++++++++++++++++++++++++----- src/js/Abacus.min.js | 6 ++--- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 86cf0dc..adc9dbc 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A combinatorics library for Node/XPCOM/JS, PHP, Python, C/C++, Java (php/python/java/c implementations in progress) -**version 0.8.1** (~ 49kB minified, ~ 16kB zipped) +**version 0.8.2** (~ 49kB minified, ~ 16kB zipped) ![abacus combinatorial numbers](/abacus.jpg) diff --git a/src/js/Abacus.js b/src/js/Abacus.js index 7069859..0d7e33e 100644 --- a/src/js/Abacus.js +++ b/src/js/Abacus.js @@ -2,7 +2,7 @@ * * Abacus * A combinatorics library for Node/XPCOM/JS, PHP, Python, Java, C/C++ -* @version: 0.8.1 +* @version: 0.8.2 * https://github.com/foo123/Abacus **/ !function( root, name, factory ){ @@ -22,7 +22,7 @@ else if ( !(name in root) ) /* Browser/WebWorker/.. */ /* module factory */ function ModuleFactory__Abacus( undef ){ "use strict"; -var Abacus = {VERSION: "0.8.1"}, stdMath = Math, PROTO = 'prototype', CLASS = 'constructor' +var Abacus = {VERSION: "0.8.2"}, stdMath = Math, PROTO = 'prototype', CLASS = 'constructor' ,slice = Array.prototype.slice, HAS = Object[PROTO].hasOwnProperty, toString = Object[PROTO].toString ,log2 = stdMath.log2 || function(x) { return stdMath.log(x) / stdMath.LN2; } ,trim_re = /^\s+|\s+$/g @@ -601,7 +601,8 @@ function factorial( n, m ) if ( null == m ) { // http://www.luschny.de/math/factorial/index.html - // simple factorial = n! = n (n-1)! + // https://en.wikipedia.org/wiki/Factorial + // simple factorial = F(n) = n F(n-1) = n! if ( 10 >= n ) return 0 > n ? O : (0 === n ? I : NUM(([1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800])[n-1])); key = String(n)/*+'!'*/; if ( null == factorial.mem1[key] ) @@ -611,7 +612,9 @@ function factorial( n, m ) } else if ( false === m ) { - // derangement sub-factorial !n = D(n) = n D(n-1) + (-1)^n = [(n!+1)/e] + // http://mathworld.wolfram.com/Subfactorial.html + // https://en.wikipedia.org/wiki/Derangement + // derangement sub-factorial D(n) = n D(n-1) + (-1)^n = !n = [(n!+1)/e] if ( 10 >= n ) return 2 > n ? O : NUM(([1, 2, 9, 44, 265, 1854, 14833, 133496, 1334961])[n-2]); key = '!'+String(n); if ( null == factorial.mem2[key] ) @@ -633,6 +636,7 @@ function factorial( n, m ) } else if ( is_array(m) ) { + // https://en.wikipedia.org/wiki/Multinomial_theorem // multinomial = n!/m1!..mk! if ( !m.length ) return 0 > n ? O : factorial(n); else if ( 0 > n ) return O; @@ -654,7 +658,8 @@ function factorial( n, m ) factorial.mem3[key] = operate(mul, I, null, n+m+1, n); return factorial.mem3[key]; } - // binomial = n!/m!(n-m)! + // https://en.wikipedia.org/wiki/Binomial_coefficient + // binomial = C(n,m) = C(n-1,m-1)+C(n-1,m) = n!/m!(n-m)! if ( m+m > n ) m = n-m; // take advantage of symmetry if ( (0 > m) || (1 > n) ) return O; else if ( (0 === m) || (1 === n) ) return I; @@ -664,7 +669,7 @@ function factorial( n, m ) factorial.mem3[key] = Arithmetic.isDefault() ? stdMath.round(operate(function(Cnm, i){ // this is faster and will not overflow unnecesarily for default arithmetic return Cnm*(1+n/i); - }, (n=n-m)+1, null, 2, m)) : div(factorial(n,-m), factorial(m)); + }, (n=n-m)+1, null, 2, m)) : add(factorial(n-1,m-1),factorial(n-1,m))/*div(factorial(n,-m), factorial(m))*/; return factorial.mem3[key]; } return O; @@ -768,6 +773,46 @@ function compositions( n, K /*exactly K parts or null*/, M /*max part is M or nu return compositions.mem[key]; } compositions.mem = {}; +function catalan( n ) +{ + var Arithmetic = Abacus.Arithmetic, O = Arithmetic.O, + NUM = Arithmetic.N, div = Arithmetic.div, mul = Arithmetic.mul, key; + // https://en.wikipedia.org/wiki/Catalan_number + // catalan numbers C(n) = (4n+2)C(n-1)/(n+1) + if ( 14 >= n ) return 0 > n ? O : NUM(([1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900])[n]); + key = String(n); + if ( null == catalan.mem[key] ) + /*catalan.mem[key] = operate(function(c,i){return add(c,mul(catalan(i),catalan(n-1-i)));},O,null,0,n-1,1);*/ + catalan.mem[key] = div(mul(catalan(n-1),4*n-2),n+1);/* n -> n-1 */ + return catalan.mem[key]; +} +catalan.mem = {}; +function bell( n ) +{ + var Arithmetic = Abacus.Arithmetic, O = Arithmetic.O, + NUM = Arithmetic.N, add = Arithmetic.add, mul = Arithmetic.mul, key; + // https://en.wikipedia.org/wiki/Bell_number + // bell numbers B(n) = SUM[k:0->n-1] ( C(n-1,k) B(k) ) + if ( 12 >= n ) return 0 > n ? O : NUM(([1,1,2,5,15,52,203,877,4140,21147,115975,678570])[n]); + key = String(n); + if ( null == bell.mem[key] ) + bell.mem[key] = operate(function(b,k){return add(b,mul(factorial(n-1,k),bell(k)));},O,null,0,n-1,1); + return bell.mem[key]; +} +bell.mem = {}; +function fibonacci( n ) +{ + var Arithmetic = Abacus.Arithmetic, O = Arithmetic.O, + NUM = Arithmetic.N, add = Arithmetic.add, key; + // http://en.wikipedia.org/wiki/Fibonacci_number + // fibonacci numbers F(n) = F(n-1) + F(n-2) + if ( 29 >= n ) return 0 > n ? O : NUM(([0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765, 10946,17711,28657,46368,75025,121393,196418,317811])[n]); + key = String(n); + if ( null == fibonacci.mem[key] ) + fibonacci.mem[key] = add(fibonacci(n-1),fibonacci(n-2)); + return fibonacci.mem[key]; +} +fibonacci.mem = {}; Abacus.Class = Class; @@ -861,6 +906,9 @@ Abacus.Math = { ,factorial: factorial ,partitions: partitions ,compositions: compositions +,bell: bell +,catalan: catalan +,fibonacci: fibonacci }; // pluggable arithmetics, eg biginteger Arithmetic @@ -2267,7 +2315,7 @@ Tensor = Abacus.Tensor = Class(CombinatorialIterator, { ,DUAL: CombinatorialIterator.DUAL ,count: function( n, $ ) { var O = Abacus.Arithmetic.O, type = $ && $.type ? $.type : "tensor"; - return "tuple"===type ? (!n || (0 >= n[0]) ? O : Abacus.Math.exp(n[0], n[1])) : (!n || !n.length ? O : Abacus.Math.product(n)); + return "tuple"===type ? (!n || (0 >= n[0]) ? O : Abacus.Math.exp(n[1], n[0])) : (!n || !n.length ? O : Abacus.Math.product(n)); } ,initial: function( n, $, dir ) { // some C-P-T dualities, symmetries & processes at play here diff --git a/src/js/Abacus.min.js b/src/js/Abacus.min.js index 5f95a90..58d8b0e 100644 --- a/src/js/Abacus.min.js +++ b/src/js/Abacus.min.js @@ -2,8 +2,8 @@ * * Abacus * A combinatorics library for Node/XPCOM/JS, PHP, Python, Java, C/C++ -* @version: 0.8.1 +* @version: 0.8.2 * https://github.com/foo123/Abacus **/ -!function(n,e,t){"use strict";"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"]?(n.$deps=n.$deps||{})&&(n.EXPORTED_SYMBOLS=[e])&&(n[e]=n.$deps[e]=t.call(n)):"object"==typeof module&&module.exports?(module.$deps=module.$deps||{})&&(module.exports=module.$deps[e]=t.call(n)):"undefined"!=typeof System&&"function"==typeof System.register&&"function"==typeof System["import"]?System.register(e,[],function(r){r(e,t.call(n))}):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(e)?define(e,["module"],function(e){return t.moduleUri=e.uri,t.call(n)}):e in n||(n[e]=t.call(n)||1)&&"function"==typeof define&&define.amd&&define(function(){return n[e]})}(this,"Abacus",function(n){"use strict";function e(){throw new Error("Method not implemented!")}function t(n){return n instanceof Array||"[object Array]"===Cn.call(n)}function r(n){return n instanceof String||"[object String]"===Cn.call(n)}function u(n){var e=n.toString(2),t=32-e.length;return t>0?new Array(t+1).join("0")+e:e}function i(n,e,r,u,i,l,o){var a,s,c,f,d,m,h,_,p=e,b=null,v=r&&t(r);if(v&&(null==u&&(u=0),null==i&&(i=r.length-1)),null==l&&(l=u>i?-1:1),0===l||v&&!r.length||0>=An.floor((i-u)/l)+1)return p;if(0>l?(_=(u-i)%-l,_&&(b=i),i+=_,m=i,h=u,f=-1,c=-(-l<<4)):(_=(i-u)%l,_&&(b=i),i-=_,m=u,h=i,f=1,c=l<<4),d=u+l*(15&An.floor((i-u)/l+1)),v){for(m=An.max(0,m),h=An.min(r.length-1,h),a=u;a>=m&&h>=a&&f*(d-a)>0;a+=l)p=n(p,r[a],a);for(s=d;s>=m&&h>=s;s+=c)a=s,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a);!0===o&&null!==b&&b>=0&&b=m&&h>=a&&f*(d-a)>0;a+=l)p=n(p,a,a);for(s=d;s>=m&&h>=s;s+=c)a=s,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a);!0===o&&null!==b&&(p=n(p,b,b))}return p}function l(n,e,r){var u=t(n)?n:(n=0|n)>0?new Array(n):[];if(n=u.length,n>0&&null!=e){r=r||0;var l=e;i("function"==typeof e?function(n,t,r){return n[r]=e(r),n}:e===+e?function(n,e,t){return n[t]=l,l+=r,n}:function(n,t,r){return n[r]=e,n},u,u)}return u}function o(n,e,t){return i(function(n,t,r){return n[r]=t[e],n},!0===t?n:new Array(n.length),n)}function a(n,e,r,u,l){return null==e?n:i(t(r)?function(n,e,t){return n[t]=r[t]-1-e,n}:function(n,e,t){return n[t]=r-1-e,n},n,e,u,l)}function s(n,e,t,r,u){if(null==e)return n;if(null==r&&(r=0),null==u&&(u=e.length-1),u>r)for(var i,l=r,o=u;o>l;l++,o--)i=e[l],n[l]=e[o],n[o]=i;return n}function c(n,e){return null==e&&(e=0),t(n)?l(n,t(e)?function(t){return e[t]-1-n[n.length-1-t]}:function(t){return e-n[t]}):n===+n&&e===+e?e-n:xn.Arithmetic.sub(xn.Arithmetic.N(e),n)}function f(n,e,r,u,l){if(null==e)return null;var o=0;return i(t(r)?function(n,e,t){return n[t]=r[t]>0?(e+o)%r[t]:0,o+=r[t]-n[t],n}:function(n,e,t){return n[t]=(e+o)%r,o+=r-n[t],n},n,e,u,l)}function d(n,e,t,r,u,l,o,a){if(null==e)return null;null==t&&(t=1),null==r&&(r=0),null==u&&(u=0),null==l&&(l=e.length-1),null==o&&(o=u),null==a&&(a=l);var s=0,c=o>a?-1:1,f=o;return i(function(n,e,u){return e=r+t*e,n[f]=e-s,s=e,f+=c,n},n,e,u,l)}function m(n,e,t,r,u,l,o,a){if(null==e)return null;null==t&&(t=1),null==r&&(r=0),null==u&&(u=0),null==l&&(l=e.length-1),null==o&&(o=u),null==a&&(a=l);var s=0,c=o>a?-1:1,f=o;return i(function(n,e,u){return s+=e,n[f]=r+t*s,f+=c,n},n,e,u,l)}function h(n,e,t,r,u,i,l,o){r=-1===r?-1:1,null==u&&(u=0),null==i&&(i=e.length-1),null==l&&(l=0),null==o&&(o=t.length-1);var a=u>i?-1:1,s=l>o?-1:1,c=a*(i-u)+1,f=s*(o-l)+1,d=u,m=l,h=0;if(null==n&&(n=new Array(An.min(c,f))),0===n.length)return n;for(;a*(i-d)>=0&&s*(o-m)>=0;)1===r&&e[d]t[m]?d+=a:1===r&&e[d]>t[m]||-1===r&&e[d]i?-1:1,c=o>a?-1:1,f=s*(i-u)+1,d=u,m=u,h=0;if(!t||!t.length)return e===+e?l(e,u,s):e?e.slice():e;if(null==n&&(n=new Array(f)),e===+e){for(;s*(i-d)>=0&&c*(a-m)>=0;)d===t[m]?(d+=s,m+=s):1===r&&d>t[m]||-1===r&&d=0;)n[h++]=d,d+=s}else{for(;s*(i-d)>=0&&c*(a-m)>=0;)e[d]===t[m]?(d+=s,m+=s):1===r&&e[d]>t[m]||-1===r&&e[d]=0;)n[h++]=e[d],d+=s}return hi?-1:1,d=l>o?-1:1,m=f*(i-u)+1,h=d*(o-l)+1,_=m+h,p=u,b=l,v=0,g=null,y=!s;if(null==n&&(n=new Array(_)),0===n.length)return c?e:n;for(;f*(i-p)>=0&&d*(o-b)>=0;){if(s&&v){if(e[p]===g){p+=f;continue}if(t[b]===g){b+=d;continue}}a?1===r&&e[p][0]t[b][0]?(n[v++]=g=e[p],p+=f):1===r&&e[p][0]>t[b][0]||-1===r&&e[p][0]t[b][1]?(n[v++]=g=e[p],y&&(n[v++]=t[b])):(n[v++]=g=t[b],y&&(n[v++]=e[p])),p+=f,b+=d):1===r&&e[p]t[b]?(n[v++]=g=e[p],p+=f):1===r&&e[p]>t[b]||-1===r&&e[p]=0;)(y||e[p]!==g)&&(n[v++]=g=e[p],p+=f);for(;d*(o-b)>=0;)(y||t[b]!==g)&&(n[v++]=g=t[b],b+=d);if(c){for(p=0>f?i:u,v=0;_>v;v++,p++)e[p]=n[v];return e}return vu?-1:1,a=l*(u-r)+1;if(t=!0===t,1>=a)return t?1===a?[r]:[]:n;e=-1===e?-1:1;var s=a,c=1,f=2,d=An.min,m=new Array(a);for(t&&(n=i(function(n,e,t){return n[t-r]=[e,t],n},new Array(a),n,r,u,1),r=0,u=a-1);s>0;)i(function(u,i){p(m,n,n,e,r+l*i,r+l*(i+c-1),r+l*(i+c),r+l*d(i+f-1,a-1),t,!1,!0)},null,null,0,a-c-1,f),c<<=1,f<<=1,s>>=1;return t?o(n,n,1):n}function v(n,e,r,u){var l,o=xn.Math.rndInt,a=!0===e?1:0;return t(r)?1<(l=r.length)&&i(function(n){if(aa;a++)o[a]=n[i+f(0,d)];return t&&b(o),o}for(a=0;e>a;a++)s=f(0,--d),c=n[i+s],n[i+s]=n[i+d],n[i+d]=c,o[a]=c,u&&(u[a]=s);if(u)for(a=e-1;a>=0;a--)s=u[a],c=n[i+d],n[i+d]=n[i+s],n[i+s]=c,d++;return t&&b(o),o}function y(n,e,t,r,u){t=-1===t?-1:1,null==r&&(r=0),null==u&&(u=e.length-1);var i,l,o=r,a=u;if(n===e[o])return o;if(n===e[a])return a;for(;a>o;){if(i=o+(a-o+1>>>1),l=e[i],n===l)return i;1===t&&l>n||-1===t&&n>l?a=i-1:o=i+1}return-1}function x(n,e){return n+e}function A(n,e){return n*e}function w(n,e,t,r){return i(xn.Arithmetic.add,xn.Arithmetic.O,n,e,t,r)}function M(n,e,t,r){return i(xn.Arithmetic.mul,xn.Arithmetic.I,n,e,t,r)}function I(n){var e=xn.Arithmetic;return e.shl(e.I,e.N(n))}function O(n,e){var t=xn.Arithmetic,r=t.N;return t.pow(r(n),r(e))}function C(n,e){var r,u=xn.Arithmetic,l=u.O,o=u.I,a=u.J,s=u.N,c=u.add,f=(u.sub,u.div),d=u.mul;return null==e?10>=n?0>n?l:0===n?o:s([1,2,6,24,120,720,5040,40320,362880,3628800][n-1]):(r=String(n),null==C.mem1[r]&&(C.mem1[r]=d(C(n-1),n)),C.mem1[r]):!1===e?10>=n?2>n?l:s([1,2,9,44,265,1854,14833,133496,1334961][n-2]):(r="!"+String(n),null==C.mem2[r]&&(C.mem2[r]=c(1&n?a:o,d(C(n-1,!1),n))),C.mem2[r]):!0===e?10>=n?0>n?l:0===n?o:s([1,2,4,10,26,76,232,764,2620,9496][n-1]):(r="I"+String(n),null==C.mem2[r]&&(C.mem2[r]=c(C(n-1,!0),d(C(n-2,!0),n-1))),C.mem2[r]):t(e)?e.length?0>n?l:(r=String(n)+"@"+e.join(","),null==C.mem3[r]&&(C.mem3[r]=f(C(n),i(function(n,e){return d(n,C(e))},C(e[e.length-1]),e,e.length-2,0))),C.mem3[r]):0>n?l:C(n):e===+e?0>e?-e>n?l:(r=String(n)+"@"+String(e),null==C.mem3[r]&&(C.mem3[r]=i(d,o,null,n+e+1,n)),C.mem3[r]):(e+e>n&&(e=n-e),0>e||1>n?l:0===e||1===n?o:1===e?s(n):(r=String(n)+"@"+String(e),null==C.mem3[r]&&(C.mem3[r]=u.isDefault()?An.round(i(function(e,t){return e*(1+n/t)},(n-=e)+1,null,2,e)):f(C(n,-e),C(e))),C.mem3[r])):l}function L(n,e,t,r){var u,l,o=xn.Arithmetic,a=o.add,s=o.O;return 0>n||0>=e||t>r||t+e>n+1||n>e*r?s:r===n&&1===e||e===n&&1===r?o.I:(u=String(n)+","+String(e)+","+String(t)+","+String(r),null==L.mem[u]&&(n-=e*(t-1),r=r-t+1,l=String(n)+","+String(e)+","+String(t)+","+String(r),null==L.mem[l]&&(L.mem[l]=i(function(t,u){return a(t,L(n-r,e-1,1,u))},s,null,An.max(1,An.ceil((n-r)/(e-1))),An.min(r,n-r-e+2),1)),L.mem[u]=L.mem[l]),L.mem[u])}function S(n,e,t){e=null==e?null:0|e,t=null==t?null:0|t;var r,u=xn.Arithmetic.add,l=xn.Arithmetic.O,o=t?t:0,a=t?t:1,s=e?e:1,c=e?e:n;return 0>n||e&&t&&(e+t>n+1||n>e*t)||t&&t>n||e&&e>n?l:(t&&!e&&(o=0,a=1,s=t,c=t,e=t,t=null),r=String(n)+"|"+String(e)+"|"+String(t),null==S.mem[r]&&(S.mem[r]=i(function(e,t){return i(function(e,r){return u(e,L(n,t,1,r))},e,null,a,o?o:n-t+1,1)},l,null,s,c,1)),S.mem[r])}function E(n,e,t,r){var u,l=xn.Arithmetic,o=l.add,a=l.O;return 0>n||0>=e||t>r||t+e>n+1||n>e*r?a:t===r?e*t===n?l.I:a:n===r?C(n-e*t+e-1,e-1):t+1===r?C(e,n-e*t):(u=String(n)+","+String(e)+","+String(t)+","+String(r),null==E.mem[u]&&(E.mem[u]=i(function(n,u){return o(n,E(u,e-1,t,r))},a,null,n-r,n-t,1)),E.mem[u])}function D(n,e,t){e=null==e?null:0|e,t=null==t?null:0|t;var r,u=xn.Arithmetic.O,l=xn.Arithmetic.add;return 0>n||e&&t&&(e+t>n+1||n>e*t)||t&&t>n||e&&e>n?u:(r=String(n)+"|"+String(e)+"|"+String(t),null==D.mem[r]&&(e&&t?D.mem[r]=E(n,e,1,t):e?D.mem[r]=E(n,e,1,n):t?D.mem[r]=i(function(e,r){return l(e,E(n,r,1,t))},u,null,An.ceil(n/t),n,1):D.mem[r]=n>=1?I(n-1):Arithmetic.I),D.mem[r])}function R(n){if(!arguments.length||null==n)return $n;if(r(n)){n=n.toUpperCase().split(",");var e,t=0,u=n.length;for(e=0;u>e;e++)t|=On.call(R,n[e])?R[n[e]]:0;return t>0&&!(t&(Xn|Tn))&&(t|=$n),t&Tn&&(t&=~Un),t>0?t:$n}return n&Tn&&(n&=~Un),jn&n?n:$n}function U(){var n,e,r,u,i,l,o,a,s,c,f=arguments,d=f.length;if(!d)return[];if(!0===f[0]){for(s=f[1].length,n=2;d>n;n++)s*=f[n].length;for(c=new Array(s),n=0;s>n;n++){for(a=0,l=1,r=n,e=1;d>e;e++)u=f[e].length,i=r%u,r=~~(r/u),o=f[e][i],a+=l*o,l*=u;c[n]=a}}else{for(s=f[0].length,n=1;d>n;n++)s*=f[n].length;for(c=new Array(s),n=0;s>n;n++){for(a=[],r=n,e=d-1;e>=0;e--)if(u=f[e].length,i=r%u,r=~~(r/u),o=f[e][i],t(o))for(l=o.length-1;l>=0;l--)a.unshift(o[l]);else a.unshift(o);c[n]=a}}return c}function k(n,e){if(0>e)return[];var t,r=l(e,0,0),u=n.length;for(e-=1,t=0;u>t;t++)r[e-n[t]]=1;return r}function $(n,e){e=An.min(e||n.length,n.length);var t,r=[];for(e-=1,t=0;e>=t;t++)0=u?o=e.slice():(o=N(_(null,u-1,P(e,a-1,r))),0<(u-=i(x,0,o))&&o.push(u),0>r&&s(o,o));else{var c,f,d,m,h,p=1,b=0,v=0,g="push";if(0>r&&(p=-p,b=a-1-b,g="unshift"),t(e[b]))for(h=e[b],o=[[h[1],h[0]]],c=0,d=1,m=p+b;a>d;d++,m+=p)h=e[m],f=0>r?0:c,h[1]===o[f][0]?o[f][1]+=h[0]:(o[g]([h[1],h[0]]),c++);else for(u=e[b],o=l(u,1,0),0>r&&(v=u-1-v),d=1,m=p+b;a>d;d++,m+=p)for(c=0,f=v,h=e[m];u>c&&h>0;)o[f]++,h--,c++,f+=p}return o}function X(n,e){if(null==n)return null;var t,r,u,i,l=[],o=n.length,a=-1===e,s=1,c=0,f="push";for(a&&(s=-s,c=o-1-c,f="unshift"),u=n[c],i=[u,1],t=1;o>t;t++)r=s*t+c,u===n[r]?i[1]++:(l[f](i),u=n[r],i=[u,1]);return l[f](i),l}function j(n,e){if(null==n)return null;var t,r,u,i,l,o=[],a=n.length,s=-1===e,c=1,f=0,d="push";for(s&&(c=-c,f=a-1-f,d="unshift"),t=0;a>t;t++)if(l=n[c*t+f],1===l[1])o[d](l[0]);else for(u=l[1],i=l[0],r=0;u>r;r++)o[d](i);return o}function q(n,e,t){var r=n?n.length:0,u=1,l=0,o=0,a=r>o?n[o]||1:1;return-1===t&&(u=-1,l=(r||e)-1),i(function(e,t){return 0>=a&&(o++,l+=u,a=r>o?n[o]||1:1),a--,e[t]=l,e},new Array(e),null,0,e-1)}function J(n,e,t){var r,u,i=e.length,l=i*i;for(n=n||new Array(l),r=0,u=0;l>r;)n[r+u]=0,++u>=i&&(u=0,r+=i);if(!0===t)for(r=0;i>r;r++)n[i*e[r]+r]=1;else for(r=0,u=0;i>u;u++,r+=i)n[r+e[r]]=1;return n}function V(n,e,t){var r,u,i=e.length,l=An.floor(An.sqrt(i));if(n=n||new Array(l),!0===t)for(r=0,u=0;l>r;)e[l*r+u]&&(n[u]=r),++u>=l&&(u=0,r++);else for(r=0,u=0;l>r;)e[r+u]&&(n[r]=u),++u>=l&&(u=0,r++);return n}function G(n){return F(null,b(n,1,!0))}function H(n,e){return e&&e.length?i(function(n,t,r){return n[r]=ti;i++)1&r&&(e-=s[r>>>1<<1]),s[r]+=1,r>>>=1;return s[r]+=1,n[t]=e,n},n||new Array(r),e)}function Y(n,e,t){var r,u,l,o=e.length,a=An.ceil(Ln(t||o)),s=1<=r;r++)for(l=1,u=1<=l;l++)f[u-1+l]=1<u;u++)f[r]-=1,r<<=1,e>=f[r]&&(e-=f[r],r++);return f[r]=0,n[t]=r-s,n},n||new Array(o),e)}function z(n,e,t){var r,u=n.length,i=null==e;if(u>1)for(i&&(e=new Array(u-1),t=0),r=u-1;r>=1;r--)e[t++]=[n[0],n[r]];else i&&(e=[]);return i?e:t}function K(n,e){var t,r,u,i,l=n.length,o=new Array(l),a=!0===e?1:0,s=new Array(l),c=0,f=0;for(t=0;l>t;t++)s[t]=0;for(u=new Array(l),i=0,r=c++,u[i++]=r,s[r]=1;l>c;){if(r=n[r],s[r])for(i>a&&(u.length=i,o[f++]=u),u=new Array(l),i=0;l>c&&s[r=c];)++c;s[r]||(u[i++]=r,s[r]=1)}return i>a&&(u.length=i,o[f++]=u),fe;e++)i=z(l[e],u,i);return it;t++)l[t]=t;for(t=0;i>t;t++)swap=r[t],u=l[r[0]],l[r[0]]=l[r[1]],l[r[1]]=u;return l}function Z(n){var e,t,r,u=n,i=u.length,l=i?u[0]:[],o=l.length;for(t=1;i>t;t++)for(r=l.slice(),e=0;o>e;e++)l[e]=r[u[t][e]];return l}function nn(n){var e,t,r,u,i,l,o=n,a=o.length,s=0;for(r=0;a>r;r++)s+=o[r].length;for(e=new Array(s),u=0,r=0;a>r;r++){for(i=o[r],l=i.length,t=0;l>t;t++)e[u+t]=u+i[t];u+=l}return e}function en(n,e){if(e=e||n.length,e!==n.length)return!1;var t,r,u=l(e,0,0);for(t=0;e>t;t++){if(r=n[t],0>r||r>=e||0t;t++)if(1!==u[t])return!1;return!0}function tn(n){for(var e=n.length,t=0;e>t;t++)if(n[t]!==t)return!1;return!0}function rn(n){for(var e=n.length,t=0,r=n[t];e>t;t++,r=n[t])if(0>r||r>=e||n[r]!==t)return!1;return!0}function un(n,e){if(e=e||1,1>e)return!1;var t,r,u,i=n.length;for(t=0;i>t;t++){for(r=n[t],u=1;e>=u&&t!==r;)u++,r=n[r];if(t!==r||u!==e&&(u>=e||e%u>0))return!1}return!0}function ln(n,e,t){e=0|e;for(var r=0,u=n.length,i=0;u>i;i++)if(n[i]===i&&r++,r>e)return!1;return!0===t?r===e:!0}function on(n){for(var e=n.length,t=1,r=n[0];e>t;t++)if(n[t]!==(r+t)%e)return!1;return!0}function an(n){for(var e=n.length-1,t=-1,r=0,u=n[r];e>r;r++,u=n[r])if(u>t&&(t=u),r>=t)return!1;return!0}function sn(n,e,t,r,u,i){var l,o,a,s,c,f,d,m,h,_,p=e;if("tuple"===r?(l=p[0],p=p[1]):l=p.length,h=0,_=l-1,f=1,s=_,c=h,d=1,m=0,Pn&u&&(f=-f,s=_-s,c=_-c,d=-d,m=_-m),kn&u&&(f=-f,s=_-s,c=_-c,d=-d,m=_-m),Un&u&&(t=-t),0>t)if("tuple"===r){for(o=s;o>=h&&_>=o&&0===n[o];)o-=f;if(o>=h&&_>=o)for(p-=1,n[o]=n[o]-1,a=o+f;a>=h&&_>=a;a+=f)n[a]=p;else n=null}else{for(o=s;o>=h&&_>=o&&0===n[o];)o-=f;if(o>=h&&_>=o)for(n[o]=n[o]-1,a=o+f;a>=h&&_>=a;a+=f)n[a]=p[d*a+m]-1;else n=null}else if("tuple"===r){for(o=s;o>=h&&_>=o&&n[o]+1===p;)o-=f;if(o>=h&&_>=o)for(n[o]=n[o]+1,a=o+f;a>=h&&_>=a;a+=f)n[a]=0;else n=null}else{for(o=s;o>=h&&_>=o&&n[o]+1===p[d*o+m];)o-=f;if(o>=h&&_>=o)for(n[o]=n[o]+1,a=o+f;a>=h&&_>=a;a+=f)n[a]=0;else n=null}return n}function cn(n,e,t,r,u,i,l){var o,a,s,c,f,d,m,h,_,p,b,v,g,y,x=e,A=null==i?x:i;if(g=0,y=x-1,h=1,m=y,_=1,p=0,b=1,v=0,Pn&u&&(h=-h,m=y-m,_=-_,p=A-1-p,t=-t),kn&u&&(h=-h,m=y-m,b=-1,v=A-1),Un&u&&(t=-t),0>t)if("cyclic"===r)if(o=y-m,_*n[o]+p>0)for(b=x-1,h=x+h,s=0;x>s;s++)f=(_*n[o]+p+b)%x,n[o]=_*f+p,o=(o+h)%x;else n=null;else if("involution"===r)n=null;else if("connected"===r)n=null;else do{for(d=!1,o=m-h;o>=g&&y>=o&&_*n[o]<=_*n[o+h];)o-=h;if(o>=g&&y>=o){for(a=m;a>=g&&y>=a&&h*(a-o)>0&&_*n[o]<=_*n[a];)a-=h;for(f=n[o],n[o]=n[a],n[a]=f,s=o+h,c=m;s>=g&&y>=s&&c>=g&&y>=c&&h*(c-s)>0;)f=n[s],n[s]=n[c],n[c]=f,d=d||b*s+v===n[s]||b*c+v===n[c],s+=h,c-=h;if("derangement"===r)for(a>=g&&y>=a&&(d=d||b*a+v===n[a]),c>=g&&y>=c&&(d=d||b*c+v===n[c]),d=d||b*o+v===n[o],s=o-h;!d&&s>=g&&y>=s;s-=h)d=b*s+v===n[s];else d=!1}else n=null}while(n&&d);else if("cyclic"===r)if(o=y-m,_*n[o]+ps;s++)f=(_*n[o]+p+b)%x,n[o]=_*f+p,o=(o+h)%x;else n=null;else if("involution"===r)n=null;else if("connected"===r)n=null;else do{for(d=!1,o=m-h;o>=g&&y>=o&&_*n[o]>=_*n[o+h];)o-=h;if(o>=g&&y>=o){for(a=m;a>=g&&y>=a&&h*(a-o)>0&&_*n[o]>=_*n[a];)a-=h;for(f=n[o],n[o]=n[a],n[a]=f,s=o+h,c=m;s>=g&&y>=s&&c>=g&&y>=c&&h*(c-s)>0;)f=n[s],n[s]=n[c],n[c]=f,d=d||b*s+v===n[s]||b*c+v===n[c],s+=h,c-=h;if("derangement"===r)for(a>=g&&y>=a&&(d=d||b*a+v===n[a]),c>=g&&y>=c&&(d=d||b*c+v===n[c]),d=d||b*o+v===n[o],s=o-h;!d&&s>=g&&y>=s;s-=h)d=b*s+v===n[s];else d=!1}else n=null}while(n&&d);return n}function fn(n,e,t,r,u){if(null==n)return null;var i,l=null;if("ordered"===u)for(l={},i=0;t>i;i++)l[n[i]]=1;return l}function dn(n,e,t,r,u,i){var l,o,a,s,c,f,d,m,h,_,p,b,v,g,y=e[1],x=e[0];if(d=0,m=y-1,f=1,c=m,h=1,_=0,p=1,b=0,Pn&u&&(h=-h,_=x-1-_,f=-f,c=m-c,p=-p,b=m-b),kn&u&&(f=-f,c=m-c,p=-p,b=m-b),Un&u&&(t=-t),0>t)if("ordered+repeated"===r){for(l=c;l>=d&&m>=l&&0===n[l];)l-=f;if(l>=d&&m>=l)for(x-=1,n[l]=n[l]-1,o=l+f;o>=d&&m>=o;o+=f)n[o]=x;else n=null}else if("ordered"===r){for(null==i&&(i=fn(n,x,y,u,r)),l=c,a=-1;-1===a&&l>=d&&m>=l;){if(h*n[l]+_-h>=0)for(o=h*n[l]+_-h;o>=0&&x>o;o-=h)if(s=h*o+_,null==i[s]){a=l,i[s]=1;break}i[n[l]]=null,l-=f}if(a>-1)for(n[a]=s,o=x-1-_,s=h*o+_,l=a+f;l>=d&&m>=l;l+=f){for(;o>=0&&x>o&&null!=i[s];)o-=h,s=h*o+_;n[l]=s,i[s]=1}else n=null}else if(g="repeated"===r,v=g?0:1,Pn&u)if(f=-f,c=m-c,p=-p,b=m-b,l=m-c,o=0>f?d:m,!g&&n[o]+1>y||g&&n[o]>0){if(g)for(;l>=d&&m>=l&&0===n[l];)l+=f;else for(;l>=d&&m>=l&&p*l+b===n[l];)l+=f;for(n[l]-=1,l-=f;l>=d&&m>=l;)n[l]=n[l+f]-v,l-=f}else n=null;else{for(a=-1,l=c;l-f>=d&&m>=l-f;l-=f)if(n[l]>n[l-f]+v){a=l;break}if(!(a>=d&&m>=a)&&0f?m:d]&&(a=0>f?m:d),a>=d&&m>=a){for(s=x-1+v,l=c;l>=d&&m>=l&&f*(l-a)>0;l-=f)s-=v,n[l]=s;n[a]--}else n=null}else if("ordered+repeated"===r){for(l=c;l>=d&&m>=l&&n[l]+1===x;)l-=f;if(l>=d&&m>=l)for(n[l]=n[l]+1,o=l+f;o>=d&&m>=o;o+=f)n[o]=0;else n=null}else if("ordered"===r){for(null==i&&(i=fn(n,x,y,u,r)),l=c,a=-1;-1===a&&l>=d&&m>=l;){if(h*n[l]+_+h=0&&x>o;o+=h)if(s=h*o+_,null==i[s]){a=l,i[s]=1;break}i[n[l]]=null,l-=f}if(a>-1)for(n[a]=s,o=_,s=h*o+_,l=a+f;l>=d&&m>=l;l+=f){for(;o>=0&&x>o&&null!=i[s];)o+=h,s=h*o+_;n[l]=s,i[s]=1}else n=null}else if(g="repeated"===r,v=g?0:1,Pn&u)if(f=-f,c=m-c,p=-p,b=m-b,l=m-c,!g&&n[l]+y=d&&m>=l+f&&n[l]+v===n[l+f];)n[l]=s,l+=f,s+=v;n[l]+=1}else n=null;else{if(g){for(a=-1,o=x-1,l=c;l>=d&&m>=l;l-=f)if(n[l]=d&&m>=l;l-=f)if(n[l]=d&&m>=a){if(s=n[a]+1,o=g?x-1:x-y+p*a+b,s===o)n[a]=s;else if(o>s)for(l=a;l>=d&&m>=l;l+=f)n[l]=s,s+=v}else n=null}return n}function mn(n,e,t,r,u,i,o){var a,s,c,f,d,m,h,_,p,b,v=e;if(p=0,b=n.length-1,_=1,c=p,f=b,kn&i&&(_=-_,c=b-c,f=b-f),Un&i&&(t=-t),0>t)if(r)n=null;else if(Pn&i)n=null;else if(s=u?c+_:c,s>=p&&b>=s&&n[s]>1){for(a=f,h=0;a>=p&&b>=a&&_*(a-s)>=0&&1===n[a];)h+=n[a],a-=_;m=n[a]-1,h++,n[a]=m,n=0>_?n.slice(a):n.slice(0,a+1),h>m?(s=h%m,n=0>_?(s?[s]:[]).concat(l(An.floor(h/m),m)).concat(n):n.concat(l(An.floor(h/m),m)).concat(s?[s]:[])):h>0&&(n=0>_?[h].concat(n):n.concat([h]))}else n=null;else if(r)n=null;else if(Pn&i)n=null;else if(u?(m=An.min(u,v-u),d=An.floor(v/u)+(v%u?1:0)-1,m=b>d||n[c+(d-1)*_]=p&&b>=s&&m){for(b>0?(a=f-_,h=n[f]):(a=f,h=0);a>=p&&b>=a&&a-_>=p&&b>=a-_&&_*(a-s)>0&&n[a]===n[a-_];)h+=n[a],a-=_;n[a]++,h--,n=h>0?0>_?l(h,1).concat(n.slice(a)):n.slice(0,a+1).concat(l(h,1)):0>_?n.slice(a):n.slice(0,a+1)}else n=null;return n}function hn(n,e,t,r,u,i,o){var a,s,c,f,d,m,h,_,p=e;if(h=0,_=n.length-1,m=1,s=h,c=_,kn&i&&(m=-m,s=_-s,c=_-c),Un&i&&(t=-t),0>t)if(r)if(u)n=null;else if(Pn&i)if(f=n[s],p-r+1>f){for(n[s]=1,a=s+m;a>=h&&_>=a&&1===n[a];)a+=m;n[a]--,a-m>=h&&_>=a-m&&(n[a-m]=1+f)}else n=null;else if(f=n[c],p-r+1>f){for(n[c]=1,a=c;a>=h&&_>=a&&1===n[a];)a-=m;n[a]--,a+m>=h&&_>=a+m&&(n[a+m]=1+f)}else n=null;else if(u)n=null;else if(Pn&i)n=null;else if(p>n.length){for(a=c,d=0;a>=h&&_>=a&&1===n[a];)a-=m,d++;f=n[a]-1,n[a]=f,d++,d>0&&(a+m>=h&&_>=a+m?(a+=m,n[a]=d,d=0,n=0>m?n.slice(a):n.slice(0,a+1)):n=0>m?l(d,1,0).concat(n):n.concat(l(d,1,0)))}else n=null;else if(r)if(u)n=null;else if(Pn&i)if(p-r+1>n[c]){for(a=s;a>=h&&_>=a&&1===n[a];)a+=m;f=n[a],n[a]=1,n[s]=f-1,a+m>=h&&_>=a+m&&n[a+m]++}else n=null;else if(p-r+1>n[s]){for(a=c;a>=h&&_>=a&&1===n[a];)a-=m;f=n[a],n[a]=1,n[c]=f-1,a-m>=h&&_>=a-m&&n[a-m]++}else n=null;else u?n=null:Pn&i?n=null:p>n[s]?(d=n[c],n[c-m]++,0>m?(n.shift(),n.unshift.apply(n,l(d-1,1,0))):(n.pop(),n.push.apply(n,l(d-1,1,0)))):n=null;return n}var _n,pn,bn,vn,gn,yn,xn={VERSION:"0.8.1"},An=Math,wn="prototype",Mn="constructor",In=Array.prototype.slice,On=Object[wn].hasOwnProperty,Cn=Object[wn].toString,Ln=An.log2||function(n){return An.log(n)/An.LN2},Sn=/^\s+|\s+$/g,En=(String.prototype.trim?function(n){return n.trim()}:function(n){return n.replace(Sn,"")},Object.create),Dn=function(n,e){for(var t in e)On.call(e,t)&&(n[t]=e[t]);return n},Rn=function(n,e){1===arguments.length&&(e=n,n=Object);var t=e[Mn];return On.call(e,"__static__")&&(t=Dn(t,e.__static__),delete e.__static__),t[wn]=Dn(En(n[wn]),e),t},Un=1,kn=2,$n=4,Pn=8,Nn=16,Tn=32,Xn=$n|Pn|Nn,jn=Xn|Tn|Un|kn;return C.mem1={},C.mem2={},C.mem3={},L.mem={},S.mem={},E.mem={},D.mem={},xn.Class=Rn,xn.Options={MAXMEM:1e6,RANDOM:"index"},R.LEX=R.LEXICOGRAPHIC=$n,R.COLEX=R.COLEXICOGRAPHIC=Pn,R.MINIMAL=R.GRAY=Nn,R.RANDOM=Tn,R.REV=R.ANTI=R.REVERSE=R.REVERSED=Un,R.REF=R.REFLECT=R.REFLECTED=kn,R.REVLEX=R.ANTILEX=R.REVERSELEXICOGRAPHIC=R.ANTILEXICOGRAPHIC=$n|Un,R.REFLEX=R.REFLECTEDLEXICOGRAPHIC=$n|kn,R.REVCOLEX=R.ANTICOLEX=R.REVERSECOLEXICOGRAPHIC=R.ANTICOLEXICOGRAPHIC=Pn|Un,R.REFCOLEX=R.REFLECTEDCOLEXICOGRAPHIC=Pn|kn,xn.ORDER=R,xn.Math={O:0,I:1,J:-1,N:function(n){return xn.Arithmetic.add(xn.Arithmetic.O,n)},V:function(n){return xn.Arithmetic.sub(xn.Arithmetic.O,n)},rnd:An.random,rndInt:function(n,e){return xn.Math.round((e-n)*xn.Math.rnd()+n)},equ:function(n,e){return n===e},gte:function(n,e){return n>=e},lte:function(n,e){return e>=n},gt:function(n,e){return n>e},lt:function(n,e){return e>n},inside:function(n,e,t,r){return r?n>=e&&t>=n:n>e&&t>n},clamp:function(n,e,t){return e>n?e:n>t?t:n},wrap:function(n,e,t){return e>n?t:n>t?e:n},wrapR:function(n,e){return 0>n?n+e:n},add:x,sub:function(n,e){return n-e},mul:A,div:function(n,e){return xn.Math.floor(n/e)},mod:function(n,e){return n%e},pow:An.pow,shl:function(n,e){return n<>e},bor:function(n,e){return n|e},band:function(n,e){return n&e},xor:function(n,e){return n^e},abs:An.abs,min:An.min,max:An.max,floor:An.floor,ceil:An.ceil,round:An.round,num:function(n){return"number"==typeof n?xn.Math.floor(n):parseInt(n,10)},val:function(n){return xn.Math.floor(n.valueOf())},sum:w,product:M,pow2:I,exp:O,factorial:C,partitions:S,compositions:D},xn.Arithmetic={isDefault:function(){return 0===xn.Arithmetic.O&&xn.Arithmetic.add===x},O:0,I:1,J:-1,N:xn.Math.N,V:xn.Math.V,equ:xn.Math.equ,gte:xn.Math.gte,lte:xn.Math.lte,gt:xn.Math.gt,lt:xn.Math.lt,inside:xn.Math.inside,clamp:xn.Math.clamp,wrap:xn.Math.wrap,wrapR:xn.Math.wrapR,add:xn.Math.add,sub:xn.Math.sub,mul:xn.Math.mul,div:xn.Math.div,mod:xn.Math.mod,pow:xn.Math.pow,shl:xn.Math.shl,shr:xn.Math.shr,bor:xn.Math.bor,band:xn.Math.band,xor:xn.Math.xor,abs:xn.Math.abs,min:xn.Math.min,max:xn.Math.max,floor:xn.Math.floor,ceil:xn.Math.ceil,round:xn.Math.round,rnd:xn.Math.rndInt,num:xn.Math.num,val:xn.Math.val},xn.Util={array:l,operate:i,intersection:h,difference:_,union:p,search:y,complementation:a,reflection:s,reversion:c,gray:f,finitedifference:d,partialsum:m,sort:b,shuffle:v,pick:g,pluck:o},xn.BitArray=Rn({constructor:function qn(n){var e=this;return e instanceof qn?(e.length=n,void(e.bits=new Uint32Array(An.ceil(n/32)))):new qn(n)},length:0,bits:null,dispose:function(){var n=this;return n.length=null,n.bits=null,n},clone:function(){var n=this,e=new xn.BitArray(n.length);return e.bits=new Uint32Array(n.bits),e},fromArray:function(n){var e=this;return e.bits=new Uint32Array(n),e},toArray:function(){return In.call(this.bits)},toString:function(){var n,e,t=this.toArray();for(n=0,e=t.length;e>n;n++)t[n]=u(t[n]);return t.join("")},reset:function(){var n,e=this,t=e.bits,r=t.length;for(n=0;r>n;n++)t[n]=0;return e},isset:function(n){return!!(this.bits[n>>>5]&1<<(31&n))},set:function(n){var e=this;return e.bits[n>>>5]|=1<<(31&n),e},unset:function(n){var e=this;return e.bits[n>>>5]&=~(1<<(31&n)),e},toggle:function(n){var e=this;return e.bits[n>>>5]^=1<<(31&n),e}}),_n=xn.CombinatorialIterator=Rn({constructor:function Jn(n,e,t){var r=this,u=r[Mn],i=xn.Arithmetic;r.name=n||"CombinatorialIterator",r.n=e||0,r.$=t=t||{},t.type=String(t.type||"default").toLowerCase(),t.order=t.order||$n,t.base=t.base||0,t.minbase=null!=t.minbase?t.minbase:t.base,t.maxbase=null!=t.maxbase?t.maxbase:t.base,t.dimension=t.dimension||0,t.mindimension=null!=t.mindimension?t.mindimension:t.dimension,t.maxdimension=null!=t.maxdimension?t.maxdimension:t.dimension,t.rand=t.rand||{},t.count=u.count(r.n,r.$),t.first=i.O,t.last=i.gt(t.count,i.O)?i.sub(t.count,i.I):i.J,t.sub instanceof Jn&&(t.subcount=xn.Arithmetic.mul(t.count,t.sub.total()),t.submethod=String(t.submethod||"project").toLowerCase(),t.subcascade=-1===t.subcascade?-1:1,"concatenate"===t.submethod?t.subdimension=t.dimension+t.sub.dimension():"compose"===t.submethod?t.subdimension=t.dimension*t.sub.dimension():t.subdimension=t.dimension),t.instance=r,r.order(t.order)},__static__:{Iterable:function Vn(n,e){var t=this;return t instanceof Vn?(e=-1===e?-1:1,void(t.next=function(){return n.hasNext(e)?{value:n.next(e)}:{done:!0}})):new Vn(n,e)},C:function(n,e){return a(n,n,e)},P:function(n){return s(n,n)},T:function(n,e){return c(n,e)},DUAL:function(n,e,t){if(null==n)return null;var r=this,u=r.C,i=r.P,l=(r.T,t&&null!=t.base?t.base:e),o=t&&null!=t.order?t.order:$n;return n=Tn&o?kn&o?i(n):n:Nn&o?kn&o?i(n):n:Pn&o?kn&o?u(n,l,t):i(u(n,l,t)):kn&o?i(n):n},count:e,initial:e,succ:function(n,e,t,r,u,i){if(null==t||null==n||null==e)return null;var l=this,o=xn.Arithmetic;r&&null!=r.order?r.order:$n;return u=-1===u?-1:1,l.unrank(o.add(e,0>u?o.J:o.I),t,r)},rand:function(n,e){var t,r=this,u=xn.Arithmetic,i=e&&null!=e.last?e.last:u.sub(r.count(n,e),u.I),l=u.O,o=u.rnd(l,i);return t=u.equ(l,o)?r.initial(n,e,1):u.equ(i,o)?r.initial(n,e,-1):r.unrank(o,n,e)},rank:e,unrank:e,cascade:function(n,e,t,r){if(-1===r){var u=n;n=e,e=u}return"concatenate"===t?n&&e?n.concat(e):n||e||null:"compose"===t?n&&e?U(!0,n,e):n||e||null:null==n||null==e?null:l(n.length,function(t){return n[t]t?(u.__item=i.rand(o,l),u.__index=s):(u._traversed&&u._traversed.dispose(),u._traversed=new xn.BitArray(a.val(t)),e=u.random("index"),u._traversed.set(+e),u.__item=i.unrank(e,o,l),null!=u.__item&&(u.__index=e)),u._index=s):(u.__item=i.initial(o,l,n),null!=u.__item&&(u.__index=0>n?r:s,u._update()),u._index=u.__index),u._item=i.output(u.__item,u.__index,o,l),u._prev=Tn&c||n>0?!1:null!=u.__item,u._next=0>n&&!(Tn&c)?!1:null!=u.__item,u},_update:function(){return this.item__=null,this},order:function(n,e){if(!arguments.length)return this._order;var t,u,i,l,o,a=this,s=a[Mn],c=xn.Arithmetic,f=(c.O,c.I,!0===n);return e=-1===e,i=a.n,l=a.$,a._traversed&&(a._traversed.dispose(),a._traversed=null),f?n=l.order:r(n)&&-1<(u=n.indexOf("|"))?(t=n.substr(u+1),n=R(n.substr(0,u))):t=n=R(n),o=e?-1:1,l.order=n,l.sub?(f?l.sub.rewind(o):l.sub.order(t,o),a.__subindex=l.sub.index(),a.__subitem=l.sub.next(o),a._subindex=null,a._subitem=null):(a.__subindex=null,a.__subitem=null,a._subindex=null,a._subitem=null),a._init(o),l.sub&&(a._prev=a._prev&&null!=a.__subitem,a._next=a._next&&null!=a.__subitem,a._subindex=c.add(c.mul(a.__subindex,l.count),a._index),a._subitem=s.cascade(a._item,a.__subitem,l.submethod,l.subcascade)),a},index:function(n,e){if(e=!0===e,!arguments.length)return this.$.sub?this._subindex:this._index;var t,r=this,u=r[Mn],i=xn.Arithmetic,l=i.O,o=(i.I,i.J),a=r.n,s=r.$,c=s.sub&&!e?s.subcount:s.count,f=s.sub&&!e?r._subindex:r._index,d=s.order;return n=i.wrapR(i.N(n),c),!i.equ(n,f)&&i.inside(n,o,c)&&(c=s.count,t=s.last,s.sub&&!e&&(s.sub.index(i.div(n,c)),r.__subindex=s.sub.index(),r.__subitem=s.sub.item(),n=i.mod(n,c)),Tn&d||(r.__index=n,r._index=n,r.__item=i.equ(l,n)?u.initial(a,s,1):i.equ(t,n)?u.initial(a,s,-1):u.unrank(n,a,s),r._update(),r._item=u.output(r.__item,r.__index,a,s),r._prev=null!=r.__item,r._next=null!=r.__item),s.sub&&(r._prev=r._prev&&null!=r.__subitem,r._next=r._next&&null!=r.__subitem,r._subindex=i.add(i.mul(r.__subindex,c),r._index),r._subitem=u.cascade(r._item,r.__subitem,s.submethod,s.subcascade))),r},item:function(n,e){if(!arguments.length)return this.$.sub?this._subitem:this._item;var u,i,l,o,a,s,c=this,f=c[Mn],d=c.n,m=c.$,h=m.sub?m.subcount:m.count,_=m.sub?c._subindex:c._index,p=xn.Arithmetic,b=p.O,v=(p.I,p.J),g=null;return r(e)?-1<(s=e.indexOf("|"))?(g=e.substr(s+1),e=R(e.substr(0,s))):g=e=R(e):null!=e?g=e=R(e):(e=m.order,g=null),m.sub||(g=null),t(n)?(h=m.count,u=m.last,l=m.order,m.order=e,c.__item=n.slice(),c._update(),c.__index=f.rank(c.__item,d,m),c._item=f.output(c.__item,c.__index,d,m),Tn&e?c._index=c.__index:c._index=c.__index,c._prev=null!=c.__item,c._next=null!=c.__item,c):(n=p.wrapR(p.N(n),h),e===m.order&&null===g&&p.equ(n,_)?m.sub?c._subitem:c._item:p.inside(n,v,h)?(a=null,h=m.count,u=m.last,m.sub&&(a=m.sub.item(p.div(n,h),g),n=p.mod(n,h)),Tn&e?(i=null,l=m.order,m.order=e,o=f.output(f.rand(d,m),i,d,m),m.order=l,m.sub&&(o=f.cascade(o,a,m.submethod,m.subcascade)),o):(i=n,l=m.order,m.order=e,o=f.output(p.equ(b,n)?f.initial(d,m,1):p.equ(u,n)?f.initial(d,m,-1):f.unrank(i,d,m),i,d,m),m.order=l,m.sub&&(o=f.cascade(o,a,m.submethod,m.subcascade)),o)):null)},random:function(n,e,t,r){var u,i=this,l=i[Mn],o=i.$,a=o.order;if(r=!0===r,"index"===n){var s,c,f=xn.Arithmetic,d=f.N,m=f.O,h=f.I;return e===!!e&&(r=e,e=null, -t=null),o.sub&&!r?(s=o.subcount,c=f.sub(s,h)):(s=o.count,c=o.last),null==e&&null==t?(e=m,t=c):null==t?(e=d(e||0),t=c):(e=d(e),t=d(t)),f.rnd(e,t)}return o.order|=Tn,u=l.rand(i.n,o),o.order=a,u=l.output(u,null,i.n,o),o.sub&&!r?l.cascade(u,o.sub.random(),o.submethod,o.subcascade):u},rewind:function(n){var e=this;return e.order(!0,-1===n?-1:1)},hasNext:function(n){var e=this;return-1===n?Tn&e.$.order?!1:e._prev:e._next},next:function(n){var e,t,r,u,i,l,o=this,a=o[Mn],s=xn.Arithmetic,c=s.O,f=s.I,d=s.J,m=o.n,h=o.$,_=h.order,p=h.count,b=h.sub?o._subitem:o._item,v=null!=b;if(n=-1===n?-1:1,0>n&&Tn&_)return null;if(e=0>n?d:f,Tn&_)if(u=h.last,s.lt(o._index,u))if(t=o._traversed){for(r=o.random("index"),i=xn.Math.rnd()>.5?d:f;t.isset(+r);)r=s.wrap(s.add(r,i),c,u);t.set(+r),o.__item=a.unrank(r,m,h),null!=o.__item&&(o.__index=r)}else o.__item=a.rand(m,h),o.__index=null;else o._item=o.__item=null,o._traversed&&(o._traversed.dispose(),o._traversed=null);else o.__item=a.succ(o.__item,o.__index,m,h,n,o.item__),null!=o.__item&&(o.__index=s.add(o.__index,e));return l=null!=o.__item,l?(o._index=s.add(o._index,e),null===o.__index&&(o.__index=o._index),0>n?(o._prev=l,o._next=v):(o._prev=v,o._next=l)):h.sub&&h.sub.hasNext(n)?(o.__subindex=h.sub.index(),o.__subitem=h.sub.next(n),o._init(n),l=null!=o.__item):(o.__subindex=null,o.__subitem=null,0>n?(o._prev=l,o._next=v):(o._prev=v,o._next=l)),o._item=a.output(o.__item,o.__index,m,h),h.sub&&(l=l&&null!=o.__subitem,o._subindex=l?s.add(s.mul(o.__subindex,p),o._index):null,o._subitem=l?a.cascade(o._item,o.__subitem,h.submethod,h.subcascade):null,0>n?o._prev=l:o._next=l),b},range:function(n,e){var t,r,u,l,o=this,a=xn.Arithmetic,s=a.N,c=a.O,f=a.I,d=o.$,m=d.sub?d.subcount:d.count,h=d.sub?a.sub(m,f):d.last,_=1,p=arguments.length,b=!(Tn&d.order);return 1>p?(n=c,e=h):2>p?(n=s(n),e=h):(n=s(n),e=s(e)),n=a.wrapR(n,m),e=a.wrapR(e,m),a.gt(n,e)&&(t=n,n=e,e=t,_=-1),n=a.clamp(n,c,h),b&&(e=a.clamp(e,c,h)),a.lte(n,e)?(l=[o.$.order,o.__index,o._index,o.__item&&o.__item.slice(),o._item,o.__subindex,o._subindex,o.__subitem,o._subitem,o._prev,o._next],b&&o.index(n),u=a.val(a.sub(e,n)),r=i(function(n,e,t){return n[t]=o.next(),n},new Array(u+1),null,0>_?u:0,0>_?0:u,0>_?-1:1),o.$.order=l[0],o.__index=l[1],o._index=l[2],o.__item=l[3],o._item=l[4],o.__subindex=l[5],o._subindex=l[6],o.__subitem=l[7],o._subitem=l[8],o._prev=l[9],o._next=l[10],o._update()):r=[],r},__iter__:function(){return new _n.Iterable(this)}}),"undefined"!=typeof Symbol&&"undefined"!=typeof Symbol.iterator&&(_n[wn][Symbol.iterator]=_n[wn].__iter__),pn=xn.Tensor=Rn(_n,{constructor:function Gn(){var n,e=this,r=In.call(arguments);if(n=!r.length||r[r.length-1]instanceof _n||t(r[r.length-1])||r[r.length-1]===+r[r.length-1]?{}:r.pop()||{},n.type=String(n.type||"tensor").toLowerCase(),n.order=n.order||$n,r.length&&t(r[0])&&(r=r[0]),r&&r.length||(r=[]),!(e instanceof Gn))return new Gn(r,n);if("tuple"===n.type)r[0]=r[0]||1,r[1]=r[1]||1,r[0]instanceof _n?(n.sub=r[0],r[0]=n.sub.dimension()):r[1]instanceof _n&&(n.sub=r[1],r[1]=n.sub.base()),n.base=r[1],n.dimension=r[0];else{var u=i(function(n,e){return en[1]&&(n[1]=e),n},[1/0,0],r);n.base=r,n.minbase=u[0],n.maxbase=u[1],n.dimension=r.length}_n.call(e,"Tensor",r,n)},__static__:{C:_n.C,P:_n.P,T:_n.T,DUAL:_n.DUAL,count:function(n,e){var t=xn.Arithmetic.O,r=e&&e.type?e.type:"tensor";return"tuple"===r?!n||0>=n[0]?t:xn.Math.exp(n[0],n[1]):n&&n.length?xn.Math.product(n):t},initial:function(n,e,t){var r,u=this,i=e&&e.type?e.type:"tensor",o=e&&e.order?e.order:$n;return t=-1===t?-1:1,($n&o&&Un&o||Pn&o&&!(Un&o))&&(t=-t),r="tuple"===i?n[0]?0>t?l(n[0],n[1]-1,0):l(n[0],0,0):[]:n.length?0>t?l(n.length,function(e){return n[e]-1}):l(n.length,0,0):[],r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,i){return!t||!t[0]||0>=t[0]||null==n?null:(u=-1===u?-1:1,sn(n,t,u,r&&r.type?r.type:"tensor",r&&null!=r.order?r.order:$n,i))},rand:function(n,e){var t,r=xn.Math.rndInt,u=this,i=e&&e.type?e.type:"tensor";return t="tuple"===i?n[0]?l(n[0],function(e){return r(0,n[1]-1)}):[]:n.length?l(n.length,function(e){return r(0,n[e]-1)}):[],t=u.DUAL(t,n,e)},randu:_n.rand,rank:function(n,e,t){var r,u,i,l=this,o=xn.Arithmetic,a=t&&null!=t.order?t.order:$n,s=t&&t.type?t.type:"tensor",c=o.add,f=o.sub,d=o.mul,r=o.O,m=o.J;if(n=l.DUAL(n,e,t),"tuple"===s){if(u=e[0],!u)return m;for(e=e[1],i=0;u>i;i++)r=c(d(r,e),n[i])}else{if(u=e.length,!u)return m;for(i=0;u>i;i++)r=c(d(r,e[i]),n[i])}return($n&a&&Un&a||Pn&a&&!(Un&a))&&(r=f(t&&null!=t.last?t.last:f(l.count(e,t),o.I),r)),r},unrank:function(n,e,t){var r,u,i,l,o,a,s=this,c=xn.Arithmetic,f=t&&null!=t.order?t.order:$n,d=t&&t.type?t.type:"tensor",m=c.sub,h=c.mod,_=c.div,p=c.val;if(($n&f&&Un&f||Pn&f&&!(Un&f))&&(n=m(t&&null!=t.last?t.last:m(s.count(e,t),c.I),n)),"tuple"===d){if(a=e[0],!a)return[];for(o=new Array(a),u=e[1],r=n,i=a-1;i>=0;i--)l=h(r,u),r=_(r,u),o[i]=p(l)}else{if(a=e.length,!a)return[];for(o=new Array(a),r=n,i=a-1;i>=0;i--)u=e[i],l=h(r,u),r=_(r,u),o[i]=p(l)}return o=s.DUAL(o,e,t)},cascade:_n.cascade,output:_n.output,gray:f,product:U,component:function(n,e){var t,r,u,i,l,o,a=[],s=e,c=s.length;for(t=0;c>t;t++)if(u=s[t],l=n[t],i=u[l],i instanceof Array)for(r=0,o=i.length;o>r;r++)a.push(i[r]);else a.push(i);return a}}}),bn=xn.Permutation=Rn(_n,{constructor:function Hn(n,e){var r=this;return r instanceof Hn?(e=e||{},e.type=String(e.type||"permutation").toLowerCase(),n=n||1,n instanceof _n&&(e.sub=n,n=e.sub.dimension()),e.base=e.dimension=n,e.rand={derangement:1,involution:1,connected:1},"multiset"===e.type&&(e.multiplicity=t(e.multiplicity)&&e.multiplicity.length?e.multiplicity.slice():l(n,1,0),e.multiplicity=e.multiplicity.concat(l(n-i(x,0,e.multiplicity),1,0)),e.base=e.multiplicity.length,e.multiset=q(e.multiplicity,n)),void _n.call(r,"Permutation",n,e)):new Hn(n,e)},__static__:{C:_n.C,P:_n.P,T:_n.T,DUAL:_n.DUAL,count:function(n,e){var t=xn.Arithmetic.O,r=xn.Math.factorial,u=e&&e.type?e.type:"permutation";return 0>=n?t:"cyclic"===u?xn.Arithmetic.N(n):"multiset"===u?r(n,e.multiplicity):"derangement"===u?2>n?t:r(n,!1):"involution"===u?r(n,!0):r("connected"===u?n-1:n)},initial:function(n,e,t){var r,u=this,i=e&&e.type?e.type:"permutation",o=e&&null!=-e.order?e.order:$n;if(0>=n)return null;if(t=-1===t?-1:1,($n&o&&Un&o||Pn&o&&!(Un&o))&&(t=-t),"cyclic"===i)r=0>t?[n-1].concat(l(n-1,0,1)):l(n,0,1);else if("derangement"===i){if(2>n)return null;if(1&n){var a=An.floor(n/2);r=0>t?l(n-a-1,n-1,-1).concat([a-1,a]).concat(l(a-1,a-2,-1)):l(n-3,function(n){return 1&n?n-1:n+1}).concat([n-2,n-1,n-3])}else r=0>t?l(n,n-1,-1):l(n,function(n){return 1&n?n-1:n+1})}else r="multiset"===i?0>t?e.multiset.slice().reverse():e.multiset.slice():"connected"===i?null:0>t?l(n,n-1,-1):l(n,0,1);return r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,i){if(!t||0>=t||null==n)return null;var l=r&&r.type?r.type:"permutation";return u=-1===u?-1:1,cn(n,t,u,l,r&&null!=r.order?r.order:$n,r&&null!=r.base?r.base:null,i)},rand:function(n,e){var t,r=xn.Math.rndInt,u=this,i=e&&e.type?e.type:"permutation";if("cyclic"===i){var o=r(0,n-1);t=o>0?l(n-o,o,1).concat(l(o,0,1)):l(n,0,1)}else if("derangement"===i){t=new Array(n);var a,s,c,f=!1;do{for(a=0;n>a;a++)t[a]=a;for(a=n-1,f=!1;a>=0;){if(c=r(0,a),t[c]===a){f=!0;break}s=t[a],t[a]=t[c],t[c]=s,a--}f=f||0===t[0]}while(f)}else if("involution"===i){t=l(n,0,1);for(var d,m,h,_,s,p,b=xn.Math.rnd,g=.5,y=1,x=n,A=[1].concat(l(n-1,function(){var n=g;return y+=1,g=1/(1+y*g),n})),w=l(n,0,1);x>=2;)d=x-1,m=w[d],--x,g=A[x],s=b(),s>g&&(h=r(0,x-1),_=w[h],--x,p=w[h],w[h]=w[x],w[x]=p,p=t[_],t[_]=t[m],t[m]=p)}else t="multiset"===i?v(e.multiset.slice()):"connected"===i?v(l(n,0,1),!0):v(l(n,0,1));return t=u.DUAL(t,n,e)},randu:_n.rand,rank:function(n,t,r){var u,i,l,o,a,s=this,c=xn.Arithmetic,f=r&&r.type?r.type:"permutation",d=r&&null!=-r.order?r.order:$n,m=c.sub,h=c.add,_=c.mul,p=c.div,b=c.O,v=c.I;if(t=t||n.length,!t)return c.J;if(n=s.DUAL(n,t,r),"cyclic"===f)b=c.NUM(n[0]);else{if("derangement"===f||"involution"===f||"connected"===f)return e();if("multiset"===f)for(a=r.multiplicity.slice(),o=r&&null!=r.count?r.count:xn.Math.factorial(t,a),l=t-1,u=0;l>u&&c.gt(o,v);u++)i=n[u],b=h(b,p(_(o,w(a,0,i-1,1)),t-u)),o=p(_(o,a[i]),t-u),a[i]--;else for(n=B(null,n),l=t-1,u=0;l>u;u++)b=h(_(b,t-u),n[u])}return($n&d&&Un&d||Pn&d&&!(Un&d))&&(b=m(r&&null!=r.last?r.last:m(s.count(t,r),v),b)),b},unrank:function(n,t,r){var u,i,o,a,s,c,f,d,m=this,h=xn.Arithmetic,_=r&&r.type?r.type:"permutation",p=r&&null!=-r.order?r.order:$n,b=h.mod,v=h.div,g=h.mul,y=h.sub,x=h.val;if(!t)return[];if(($n&p&&Un&p||Pn&p&&!(Un&p))&&(n=y(r&&null!=r.last?r.last:y(m.count(t,r),h.I),n)),"cyclic"===_)n=x(n),u=l(t,function(e){return(n+e)%t});else{if("derangement"===_||"involution"===_||"connected"===_)return e();if("multiset"===_)for(d=r.multiplicity.slice(),u=l(t),f=r&&null!=r.count?r.count:xn.Math.factorial(t,d),o=0;t>o;o++){for(s=0,a=0,i=x(v(g(n,t-o),f));a=0;o--)s=t-o,c=b(i,s),i=v(i,s),u[o]=x(c);Y(u,u)}}return u=m.DUAL(u,t,r)},cascade:_n.cascade,output:_n.output,permute:function(n,e,t){var r,u,i,o=n.length;for(!0===t?(u=l(o),i=n):(u=n,i=n.slice()),r=0;o>r;r++)u[r]=i[e[r]];return u},shuffle:function(n,e){if("cyclic"===e){var t=n.length,r=xn.Math.rndInt(0,t-1);return r>0&&n.push.apply(n,n.splice(0,r)),n}return"connected"===e?v(n,!0):v(n)},compose:function(){return Z(In.call(arguments))},concatenate:function(){return nn(In.call(arguments))},cycles:function(n,e){return-1===e?cycles2permutation(n):K(n)},swaps:function(n,e){return-1===e?W(n):Q(n)},inversion:function(n,e){return-1===e?Y(null,n):B(null,n)},inverse:function(n){return F(null,n)},multiset:function(n,e,t){return n===+n?q(e,n,-1===t?-1:1):-1===t?G(n):H(n,e)},matrix:function(n,e,t){return-1===t?V(null,n,e):J(null,n,e)},parity:e,is_permutation:en,is_identity:tn,is_cyclic:on,is_derangement:ln,is_involution:rn,is_kthroot:un,is_connected:an}}),vn=xn.Combination=Rn(_n,{constructor:function Fn(n,e,r){var u=this;if(!(u instanceof Fn))return new Fn(n,e,r);if(t(n)?(r=e||{},e=n[1]||1,n=n[0]||1):(r=r||{},n=n||1,e=e||1),r.type=String(r.type||"unordered").toLowerCase(),-1t?l(n[1],n[0]-1,0):l(n[1],0,0):"ordered"===i?0>t?l(n[1],n[0]-1,-1):l(n[1],0,1):0>t?l(n[1],n[0]-n[1],1):l(n[1],0,1),r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,i){return!t||!t[0]||0>=t[0]||null==n?null:(u=-1===u?-1:1,dn(n,t,u,r&&r.type?r.type:"unordered",r&&null!=r.order?r.order:$n,i))},rand:function(n,e){var t,r,u,i,o=this,a=e&&e.type?e.type:"unordered",s=n[1],c=xn.Math.rndInt;return n=n[0],r=n-s,u=n-1,"repeated"===a||"ordered+repeated"===a?(t=1===s?[c(0,u)]:l(s,function(){return c(0,u)}),s>1&&"repeated"===a&&b(t)):"ordered"===a?(i={},t=1===s?[c(0,u)]:n===s?v(l(s,0,1)):l(s,function(){for(var e=c(0,u);1===i[e];)e=(e+1)%n;return i[e]=1,e})):(i={},t=1===s?[c(0,u)]:n===s?l(s,0,1):s>r?_(null,n,b(l(r,function(){for(var e=c(0,u);1===i[e];)e=(e+1)%n;return i[e]=1,e}))):b(l(s,function(){for(var e=c(0,u);1===i[e];)e=(e+1)%n;return i[e]=1,e}))),t=o.DUAL(t,n,e)},randu:_n.rand,rank:function(n,e,t){var r,u,i,l,o,a=this,s=xn.Arithmetic,c=s.add,f=s.sub,d=s.mul,m=s.O,h=s.I,_=m,p=e[1],b=t&&null!=t.order?t.order:$n,v=t&&t.type?t.type:"unordered",g=xn.Math.factorial;if(n=a.DUAL(n,e,t),"ordered+repeated"===v)for(l=e[0],r=0;p>r;r++)_=c(d(_,l),n[r]);else if("repeated"===v){for(l=e[0]+p-1,o=t&&t.count?t.count:g(l,p),r=1;p>=r;r++)u=l-1-n[r-1]-r+1,i=p+1-r,u>=i&&(_=c(_,g(u,i)));_=f(f(o,h),_)}else if("ordered"===v)for(l=e[0],n=B(null,n,l),r=0;p>r;r++)_=c(d(_,l-r),n[r]);else{for(l=e[0],o=t&&t.count?t.count:g(l,p),r=1;p>=r;r++)u=l-1-n[r-1],i=p+1-r,u>=i&&(_=c(_,g(u,i)));_=f(f(o,h),_)}return($n&b&&Un&b||Pn&b&&!(Un&b))&&(_=f(t&&null!=t.last?t.last:f(a.count(e,t),h),_)),_},unrank:function(n,e,t){var r,u,i,o,a,s,c,f=this,d=xn.Arithmetic,m=d.O,h=d.I,_=d.sub,p=d.div,b=d.mod,v=d.mul,g=d.lte,y=d.gt,x=d.val,A=e[1],w=t&&t.type?t.type:"unordered",M=t&&null!=t.order?t.order:$n;if(e=e[0],($n&M&&Un&M||Pn&M&&!(Un&M))&&(n=_(t&&null!=t.last?t.last:_(f.count(e,t),d.I),n)),r=l(A),"ordered+repeated"===w)for(o=n,s=A-1;s>=0;s--)a=b(o,e),o=p(o,e),r[s]=x(a);else if("ordered"===w){for(o=n,s=A-1;s>=0;s--)i=e-s,a=b(o,i),o=p(o,i),r[s]=x(a);Y(r,r,i)}else{c="repeated"===w,i=c?e+A-1:e,u=t&&t.count?t.count:xn.Math.factorial(i,A),n=_(_(u,h),n),u=p(v(u,i-A),i),a=i-A+1,o=A,s=i-1;do g(u,n)?(r[A-o]=c?i-a-A+1:i-a-o+1,y(u,m)&&(n=_(n,u),u=p(v(u,o),s)),o--,s--):(u=p(v(u,s-o),s),a--,s--);while(o>0)}return r=f.DUAL(r,e,t)},cascade:_n.cascade,output:function(n,e,t,r){return null==n?null:n=r&&"binary"===r.type?k(n,t[0]):n.slice()},complement:function(n,e,t){return!0===t?v(_(null,e,b(n))):_(null,e,n)},binary:function(n,e,t){return-1===t?$(n,e):k(n,e)},pick:function(n,e,t){return e>0&&n.length?g(n,e,"ordered+repeated"!==t&&"ordered"!==t,"ordered+repeated"===t||"repeated"===t,new Array(e)):[]},choose:function(n,e){return l(e.length,function(t){return n[e[t]]})}},_update:function(){var n=this;return n.item__=fn(n.__item,n.n[0],n.n[1],n.$.order,n.$.type),n}}),vn.conjugate=vn.complement,vn.project=vn.choose,gn=xn.Powerset=xn.Subset=Rn(_n,{constructor:function Bn(n,e){var t=this;return t instanceof Bn?(e=e||{},e.type=e.type||"subset",n=n||1,n instanceof _n&&(e.sub=n,n=e.sub.base()),e.base=n,e.dimension=n,e.mindimension=0,e.maxdimension=n,void _n.call(t,"Subset",n,e)):new Bn(n,e)},__static__:{C:function(n,e){return _(null,e,n)},P:_n.P,T:_n.T,DUAL:function(n,e,t){if(null==n)return null;var r=this,u=r.C,i=r.P,l=(r.T,t&&null!=t.order?t.order:$n);return n=Tn&l?kn&l?n:i(n):Nn&l?kn&l?n:i(n):Pn&l?kn&l?i(u(n,e)):u(n,e):kn&l?n:i(n)},count:function(n,e){return xn.Math.pow2(n)},initial:function(n,e,t){var r,u=this,i=e&&null!=e.order?e.order:$n;return t=-1===t?-1:1,($n&i&&Un&i||Pn&i&&!(Un&i))&&(t=-t),r=0>t?l(n,0,1):[],r=u.DUAL(r,n,e)},succ:_n.succ,rand:function(n,e){for(var t,r=this,u=xn.Math.rndInt,i=null,o=n-1;o>=0;o--)u(0,1)&&(i={len:i?i.len+1:1,k:o,next:i});return t=i?l(i.len,function(n){var e=i.k;return i=i.next,e}):[],t=r.DUAL(t,n,e)},randu:_n.rand,rank:function(n,e,t){var r=this,u=xn.Arithmetic,i=u.O,l=u.I,o=u.add,a=u.shl,s=u.sub,c=t&&null!=t.order?t.order:$n,f=i,d=0,m=n.length;for(n=r.DUAL(n,e,t);m>d;)f=o(f,a(l,n[d++]));return($n&c&&Un&c||Pn&c&&!(Un&c))&&(f=s(t&&null!=t.last?t.last:s(r.count(e,t),l),f)),f},unrank:function(n,e,t){var r=this,u=xn.Arithmetic,i=u.O,l=u.J,o=u.band,a=u.shr,s=u.gt,c=u.sub,f=t&&null!=t.order?t.order:$n,d=[],m=0,h=t&&t.count?t.count:r.count(e,t);if(!u.inside(n,l,h))return null;for(($n&f&&Un&f||Pn&f&&!(Un&f))&&(n=c(t&&null!=t.last?t.last:c(h,u.I),n));s(n,i);)s(o(n,1),i)&&d.push(m),s(o(n,2),i)&&d.push(m+1),s(o(n,4),i)&&d.push(m+2),s(o(n,8),i)&&d.push(m+3),s(o(n,16),i)&&d.push(m+4),s(o(n,32),i)&&d.push(m+5),s(o(n,64),i)&&d.push(m+6),s(o(n,128),i)&&d.push(m+7),m+=8,n=a(n,8);return d=r.DUAL(d,e,t)},cascade:_n.cascade,output:function(n,e,t,r){if(null==n)return null;var u=n;return r&&"binary"===r.type&&(n=k(n,t)),u===n?n.slice():n},binary:function(n,e,t){return-1===t?$(n,e):k(n,e)}}}),yn=xn.Partition=Rn(_n,{constructor:function Yn(n,e){var t=this;if(!(t instanceof Yn))return new Yn(n,e);e=e||{},e.type=e.type||"partition",n=n||1,n instanceof _n&&(e.sub=n,n=e.sub.base());var r=e["max="]?0|e["max="]:null,u=e["parts="]?0|e["parts="]:null,i=u?u:r?n-r+1:n,l=u?u:r?An.ceil(n/r):1;e.base=n,e.dimension=u||n,e.mindimension=An.min(l,i),e.maxdimension=An.max(l,i),e.rand={partition:1,composition:1,packed:1},_n.call(t,"Partition",n,e)},__static__:{C:function(n,e,t){var r=t&&t.type?t.type:"partition",u=t&&null!=t.order?t.order:$n,i=t&&t["max="]?0|t["max="]:null,l=t&&t["parts="]?0|t["parts="]:null;return"composition"!==r&&Pn&u&&s(n,n),l||i?n:T("composition"===r,n,1)},P:_n.P,T:_n.T,DUAL:_n.DUAL,count:function(n,e){var t=e&&e["max="]?0|e["max="]:null,r=e&&e["parts="]?0|e["parts="]:null,u=e&&e.type?e.type:"partition";return"composition"===u?xn.Math.compositions(n,r,t):xn.Math.partitions(n,r,t)},initial:function(n,e,t){var r,u,i,o=this,a=e&&e.type?e.type:"partition",c=e&&e["max="]?0|e["max="]:null,f=e&&e["parts="]?0|e["parts="]:null,d=e&&null!=e.order?e.order:$n,m="composition"===a;return 0>=n||f&&c&&(f+c>n+1||n>f*c)||f&&f>n||c&&c>n?null:(t=-1===t?-1:1,($n&d&&Un&d||Pn&d&&Un&d)&&(t=-t),f&&c?(1===f&&(r=[c]),m?(i=An.min(c,An.ceil((n-c)/(f-1))),r=[(n-c)%i||i].concat(l(f-2,i,0)).concat([c]),0>t&&s(r,r)):0>t?(u=An.min(f,An.floor(n/c)||1),n-=u*c,f-=u,0===n&&f>0&&(u--,f++,n+=c),r=[c].concat(l(u-1,c,0)).concat(n>0&&f>0?[n-f+1].concat(l(f-1,1,0)):[])):(i=An.min(c,An.ceil((n-c)/(f-1))),r=[c].concat(l(f-2,i,0).concat([(n-c)%i||i])))):f?m?(r=l(f-1,1,0).concat([n-f+1]),0>t&&s(r,r)):(i=An.ceil(n/f),u=n%i||i,r=0>t?[n-f+1].concat(l(f-1,1,0)):l(f-1,i,0).concat([u])):c?m?(r=l(n-c,1,0).concat([c]),0>t&&s(r,r)):(u=An.floor(n/c),i=n%c,r=0>t?l(u,c,0).concat(i?[i]:[]):[c].concat(l(n-c,1,0))):r=0>t?[n]:l(n,1,0),r=o.DUAL(r,n,e))},succ:function(n,e,t,r,u,i){if(null==t||null==n)return null;var l=r&&r.type?r.type:"partition",o=r&&r["max="]?0|r["max="]:null,a=r&&r["parts="]?0|r["parts="]:null;return 0>=t||a&&o&&(a+o>t+1||t>a*o)||a&&a>t||o&&o>t?null:(u=-1===u?-1:1,"composition"===l?hn(n,t,u,a,o,r&&null!=r.order?r.order:$n,i):mn(n,t,u,a,o,r&&null!=r.order?r.order:$n,i))},rand:function(n,e){var t,r,u,i,o,a=this,s=xn.Math.rndInt,c=e&&e.type?e.type:"partition",f=(e&&null!=e.order?e.order:$n,e&&e["max="]?0|e["max="]:null),d=e&&e["parts="]?0|e["parts="]:null,m=0,h=0,_="composition"===c,p=!1;if(0>=n||d&&f&&(d+f>n+1||n>d*f)||d&&d>n||f&&f>n)return null;if(d&&f)return null;if(f&&!d&&(d=f,p=!0),d)1===d?(r=[n],h=n):n===d?(r=l(d,1,0),h=n):(t={},u=n-2,l(r=b(l(d-1,function(){for(o=s(0,u);1===t[o];)o=(o+1)%(u+1);return t[o]=1,o+1})),function(n){return o=r[n],i=o-m,m=o,h+=i,i}));else{for(t=null,o=1;n>o;o++)s(0,1)&&(i=o-m,m=o,h+=i,t={len:t?t.len+1:1,x:i,next:t});r=t?l(t.len,function(){return i=t.x,t=t.next,i}):[]}return n>h&&(r.length?r.splice(s(0,r.length-1),0,n-h):r.push(n-h)),_?p&&(r=v(T(0,b(r,-1)))):(r=b(r,-1),p&&(r=T(0,r))),r=a.DUAL(r,n,e)},randu:_n.rand,rank:e,unrank:e,cascade:_n.cascade,output:function(n,e,t,r){if(null==n)return null;var u=n,i=r&&r.type?r.type:"partition",l=r&&null!=r.order?r.order:$n,o=kn&l?-1:1;r&&r["max="]?0|r["max="]:null,r&&r["parts="]?0|r["parts="]:null;return"composition"===i||"packed"===i&&(n=X(n,o)),u===n?n.slice():n},conjugate:function(n,e){return T("composition"===e,n)},subset:function(n,e){return-1===e?N(n):P(n)},pack:function(n,e){return-1===e?j(n):X(n)}}}),yn.transpose=yn.conjugate,xn}); \ No newline at end of file +!function(n,e,t){"use strict";"undefined"!=typeof Components&&"object"==typeof Components.classes&&"object"==typeof Components.classesByID&&Components.utils&&"function"==typeof Components.utils["import"]?(n.$deps=n.$deps||{})&&(n.EXPORTED_SYMBOLS=[e])&&(n[e]=n.$deps[e]=t.call(n)):"object"==typeof module&&module.exports?(module.$deps=module.$deps||{})&&(module.exports=module.$deps[e]=t.call(n)):"undefined"!=typeof System&&"function"==typeof System.register&&"function"==typeof System["import"]?System.register(e,[],function(r){r(e,t.call(n))}):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(e)?define(e,["module"],function(e){return t.moduleUri=e.uri,t.call(n)}):e in n||(n[e]=t.call(n)||1)&&"function"==typeof define&&define.amd&&define(function(){return n[e]})}(this,"Abacus",function(n){"use strict";function e(){throw new Error("Method not implemented!")}function t(n){return n instanceof Array||"[object Array]"===En.call(n)}function r(n){return n instanceof String||"[object String]"===En.call(n)}function u(n){var e=n.toString(2),t=32-e.length;return t>0?new Array(t+1).join("0")+e:e}function i(n,e,r,u,i,l,o){var a,s,c,f,d,m,h,_,p=e,b=null,v=r&&t(r);if(v&&(null==u&&(u=0),null==i&&(i=r.length-1)),null==l&&(l=u>i?-1:1),0===l||v&&!r.length||0>=In.floor((i-u)/l)+1)return p;if(0>l?(_=(u-i)%-l,_&&(b=i),i+=_,m=i,h=u,f=-1,c=-(-l<<4)):(_=(i-u)%l,_&&(b=i),i-=_,m=u,h=i,f=1,c=l<<4),d=u+l*(15&In.floor((i-u)/l+1)),v){for(m=In.max(0,m),h=In.min(r.length-1,h),a=u;a>=m&&h>=a&&f*(d-a)>0;a+=l)p=n(p,r[a],a);for(s=d;s>=m&&h>=s;s+=c)a=s,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a),a+=l,p=n(p,r[a],a);!0===o&&null!==b&&b>=0&&b=m&&h>=a&&f*(d-a)>0;a+=l)p=n(p,a,a);for(s=d;s>=m&&h>=s;s+=c)a=s,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a),a+=l,p=n(p,a,a);!0===o&&null!==b&&(p=n(p,b,b))}return p}function l(n,e,r){var u=t(n)?n:(n=0|n)>0?new Array(n):[];if(n=u.length,n>0&&null!=e){r=r||0;var l=e;i("function"==typeof e?function(n,t,r){return n[r]=e(r),n}:e===+e?function(n,e,t){return n[t]=l,l+=r,n}:function(n,t,r){return n[r]=e,n},u,u)}return u}function o(n,e,t){return i(function(n,t,r){return n[r]=t[e],n},!0===t?n:new Array(n.length),n)}function a(n,e,r,u,l){return null==e?n:i(t(r)?function(n,e,t){return n[t]=r[t]-1-e,n}:function(n,e,t){return n[t]=r-1-e,n},n,e,u,l)}function s(n,e,t,r,u){if(null==e)return n;if(null==r&&(r=0),null==u&&(u=e.length-1),u>r)for(var i,l=r,o=u;o>l;l++,o--)i=e[l],n[l]=e[o],n[o]=i;return n}function c(n,e){return null==e&&(e=0),t(n)?l(n,t(e)?function(t){return e[t]-1-n[n.length-1-t]}:function(t){return e-n[t]}):n===+n&&e===+e?e-n:Mn.Arithmetic.sub(Mn.Arithmetic.N(e),n)}function f(n,e,r,u,l){if(null==e)return null;var o=0;return i(t(r)?function(n,e,t){return n[t]=r[t]>0?(e+o)%r[t]:0,o+=r[t]-n[t],n}:function(n,e,t){return n[t]=(e+o)%r,o+=r-n[t],n},n,e,u,l)}function d(n,e,t,r,u,l,o,a){if(null==e)return null;null==t&&(t=1),null==r&&(r=0),null==u&&(u=0),null==l&&(l=e.length-1),null==o&&(o=u),null==a&&(a=l);var s=0,c=o>a?-1:1,f=o;return i(function(n,e,u){return e=r+t*e,n[f]=e-s,s=e,f+=c,n},n,e,u,l)}function m(n,e,t,r,u,l,o,a){if(null==e)return null;null==t&&(t=1),null==r&&(r=0),null==u&&(u=0),null==l&&(l=e.length-1),null==o&&(o=u),null==a&&(a=l);var s=0,c=o>a?-1:1,f=o;return i(function(n,e,u){return s+=e,n[f]=r+t*s,f+=c,n},n,e,u,l)}function h(n,e,t,r,u,i,l,o){r=-1===r?-1:1,null==u&&(u=0),null==i&&(i=e.length-1),null==l&&(l=0),null==o&&(o=t.length-1);var a=u>i?-1:1,s=l>o?-1:1,c=a*(i-u)+1,f=s*(o-l)+1,d=u,m=l,h=0;if(null==n&&(n=new Array(In.min(c,f))),0===n.length)return n;for(;a*(i-d)>=0&&s*(o-m)>=0;)1===r&&e[d]t[m]?d+=a:1===r&&e[d]>t[m]||-1===r&&e[d]i?-1:1,c=o>a?-1:1,f=s*(i-u)+1,d=u,m=u,h=0;if(!t||!t.length)return e===+e?l(e,u,s):e?e.slice():e;if(null==n&&(n=new Array(f)),e===+e){for(;s*(i-d)>=0&&c*(a-m)>=0;)d===t[m]?(d+=s,m+=s):1===r&&d>t[m]||-1===r&&d=0;)n[h++]=d,d+=s}else{for(;s*(i-d)>=0&&c*(a-m)>=0;)e[d]===t[m]?(d+=s,m+=s):1===r&&e[d]>t[m]||-1===r&&e[d]=0;)n[h++]=e[d],d+=s}return hi?-1:1,d=l>o?-1:1,m=f*(i-u)+1,h=d*(o-l)+1,_=m+h,p=u,b=l,v=0,g=null,y=!s;if(null==n&&(n=new Array(_)),0===n.length)return c?e:n;for(;f*(i-p)>=0&&d*(o-b)>=0;){if(s&&v){if(e[p]===g){p+=f;continue}if(t[b]===g){b+=d;continue}}a?1===r&&e[p][0]t[b][0]?(n[v++]=g=e[p],p+=f):1===r&&e[p][0]>t[b][0]||-1===r&&e[p][0]t[b][1]?(n[v++]=g=e[p],y&&(n[v++]=t[b])):(n[v++]=g=t[b],y&&(n[v++]=e[p])),p+=f,b+=d):1===r&&e[p]t[b]?(n[v++]=g=e[p],p+=f):1===r&&e[p]>t[b]||-1===r&&e[p]=0;)(y||e[p]!==g)&&(n[v++]=g=e[p],p+=f);for(;d*(o-b)>=0;)(y||t[b]!==g)&&(n[v++]=g=t[b],b+=d);if(c){for(p=0>f?i:u,v=0;_>v;v++,p++)e[p]=n[v];return e}return vu?-1:1,a=l*(u-r)+1;if(t=!0===t,1>=a)return t?1===a?[r]:[]:n;e=-1===e?-1:1;var s=a,c=1,f=2,d=In.min,m=new Array(a);for(t&&(n=i(function(n,e,t){return n[t-r]=[e,t],n},new Array(a),n,r,u,1),r=0,u=a-1);s>0;)i(function(u,i){p(m,n,n,e,r+l*i,r+l*(i+c-1),r+l*(i+c),r+l*d(i+f-1,a-1),t,!1,!0)},null,null,0,a-c-1,f),c<<=1,f<<=1,s>>=1;return t?o(n,n,1):n}function v(n,e,r,u){var l,o=Mn.Math.rndInt,a=!0===e?1:0;return t(r)?1<(l=r.length)&&i(function(n){if(aa;a++)o[a]=n[i+f(0,d)];return t&&b(o),o}for(a=0;e>a;a++)s=f(0,--d),c=n[i+s],n[i+s]=n[i+d],n[i+d]=c,o[a]=c,u&&(u[a]=s);if(u)for(a=e-1;a>=0;a--)s=u[a],c=n[i+d],n[i+d]=n[i+s],n[i+s]=c,d++;return t&&b(o),o}function y(n,e,t,r,u){t=-1===t?-1:1,null==r&&(r=0),null==u&&(u=e.length-1);var i,l,o=r,a=u;if(n===e[o])return o;if(n===e[a])return a;for(;a>o;){if(i=o+(a-o+1>>>1),l=e[i],n===l)return i;1===t&&l>n||-1===t&&n>l?a=i-1:o=i+1}return-1}function x(n,e){return n+e}function A(n,e){return n*e}function w(n,e,t,r){return i(Mn.Arithmetic.add,Mn.Arithmetic.O,n,e,t,r)}function M(n,e,t,r){return i(Mn.Arithmetic.mul,Mn.Arithmetic.I,n,e,t,r)}function I(n){var e=Mn.Arithmetic;return e.shl(e.I,e.N(n))}function O(n,e){var t=Mn.Arithmetic,r=t.N;return t.pow(r(n),r(e))}function C(n,e){var r,u=Mn.Arithmetic,l=u.O,o=u.I,a=u.J,s=u.N,c=u.add,f=(u.sub,u.div),d=u.mul;return null==e?10>=n?0>n?l:0===n?o:s([1,2,6,24,120,720,5040,40320,362880,3628800][n-1]):(r=String(n),null==C.mem1[r]&&(C.mem1[r]=d(C(n-1),n)),C.mem1[r]):!1===e?10>=n?2>n?l:s([1,2,9,44,265,1854,14833,133496,1334961][n-2]):(r="!"+String(n),null==C.mem2[r]&&(C.mem2[r]=c(1&n?a:o,d(C(n-1,!1),n))),C.mem2[r]):!0===e?10>=n?0>n?l:0===n?o:s([1,2,4,10,26,76,232,764,2620,9496][n-1]):(r="I"+String(n),null==C.mem2[r]&&(C.mem2[r]=c(C(n-1,!0),d(C(n-2,!0),n-1))),C.mem2[r]):t(e)?e.length?0>n?l:(r=String(n)+"@"+e.join(","),null==C.mem3[r]&&(C.mem3[r]=f(C(n),i(function(n,e){return d(n,C(e))},C(e[e.length-1]),e,e.length-2,0))),C.mem3[r]):0>n?l:C(n):e===+e?0>e?-e>n?l:(r=String(n)+"@"+String(e),null==C.mem3[r]&&(C.mem3[r]=i(d,o,null,n+e+1,n)),C.mem3[r]):(e+e>n&&(e=n-e),0>e||1>n?l:0===e||1===n?o:1===e?s(n):(r=String(n)+"@"+String(e),null==C.mem3[r]&&(C.mem3[r]=u.isDefault()?In.round(i(function(e,t){return e*(1+n/t)},(n-=e)+1,null,2,e)):c(C(n-1,e-1),C(n-1,e))),C.mem3[r])):l}function L(n,e,t,r){var u,l,o=Mn.Arithmetic,a=o.add,s=o.O;return 0>n||0>=e||t>r||t+e>n+1||n>e*r?s:r===n&&1===e||e===n&&1===r?o.I:(u=String(n)+","+String(e)+","+String(t)+","+String(r),null==L.mem[u]&&(n-=e*(t-1),r=r-t+1,l=String(n)+","+String(e)+","+String(t)+","+String(r),null==L.mem[l]&&(L.mem[l]=i(function(t,u){return a(t,L(n-r,e-1,1,u))},s,null,In.max(1,In.ceil((n-r)/(e-1))),In.min(r,n-r-e+2),1)),L.mem[u]=L.mem[l]),L.mem[u])}function S(n,e,t){e=null==e?null:0|e,t=null==t?null:0|t;var r,u=Mn.Arithmetic.add,l=Mn.Arithmetic.O,o=t?t:0,a=t?t:1,s=e?e:1,c=e?e:n;return 0>n||e&&t&&(e+t>n+1||n>e*t)||t&&t>n||e&&e>n?l:(t&&!e&&(o=0,a=1,s=t,c=t,e=t,t=null),r=String(n)+"|"+String(e)+"|"+String(t),null==S.mem[r]&&(S.mem[r]=i(function(e,t){return i(function(e,r){return u(e,L(n,t,1,r))},e,null,a,o?o:n-t+1,1)},l,null,s,c,1)),S.mem[r])}function E(n,e,t,r){var u,l=Mn.Arithmetic,o=l.add,a=l.O;return 0>n||0>=e||t>r||t+e>n+1||n>e*r?a:t===r?e*t===n?l.I:a:n===r?C(n-e*t+e-1,e-1):t+1===r?C(e,n-e*t):(u=String(n)+","+String(e)+","+String(t)+","+String(r),null==E.mem[u]&&(E.mem[u]=i(function(n,u){return o(n,E(u,e-1,t,r))},a,null,n-r,n-t,1)),E.mem[u])}function D(n,e,t){e=null==e?null:0|e,t=null==t?null:0|t;var r,u=Mn.Arithmetic.O,l=Mn.Arithmetic.add;return 0>n||e&&t&&(e+t>n+1||n>e*t)||t&&t>n||e&&e>n?u:(r=String(n)+"|"+String(e)+"|"+String(t),null==D.mem[r]&&(e&&t?D.mem[r]=E(n,e,1,t):e?D.mem[r]=E(n,e,1,n):t?D.mem[r]=i(function(e,r){return l(e,E(n,r,1,t))},u,null,In.ceil(n/t),n,1):D.mem[r]=n>=1?I(n-1):Arithmetic.I),D.mem[r])}function R(n){var e,t=Mn.Arithmetic,r=t.O,u=t.N,i=t.div,l=t.mul;return 14>=n?0>n?r:u([1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900][n]):(e=String(n),null==R.mem[e]&&(R.mem[e]=i(l(R(n-1),4*n-2),n+1)),R.mem[e])}function U(n){var e,t=Mn.Arithmetic,r=t.O,u=t.N,l=t.add,o=t.mul;return 12>=n?0>n?r:u([1,1,2,5,15,52,203,877,4140,21147,115975,678570][n]):(e=String(n),null==U.mem[e]&&(U.mem[e]=i(function(e,t){return l(e,o(C(n-1,t),U(t)))},r,null,0,n-1,1)),U.mem[e])}function k(n){var e,t=Mn.Arithmetic,r=t.O,u=t.N,i=t.add;return 29>=n?0>n?r:u([0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811][n]):(e=String(n),null==k.mem[e]&&(k.mem[e]=i(k(n-1),k(n-2))),k.mem[e])}function $(n){if(!arguments.length||null==n)return Tn;if(r(n)){n=n.toUpperCase().split(",");var e,t=0,u=n.length;for(e=0;u>e;e++)t|=Sn.call($,n[e])?$[n[e]]:0;return t>0&&!(t&(Jn|qn))&&(t|=Tn),t&qn&&(t&=~Nn),t>0?t:Tn}return n&qn&&(n&=~Nn),Vn&n?n:Tn}function N(){var n,e,r,u,i,l,o,a,s,c,f=arguments,d=f.length;if(!d)return[];if(!0===f[0]){for(s=f[1].length,n=2;d>n;n++)s*=f[n].length;for(c=new Array(s),n=0;s>n;n++){for(a=0,l=1,r=n,e=1;d>e;e++)u=f[e].length,i=r%u,r=~~(r/u),o=f[e][i],a+=l*o,l*=u;c[n]=a}}else{for(s=f[0].length,n=1;d>n;n++)s*=f[n].length;for(c=new Array(s),n=0;s>n;n++){for(a=[],r=n,e=d-1;e>=0;e--)if(u=f[e].length,i=r%u,r=~~(r/u),o=f[e][i],t(o))for(l=o.length-1;l>=0;l--)a.unshift(o[l]);else a.unshift(o);c[n]=a}}return c}function P(n,e){if(0>e)return[];var t,r=l(e,0,0),u=n.length;for(e-=1,t=0;u>t;t++)r[e-n[t]]=1;return r}function T(n,e){e=In.min(e||n.length,n.length);var t,r=[];for(e-=1,t=0;e>=t;t++)0=u?o=e.slice():(o=j(_(null,u-1,X(e,a-1,r))),0<(u-=i(x,0,o))&&o.push(u),0>r&&s(o,o));else{var c,f,d,m,h,p=1,b=0,v=0,g="push";if(0>r&&(p=-p,b=a-1-b,g="unshift"),t(e[b]))for(h=e[b],o=[[h[1],h[0]]],c=0,d=1,m=p+b;a>d;d++,m+=p)h=e[m],f=0>r?0:c,h[1]===o[f][0]?o[f][1]+=h[0]:(o[g]([h[1],h[0]]),c++);else for(u=e[b],o=l(u,1,0),0>r&&(v=u-1-v),d=1,m=p+b;a>d;d++,m+=p)for(c=0,f=v,h=e[m];u>c&&h>0;)o[f]++,h--,c++,f+=p}return o}function J(n,e){if(null==n)return null;var t,r,u,i,l=[],o=n.length,a=-1===e,s=1,c=0,f="push";for(a&&(s=-s,c=o-1-c,f="unshift"),u=n[c],i=[u,1],t=1;o>t;t++)r=s*t+c,u===n[r]?i[1]++:(l[f](i),u=n[r],i=[u,1]);return l[f](i),l}function V(n,e){if(null==n)return null;var t,r,u,i,l,o=[],a=n.length,s=-1===e,c=1,f=0,d="push";for(s&&(c=-c,f=a-1-f,d="unshift"),t=0;a>t;t++)if(l=n[c*t+f],1===l[1])o[d](l[0]);else for(u=l[1],i=l[0],r=0;u>r;r++)o[d](i);return o}function G(n,e,t){var r=n?n.length:0,u=1,l=0,o=0,a=r>o?n[o]||1:1;return-1===t&&(u=-1,l=(r||e)-1),i(function(e,t){return 0>=a&&(o++,l+=u,a=r>o?n[o]||1:1),a--,e[t]=l,e},new Array(e),null,0,e-1)}function H(n,e,t){var r,u,i=e.length,l=i*i;for(n=n||new Array(l),r=0,u=0;l>r;)n[r+u]=0,++u>=i&&(u=0,r+=i);if(!0===t)for(r=0;i>r;r++)n[i*e[r]+r]=1;else for(r=0,u=0;i>u;u++,r+=i)n[r+e[r]]=1;return n}function F(n,e,t){var r,u,i=e.length,l=In.floor(In.sqrt(i));if(n=n||new Array(l),!0===t)for(r=0,u=0;l>r;)e[l*r+u]&&(n[u]=r),++u>=l&&(u=0,r++);else for(r=0,u=0;l>r;)e[r+u]&&(n[r]=u),++u>=l&&(u=0,r++);return n}function B(n){return z(null,b(n,1,!0))}function Y(n,e){return e&&e.length?i(function(n,t,r){return n[r]=ti;i++)1&r&&(e-=s[r>>>1<<1]),s[r]+=1,r>>>=1;return s[r]+=1,n[t]=e,n},n||new Array(r),e)}function Q(n,e,t){var r,u,l,o=e.length,a=In.ceil(Dn(t||o)),s=1<=r;r++)for(l=1,u=1<=l;l++)f[u-1+l]=1<u;u++)f[r]-=1,r<<=1,e>=f[r]&&(e-=f[r],r++);return f[r]=0,n[t]=r-s,n},n||new Array(o),e)}function W(n,e,t){var r,u=n.length,i=null==e;if(u>1)for(i&&(e=new Array(u-1),t=0),r=u-1;r>=1;r--)e[t++]=[n[0],n[r]];else i&&(e=[]);return i?e:t}function Z(n,e){var t,r,u,i,l=n.length,o=new Array(l),a=!0===e?1:0,s=new Array(l),c=0,f=0;for(t=0;l>t;t++)s[t]=0;for(u=new Array(l),i=0,r=c++,u[i++]=r,s[r]=1;l>c;){if(r=n[r],s[r])for(i>a&&(u.length=i,o[f++]=u),u=new Array(l),i=0;l>c&&s[r=c];)++c;s[r]||(u[i++]=r,s[r]=1)}return i>a&&(u.length=i,o[f++]=u),fe;e++)i=W(l[e],u,i);return it;t++)l[t]=t;for(t=0;i>t;t++)swap=r[t],u=l[r[0]],l[r[0]]=l[r[1]],l[r[1]]=u;return l}function tn(n){var e,t,r,u=n,i=u.length,l=i?u[0]:[],o=l.length;for(t=1;i>t;t++)for(r=l.slice(),e=0;o>e;e++)l[e]=r[u[t][e]];return l}function rn(n){var e,t,r,u,i,l,o=n,a=o.length,s=0;for(r=0;a>r;r++)s+=o[r].length;for(e=new Array(s),u=0,r=0;a>r;r++){for(i=o[r],l=i.length,t=0;l>t;t++)e[u+t]=u+i[t];u+=l}return e}function un(n,e){if(e=e||n.length,e!==n.length)return!1;var t,r,u=l(e,0,0);for(t=0;e>t;t++){if(r=n[t],0>r||r>=e||0t;t++)if(1!==u[t])return!1;return!0}function ln(n){for(var e=n.length,t=0;e>t;t++)if(n[t]!==t)return!1;return!0}function on(n){for(var e=n.length,t=0,r=n[t];e>t;t++,r=n[t])if(0>r||r>=e||n[r]!==t)return!1;return!0}function an(n,e){if(e=e||1,1>e)return!1;var t,r,u,i=n.length;for(t=0;i>t;t++){for(r=n[t],u=1;e>=u&&t!==r;)u++,r=n[r];if(t!==r||u!==e&&(u>=e||e%u>0))return!1}return!0}function sn(n,e,t){e=0|e;for(var r=0,u=n.length,i=0;u>i;i++)if(n[i]===i&&r++,r>e)return!1;return!0===t?r===e:!0}function cn(n){for(var e=n.length,t=1,r=n[0];e>t;t++)if(n[t]!==(r+t)%e)return!1;return!0}function fn(n){for(var e=n.length-1,t=-1,r=0,u=n[r];e>r;r++,u=n[r])if(u>t&&(t=u),r>=t)return!1;return!0}function dn(n,e,t,r,u,i){var l,o,a,s,c,f,d,m,h,_,p=e;if("tuple"===r?(l=p[0],p=p[1]):l=p.length,h=0,_=l-1,f=1,s=_,c=h,d=1,m=0,Xn&u&&(f=-f,s=_-s,c=_-c,d=-d,m=_-m),Pn&u&&(f=-f,s=_-s,c=_-c,d=-d,m=_-m),Nn&u&&(t=-t),0>t)if("tuple"===r){for(o=s;o>=h&&_>=o&&0===n[o];)o-=f;if(o>=h&&_>=o)for(p-=1,n[o]=n[o]-1,a=o+f;a>=h&&_>=a;a+=f)n[a]=p;else n=null}else{for(o=s;o>=h&&_>=o&&0===n[o];)o-=f;if(o>=h&&_>=o)for(n[o]=n[o]-1,a=o+f;a>=h&&_>=a;a+=f)n[a]=p[d*a+m]-1;else n=null}else if("tuple"===r){for(o=s;o>=h&&_>=o&&n[o]+1===p;)o-=f;if(o>=h&&_>=o)for(n[o]=n[o]+1,a=o+f;a>=h&&_>=a;a+=f)n[a]=0;else n=null}else{for(o=s;o>=h&&_>=o&&n[o]+1===p[d*o+m];)o-=f;if(o>=h&&_>=o)for(n[o]=n[o]+1,a=o+f;a>=h&&_>=a;a+=f)n[a]=0;else n=null}return n}function mn(n,e,t,r,u,i,l){var o,a,s,c,f,d,m,h,_,p,b,v,g,y,x=e,A=null==i?x:i;if(g=0,y=x-1,h=1,m=y,_=1,p=0,b=1,v=0,Xn&u&&(h=-h,m=y-m,_=-_,p=A-1-p,t=-t),Pn&u&&(h=-h,m=y-m,b=-1,v=A-1),Nn&u&&(t=-t),0>t)if("cyclic"===r)if(o=y-m,_*n[o]+p>0)for(b=x-1,h=x+h,s=0;x>s;s++)f=(_*n[o]+p+b)%x,n[o]=_*f+p,o=(o+h)%x;else n=null;else if("involution"===r)n=null;else if("connected"===r)n=null;else do{for(d=!1,o=m-h;o>=g&&y>=o&&_*n[o]<=_*n[o+h];)o-=h;if(o>=g&&y>=o){for(a=m;a>=g&&y>=a&&h*(a-o)>0&&_*n[o]<=_*n[a];)a-=h;for(f=n[o],n[o]=n[a],n[a]=f,s=o+h,c=m;s>=g&&y>=s&&c>=g&&y>=c&&h*(c-s)>0;)f=n[s],n[s]=n[c],n[c]=f,d=d||b*s+v===n[s]||b*c+v===n[c],s+=h,c-=h;if("derangement"===r)for(a>=g&&y>=a&&(d=d||b*a+v===n[a]),c>=g&&y>=c&&(d=d||b*c+v===n[c]),d=d||b*o+v===n[o],s=o-h;!d&&s>=g&&y>=s;s-=h)d=b*s+v===n[s];else d=!1}else n=null}while(n&&d);else if("cyclic"===r)if(o=y-m,_*n[o]+ps;s++)f=(_*n[o]+p+b)%x,n[o]=_*f+p,o=(o+h)%x;else n=null;else if("involution"===r)n=null;else if("connected"===r)n=null;else do{for(d=!1,o=m-h;o>=g&&y>=o&&_*n[o]>=_*n[o+h];)o-=h;if(o>=g&&y>=o){for(a=m;a>=g&&y>=a&&h*(a-o)>0&&_*n[o]>=_*n[a];)a-=h;for(f=n[o],n[o]=n[a],n[a]=f,s=o+h,c=m;s>=g&&y>=s&&c>=g&&y>=c&&h*(c-s)>0;)f=n[s],n[s]=n[c],n[c]=f,d=d||b*s+v===n[s]||b*c+v===n[c],s+=h,c-=h;if("derangement"===r)for(a>=g&&y>=a&&(d=d||b*a+v===n[a]),c>=g&&y>=c&&(d=d||b*c+v===n[c]),d=d||b*o+v===n[o],s=o-h;!d&&s>=g&&y>=s;s-=h)d=b*s+v===n[s];else d=!1}else n=null}while(n&&d);return n}function hn(n,e,t,r,u){if(null==n)return null;var i,l=null;if("ordered"===u)for(l={},i=0;t>i;i++)l[n[i]]=1;return l}function _n(n,e,t,r,u,i){var l,o,a,s,c,f,d,m,h,_,p,b,v,g,y=e[1],x=e[0];if(d=0,m=y-1,f=1,c=m,h=1,_=0,p=1,b=0,Xn&u&&(h=-h,_=x-1-_,f=-f,c=m-c,p=-p,b=m-b),Pn&u&&(f=-f,c=m-c,p=-p,b=m-b),Nn&u&&(t=-t),0>t)if("ordered+repeated"===r){for(l=c;l>=d&&m>=l&&0===n[l];)l-=f;if(l>=d&&m>=l)for(x-=1,n[l]=n[l]-1,o=l+f;o>=d&&m>=o;o+=f)n[o]=x;else n=null}else if("ordered"===r){for(null==i&&(i=hn(n,x,y,u,r)),l=c,a=-1;-1===a&&l>=d&&m>=l;){if(h*n[l]+_-h>=0)for(o=h*n[l]+_-h;o>=0&&x>o;o-=h)if(s=h*o+_,null==i[s]){a=l,i[s]=1;break}i[n[l]]=null,l-=f}if(a>-1)for(n[a]=s,o=x-1-_,s=h*o+_,l=a+f;l>=d&&m>=l;l+=f){for(;o>=0&&x>o&&null!=i[s];)o-=h,s=h*o+_;n[l]=s,i[s]=1}else n=null}else if(g="repeated"===r,v=g?0:1,Xn&u)if(f=-f,c=m-c,p=-p,b=m-b,l=m-c,o=0>f?d:m,!g&&n[o]+1>y||g&&n[o]>0){if(g)for(;l>=d&&m>=l&&0===n[l];)l+=f;else for(;l>=d&&m>=l&&p*l+b===n[l];)l+=f;for(n[l]-=1,l-=f;l>=d&&m>=l;)n[l]=n[l+f]-v,l-=f}else n=null;else{for(a=-1,l=c;l-f>=d&&m>=l-f;l-=f)if(n[l]>n[l-f]+v){a=l;break}if(!(a>=d&&m>=a)&&0f?m:d]&&(a=0>f?m:d),a>=d&&m>=a){for(s=x-1+v,l=c;l>=d&&m>=l&&f*(l-a)>0;l-=f)s-=v,n[l]=s;n[a]--}else n=null}else if("ordered+repeated"===r){for(l=c;l>=d&&m>=l&&n[l]+1===x;)l-=f;if(l>=d&&m>=l)for(n[l]=n[l]+1,o=l+f;o>=d&&m>=o;o+=f)n[o]=0;else n=null}else if("ordered"===r){for(null==i&&(i=hn(n,x,y,u,r)),l=c,a=-1;-1===a&&l>=d&&m>=l;){if(h*n[l]+_+h=0&&x>o;o+=h)if(s=h*o+_,null==i[s]){a=l,i[s]=1;break}i[n[l]]=null,l-=f}if(a>-1)for(n[a]=s,o=_,s=h*o+_,l=a+f;l>=d&&m>=l;l+=f){for(;o>=0&&x>o&&null!=i[s];)o+=h,s=h*o+_;n[l]=s,i[s]=1}else n=null}else if(g="repeated"===r,v=g?0:1,Xn&u)if(f=-f,c=m-c,p=-p,b=m-b,l=m-c,!g&&n[l]+y=d&&m>=l+f&&n[l]+v===n[l+f];)n[l]=s,l+=f,s+=v;n[l]+=1}else n=null;else{if(g){for(a=-1,o=x-1,l=c;l>=d&&m>=l;l-=f)if(n[l]=d&&m>=l;l-=f)if(n[l]=d&&m>=a){if(s=n[a]+1,o=g?x-1:x-y+p*a+b,s===o)n[a]=s;else if(o>s)for(l=a;l>=d&&m>=l;l+=f)n[l]=s,s+=v}else n=null}return n}function pn(n,e,t,r,u,i,o){var a,s,c,f,d,m,h,_,p,b,v=e;if(p=0,b=n.length-1,_=1,c=p,f=b,Pn&i&&(_=-_,c=b-c,f=b-f),Nn&i&&(t=-t),0>t)if(r)n=null;else if(Xn&i)n=null;else if(s=u?c+_:c,s>=p&&b>=s&&n[s]>1){for(a=f,h=0;a>=p&&b>=a&&_*(a-s)>=0&&1===n[a];)h+=n[a],a-=_;m=n[a]-1,h++,n[a]=m,n=0>_?n.slice(a):n.slice(0,a+1),h>m?(s=h%m,n=0>_?(s?[s]:[]).concat(l(In.floor(h/m),m)).concat(n):n.concat(l(In.floor(h/m),m)).concat(s?[s]:[])):h>0&&(n=0>_?[h].concat(n):n.concat([h]))}else n=null;else if(r)n=null;else if(Xn&i)n=null;else if(u?(m=In.min(u,v-u),d=In.floor(v/u)+(v%u?1:0)-1,m=b>d||n[c+(d-1)*_]=p&&b>=s&&m){for(b>0?(a=f-_,h=n[f]):(a=f,h=0);a>=p&&b>=a&&a-_>=p&&b>=a-_&&_*(a-s)>0&&n[a]===n[a-_];)h+=n[a],a-=_;n[a]++,h--,n=h>0?0>_?l(h,1).concat(n.slice(a)):n.slice(0,a+1).concat(l(h,1)):0>_?n.slice(a):n.slice(0,a+1)}else n=null;return n}function bn(n,e,t,r,u,i,o){var a,s,c,f,d,m,h,_,p=e;if(h=0,_=n.length-1,m=1,s=h,c=_,Pn&i&&(m=-m,s=_-s,c=_-c),Nn&i&&(t=-t),0>t)if(r)if(u)n=null;else if(Xn&i)if(f=n[s],p-r+1>f){for(n[s]=1,a=s+m;a>=h&&_>=a&&1===n[a];)a+=m;n[a]--,a-m>=h&&_>=a-m&&(n[a-m]=1+f)}else n=null;else if(f=n[c],p-r+1>f){for(n[c]=1,a=c;a>=h&&_>=a&&1===n[a];)a-=m;n[a]--,a+m>=h&&_>=a+m&&(n[a+m]=1+f)}else n=null;else if(u)n=null;else if(Xn&i)n=null;else if(p>n.length){for(a=c,d=0;a>=h&&_>=a&&1===n[a];)a-=m,d++;f=n[a]-1,n[a]=f,d++,d>0&&(a+m>=h&&_>=a+m?(a+=m,n[a]=d,d=0,n=0>m?n.slice(a):n.slice(0,a+1)):n=0>m?l(d,1,0).concat(n):n.concat(l(d,1,0)))}else n=null;else if(r)if(u)n=null;else if(Xn&i)if(p-r+1>n[c]){for(a=s;a>=h&&_>=a&&1===n[a];)a+=m;f=n[a],n[a]=1,n[s]=f-1,a+m>=h&&_>=a+m&&n[a+m]++}else n=null;else if(p-r+1>n[s]){for(a=c;a>=h&&_>=a&&1===n[a];)a-=m;f=n[a],n[a]=1,n[c]=f-1,a-m>=h&&_>=a-m&&n[a-m]++}else n=null;else u?n=null:Xn&i?n=null:p>n[s]?(d=n[c],n[c-m]++,0>m?(n.shift(),n.unshift.apply(n,l(d-1,1,0))):(n.pop(),n.push.apply(n,l(d-1,1,0)))):n=null;return n}var vn,gn,yn,xn,An,wn,Mn={VERSION:"0.8.2"},In=Math,On="prototype",Cn="constructor",Ln=Array.prototype.slice,Sn=Object[On].hasOwnProperty,En=Object[On].toString,Dn=In.log2||function(n){return In.log(n)/In.LN2},Rn=/^\s+|\s+$/g,Un=(String.prototype.trim?function(n){return n.trim()}:function(n){return n.replace(Rn,"")},Object.create),kn=function(n,e){for(var t in e)Sn.call(e,t)&&(n[t]=e[t]);return n},$n=function(n,e){1===arguments.length&&(e=n,n=Object);var t=e[Cn];return Sn.call(e,"__static__")&&(t=kn(t,e.__static__),delete e.__static__),t[On]=kn(Un(n[On]),e),t},Nn=1,Pn=2,Tn=4,Xn=8,jn=16,qn=32,Jn=Tn|Xn|jn,Vn=Jn|qn|Nn|Pn;return C.mem1={},C.mem2={},C.mem3={},L.mem={},S.mem={},E.mem={},D.mem={},R.mem={},U.mem={},k.mem={},Mn.Class=$n,Mn.Options={MAXMEM:1e6,RANDOM:"index"},$.LEX=$.LEXICOGRAPHIC=Tn,$.COLEX=$.COLEXICOGRAPHIC=Xn,$.MINIMAL=$.GRAY=jn,$.RANDOM=qn,$.REV=$.ANTI=$.REVERSE=$.REVERSED=Nn,$.REF=$.REFLECT=$.REFLECTED=Pn,$.REVLEX=$.ANTILEX=$.REVERSELEXICOGRAPHIC=$.ANTILEXICOGRAPHIC=Tn|Nn,$.REFLEX=$.REFLECTEDLEXICOGRAPHIC=Tn|Pn,$.REVCOLEX=$.ANTICOLEX=$.REVERSECOLEXICOGRAPHIC=$.ANTICOLEXICOGRAPHIC=Xn|Nn,$.REFCOLEX=$.REFLECTEDCOLEXICOGRAPHIC=Xn|Pn,Mn.ORDER=$,Mn.Math={O:0,I:1,J:-1,N:function(n){return Mn.Arithmetic.add(Mn.Arithmetic.O,n)},V:function(n){return Mn.Arithmetic.sub(Mn.Arithmetic.O,n)},rnd:In.random,rndInt:function(n,e){return Mn.Math.round((e-n)*Mn.Math.rnd()+n)},equ:function(n,e){return n===e},gte:function(n,e){return n>=e},lte:function(n,e){return e>=n},gt:function(n,e){return n>e},lt:function(n,e){return e>n},inside:function(n,e,t,r){return r?n>=e&&t>=n:n>e&&t>n},clamp:function(n,e,t){return e>n?e:n>t?t:n},wrap:function(n,e,t){return e>n?t:n>t?e:n},wrapR:function(n,e){return 0>n?n+e:n},add:x,sub:function(n,e){return n-e},mul:A,div:function(n,e){return Mn.Math.floor(n/e)},mod:function(n,e){return n%e},pow:In.pow,shl:function(n,e){return n<>e},bor:function(n,e){return n|e},band:function(n,e){return n&e},xor:function(n,e){return n^e},abs:In.abs,min:In.min,max:In.max,floor:In.floor,ceil:In.ceil,round:In.round,num:function(n){return"number"==typeof n?Mn.Math.floor(n):parseInt(n,10)},val:function(n){return Mn.Math.floor(n.valueOf())},sum:w,product:M,pow2:I,exp:O,factorial:C,partitions:S,compositions:D,bell:U,catalan:R,fibonacci:k},Mn.Arithmetic={isDefault:function(){return 0===Mn.Arithmetic.O&&Mn.Arithmetic.add===x},O:0,I:1,J:-1,N:Mn.Math.N,V:Mn.Math.V,equ:Mn.Math.equ,gte:Mn.Math.gte,lte:Mn.Math.lte,gt:Mn.Math.gt,lt:Mn.Math.lt,inside:Mn.Math.inside,clamp:Mn.Math.clamp,wrap:Mn.Math.wrap,wrapR:Mn.Math.wrapR,add:Mn.Math.add,sub:Mn.Math.sub,mul:Mn.Math.mul,div:Mn.Math.div,mod:Mn.Math.mod,pow:Mn.Math.pow,shl:Mn.Math.shl,shr:Mn.Math.shr,bor:Mn.Math.bor,band:Mn.Math.band,xor:Mn.Math.xor,abs:Mn.Math.abs,min:Mn.Math.min,max:Mn.Math.max,floor:Mn.Math.floor,ceil:Mn.Math.ceil,round:Mn.Math.round,rnd:Mn.Math.rndInt,num:Mn.Math.num,val:Mn.Math.val},Mn.Util={array:l,operate:i,intersection:h,difference:_,union:p,search:y,complementation:a,reflection:s,reversion:c,gray:f,finitedifference:d,partialsum:m,sort:b,shuffle:v,pick:g,pluck:o},Mn.BitArray=$n({constructor:function Gn(n){var e=this;return e instanceof Gn?(e.length=n,void(e.bits=new Uint32Array(In.ceil(n/32)))):new Gn(n)},length:0,bits:null,dispose:function(){var n=this;return n.length=null,n.bits=null,n},clone:function(){var n=this,e=new Mn.BitArray(n.length);return e.bits=new Uint32Array(n.bits),e},fromArray:function(n){var e=this;return e.bits=new Uint32Array(n),e},toArray:function(){return Ln.call(this.bits)},toString:function(){var n,e,t=this.toArray();for(n=0,e=t.length;e>n;n++)t[n]=u(t[n]);return t.join("")},reset:function(){var n,e=this,t=e.bits,r=t.length;for(n=0;r>n;n++)t[n]=0;return e},isset:function(n){return!!(this.bits[n>>>5]&1<<(31&n))},set:function(n){var e=this;return e.bits[n>>>5]|=1<<(31&n),e},unset:function(n){var e=this;return e.bits[n>>>5]&=~(1<<(31&n)),e},toggle:function(n){var e=this;return e.bits[n>>>5]^=1<<(31&n),e}}),vn=Mn.CombinatorialIterator=$n({constructor:function Hn(n,e,t){var r=this,u=r[Cn],i=Mn.Arithmetic;r.name=n||"CombinatorialIterator",r.n=e||0,r.$=t=t||{},t.type=String(t.type||"default").toLowerCase(),t.order=t.order||Tn,t.base=t.base||0,t.minbase=null!=t.minbase?t.minbase:t.base,t.maxbase=null!=t.maxbase?t.maxbase:t.base,t.dimension=t.dimension||0,t.mindimension=null!=t.mindimension?t.mindimension:t.dimension,t.maxdimension=null!=t.maxdimension?t.maxdimension:t.dimension,t.rand=t.rand||{},t.count=u.count(r.n,r.$),t.first=i.O,t.last=i.gt(t.count,i.O)?i.sub(t.count,i.I):i.J,t.sub instanceof Hn&&(t.subcount=Mn.Arithmetic.mul(t.count,t.sub.total()),t.submethod=String(t.submethod||"project").toLowerCase(),t.subcascade=-1===t.subcascade?-1:1,"concatenate"===t.submethod?t.subdimension=t.dimension+t.sub.dimension():"compose"===t.submethod?t.subdimension=t.dimension*t.sub.dimension():t.subdimension=t.dimension),t.instance=r,r.order(t.order)},__static__:{Iterable:function Fn(n,e){var t=this;return t instanceof Fn?(e=-1===e?-1:1,void(t.next=function(){return n.hasNext(e)?{value:n.next(e)}:{done:!0}})):new Fn(n,e)},C:function(n,e){return a(n,n,e)},P:function(n){return s(n,n)},T:function(n,e){return c(n,e)},DUAL:function(n,e,t){if(null==n)return null;var r=this,u=r.C,i=r.P,l=(r.T,t&&null!=t.base?t.base:e),o=t&&null!=t.order?t.order:Tn;return n=qn&o?Pn&o?i(n):n:jn&o?Pn&o?i(n):n:Xn&o?Pn&o?u(n,l,t):i(u(n,l,t)):Pn&o?i(n):n},count:e,initial:e,succ:function(n,e,t,r,u,i){if(null==t||null==n||null==e)return null;var l=this,o=Mn.Arithmetic;r&&null!=r.order?r.order:Tn;return u=-1===u?-1:1,l.unrank(o.add(e,0>u?o.J:o.I),t,r)},rand:function(n,e){var t,r=this,u=Mn.Arithmetic,i=e&&null!=e.last?e.last:u.sub(r.count(n,e),u.I),l=u.O,o=u.rnd(l,i);return t=u.equ(l,o)?r.initial(n,e,1):u.equ(i,o)?r.initial(n,e,-1):r.unrank(o,n,e)},rank:e,unrank:e,cascade:function(n,e,t,r){if(-1===r){var u=n;n=e,e=u}return"concatenate"===t?n&&e?n.concat(e):n||e||null:"compose"===t?n&&e?N(!0,n,e):n||e||null:null==n||null==e?null:l(n.length,function(t){return n[t]t?(u.__item=i.rand(o,l),u.__index=s):(u._traversed&&u._traversed.dispose(),u._traversed=new Mn.BitArray(a.val(t)),e=u.random("index"),u._traversed.set(+e),u.__item=i.unrank(e,o,l),null!=u.__item&&(u.__index=e)),u._index=s):(u.__item=i.initial(o,l,n),null!=u.__item&&(u.__index=0>n?r:s,u._update()),u._index=u.__index),u._item=i.output(u.__item,u.__index,o,l),u._prev=qn&c||n>0?!1:null!=u.__item,u._next=0>n&&!(qn&c)?!1:null!=u.__item,u},_update:function(){return this.item__=null,this},order:function(n,e){if(!arguments.length)return this._order;var t,u,i,l,o,a=this,s=a[Cn],c=Mn.Arithmetic,f=(c.O,c.I,!0===n);return e=-1===e,i=a.n,l=a.$,a._traversed&&(a._traversed.dispose(),a._traversed=null),f?n=l.order:r(n)&&-1<(u=n.indexOf("|"))?(t=n.substr(u+1),n=$(n.substr(0,u))):t=n=$(n),o=e?-1:1,l.order=n,l.sub?(f?l.sub.rewind(o):l.sub.order(t,o),a.__subindex=l.sub.index(),a.__subitem=l.sub.next(o),a._subindex=null,a._subitem=null):(a.__subindex=null,a.__subitem=null,a._subindex=null,a._subitem=null),a._init(o),l.sub&&(a._prev=a._prev&&null!=a.__subitem,a._next=a._next&&null!=a.__subitem,a._subindex=c.add(c.mul(a.__subindex,l.count),a._index),a._subitem=s.cascade(a._item,a.__subitem,l.submethod,l.subcascade)),a},index:function(n,e){if(e=!0===e,!arguments.length)return this.$.sub?this._subindex:this._index;var t,r=this,u=r[Cn],i=Mn.Arithmetic,l=i.O,o=(i.I,i.J),a=r.n,s=r.$,c=s.sub&&!e?s.subcount:s.count,f=s.sub&&!e?r._subindex:r._index,d=s.order;return n=i.wrapR(i.N(n),c),!i.equ(n,f)&&i.inside(n,o,c)&&(c=s.count,t=s.last,s.sub&&!e&&(s.sub.index(i.div(n,c)),r.__subindex=s.sub.index(),r.__subitem=s.sub.item(),n=i.mod(n,c)),qn&d||(r.__index=n,r._index=n,r.__item=i.equ(l,n)?u.initial(a,s,1):i.equ(t,n)?u.initial(a,s,-1):u.unrank(n,a,s),r._update(),r._item=u.output(r.__item,r.__index,a,s),r._prev=null!=r.__item,r._next=null!=r.__item),s.sub&&(r._prev=r._prev&&null!=r.__subitem,r._next=r._next&&null!=r.__subitem,r._subindex=i.add(i.mul(r.__subindex,c),r._index),r._subitem=u.cascade(r._item,r.__subitem,s.submethod,s.subcascade))),r},item:function(n,e){if(!arguments.length)return this.$.sub?this._subitem:this._item;var u,i,l,o,a,s,c=this,f=c[Cn],d=c.n,m=c.$,h=m.sub?m.subcount:m.count,_=m.sub?c._subindex:c._index,p=Mn.Arithmetic,b=p.O,v=(p.I,p.J),g=null;return r(e)?-1<(s=e.indexOf("|"))?(g=e.substr(s+1),e=$(e.substr(0,s))):g=e=$(e):null!=e?g=e=$(e):(e=m.order,g=null),m.sub||(g=null),t(n)?(h=m.count,u=m.last,l=m.order,m.order=e,c.__item=n.slice(),c._update(), +c.__index=f.rank(c.__item,d,m),c._item=f.output(c.__item,c.__index,d,m),qn&e?c._index=c.__index:c._index=c.__index,c._prev=null!=c.__item,c._next=null!=c.__item,c):(n=p.wrapR(p.N(n),h),e===m.order&&null===g&&p.equ(n,_)?m.sub?c._subitem:c._item:p.inside(n,v,h)?(a=null,h=m.count,u=m.last,m.sub&&(a=m.sub.item(p.div(n,h),g),n=p.mod(n,h)),qn&e?(i=null,l=m.order,m.order=e,o=f.output(f.rand(d,m),i,d,m),m.order=l,m.sub&&(o=f.cascade(o,a,m.submethod,m.subcascade)),o):(i=n,l=m.order,m.order=e,o=f.output(p.equ(b,n)?f.initial(d,m,1):p.equ(u,n)?f.initial(d,m,-1):f.unrank(i,d,m),i,d,m),m.order=l,m.sub&&(o=f.cascade(o,a,m.submethod,m.subcascade)),o)):null)},random:function(n,e,t,r){var u,i=this,l=i[Cn],o=i.$,a=o.order;if(r=!0===r,"index"===n){var s,c,f=Mn.Arithmetic,d=f.N,m=f.O,h=f.I;return e===!!e&&(r=e,e=null,t=null),o.sub&&!r?(s=o.subcount,c=f.sub(s,h)):(s=o.count,c=o.last),null==e&&null==t?(e=m,t=c):null==t?(e=d(e||0),t=c):(e=d(e),t=d(t)),f.rnd(e,t)}return o.order|=qn,u=l.rand(i.n,o),o.order=a,u=l.output(u,null,i.n,o),o.sub&&!r?l.cascade(u,o.sub.random(),o.submethod,o.subcascade):u},rewind:function(n){var e=this;return e.order(!0,-1===n?-1:1)},hasNext:function(n){var e=this;return-1===n?qn&e.$.order?!1:e._prev:e._next},next:function(n){var e,t,r,u,i,l,o=this,a=o[Cn],s=Mn.Arithmetic,c=s.O,f=s.I,d=s.J,m=o.n,h=o.$,_=h.order,p=h.count,b=h.sub?o._subitem:o._item,v=null!=b;if(n=-1===n?-1:1,0>n&&qn&_)return null;if(e=0>n?d:f,qn&_)if(u=h.last,s.lt(o._index,u))if(t=o._traversed){for(r=o.random("index"),i=Mn.Math.rnd()>.5?d:f;t.isset(+r);)r=s.wrap(s.add(r,i),c,u);t.set(+r),o.__item=a.unrank(r,m,h),null!=o.__item&&(o.__index=r)}else o.__item=a.rand(m,h),o.__index=null;else o._item=o.__item=null,o._traversed&&(o._traversed.dispose(),o._traversed=null);else o.__item=a.succ(o.__item,o.__index,m,h,n,o.item__),null!=o.__item&&(o.__index=s.add(o.__index,e));return l=null!=o.__item,l?(o._index=s.add(o._index,e),null===o.__index&&(o.__index=o._index),0>n?(o._prev=l,o._next=v):(o._prev=v,o._next=l)):h.sub&&h.sub.hasNext(n)?(o.__subindex=h.sub.index(),o.__subitem=h.sub.next(n),o._init(n),l=null!=o.__item):(o.__subindex=null,o.__subitem=null,0>n?(o._prev=l,o._next=v):(o._prev=v,o._next=l)),o._item=a.output(o.__item,o.__index,m,h),h.sub&&(l=l&&null!=o.__subitem,o._subindex=l?s.add(s.mul(o.__subindex,p),o._index):null,o._subitem=l?a.cascade(o._item,o.__subitem,h.submethod,h.subcascade):null,0>n?o._prev=l:o._next=l),b},range:function(n,e){var t,r,u,l,o=this,a=Mn.Arithmetic,s=a.N,c=a.O,f=a.I,d=o.$,m=d.sub?d.subcount:d.count,h=d.sub?a.sub(m,f):d.last,_=1,p=arguments.length,b=!(qn&d.order);return 1>p?(n=c,e=h):2>p?(n=s(n),e=h):(n=s(n),e=s(e)),n=a.wrapR(n,m),e=a.wrapR(e,m),a.gt(n,e)&&(t=n,n=e,e=t,_=-1),n=a.clamp(n,c,h),b&&(e=a.clamp(e,c,h)),a.lte(n,e)?(l=[o.$.order,o.__index,o._index,o.__item&&o.__item.slice(),o._item,o.__subindex,o._subindex,o.__subitem,o._subitem,o._prev,o._next],b&&o.index(n),u=a.val(a.sub(e,n)),r=i(function(n,e,t){return n[t]=o.next(),n},new Array(u+1),null,0>_?u:0,0>_?0:u,0>_?-1:1),o.$.order=l[0],o.__index=l[1],o._index=l[2],o.__item=l[3],o._item=l[4],o.__subindex=l[5],o._subindex=l[6],o.__subitem=l[7],o._subitem=l[8],o._prev=l[9],o._next=l[10],o._update()):r=[],r},__iter__:function(){return new vn.Iterable(this)}}),"undefined"!=typeof Symbol&&"undefined"!=typeof Symbol.iterator&&(vn[On][Symbol.iterator]=vn[On].__iter__),gn=Mn.Tensor=$n(vn,{constructor:function Bn(){var n,e=this,r=Ln.call(arguments);if(n=!r.length||r[r.length-1]instanceof vn||t(r[r.length-1])||r[r.length-1]===+r[r.length-1]?{}:r.pop()||{},n.type=String(n.type||"tensor").toLowerCase(),n.order=n.order||Tn,r.length&&t(r[0])&&(r=r[0]),r&&r.length||(r=[]),!(e instanceof Bn))return new Bn(r,n);if("tuple"===n.type)r[0]=r[0]||1,r[1]=r[1]||1,r[0]instanceof vn?(n.sub=r[0],r[0]=n.sub.dimension()):r[1]instanceof vn&&(n.sub=r[1],r[1]=n.sub.base()),n.base=r[1],n.dimension=r[0];else{var u=i(function(n,e){return en[1]&&(n[1]=e),n},[1/0,0],r);n.base=r,n.minbase=u[0],n.maxbase=u[1],n.dimension=r.length}vn.call(e,"Tensor",r,n)},__static__:{C:vn.C,P:vn.P,T:vn.T,DUAL:vn.DUAL,count:function(n,e){var t=Mn.Arithmetic.O,r=e&&e.type?e.type:"tensor";return"tuple"===r?!n||0>=n[0]?t:Mn.Math.exp(n[1],n[0]):n&&n.length?Mn.Math.product(n):t},initial:function(n,e,t){var r,u=this,i=e&&e.type?e.type:"tensor",o=e&&e.order?e.order:Tn;return t=-1===t?-1:1,(Tn&o&&Nn&o||Xn&o&&!(Nn&o))&&(t=-t),r="tuple"===i?n[0]?0>t?l(n[0],n[1]-1,0):l(n[0],0,0):[]:n.length?0>t?l(n.length,function(e){return n[e]-1}):l(n.length,0,0):[],r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,i){return!t||!t[0]||0>=t[0]||null==n?null:(u=-1===u?-1:1,dn(n,t,u,r&&r.type?r.type:"tensor",r&&null!=r.order?r.order:Tn,i))},rand:function(n,e){var t,r=Mn.Math.rndInt,u=this,i=e&&e.type?e.type:"tensor";return t="tuple"===i?n[0]?l(n[0],function(e){return r(0,n[1]-1)}):[]:n.length?l(n.length,function(e){return r(0,n[e]-1)}):[],t=u.DUAL(t,n,e)},randu:vn.rand,rank:function(n,e,t){var r,u,i,l=this,o=Mn.Arithmetic,a=t&&null!=t.order?t.order:Tn,s=t&&t.type?t.type:"tensor",c=o.add,f=o.sub,d=o.mul,r=o.O,m=o.J;if(n=l.DUAL(n,e,t),"tuple"===s){if(u=e[0],!u)return m;for(e=e[1],i=0;u>i;i++)r=c(d(r,e),n[i])}else{if(u=e.length,!u)return m;for(i=0;u>i;i++)r=c(d(r,e[i]),n[i])}return(Tn&a&&Nn&a||Xn&a&&!(Nn&a))&&(r=f(t&&null!=t.last?t.last:f(l.count(e,t),o.I),r)),r},unrank:function(n,e,t){var r,u,i,l,o,a,s=this,c=Mn.Arithmetic,f=t&&null!=t.order?t.order:Tn,d=t&&t.type?t.type:"tensor",m=c.sub,h=c.mod,_=c.div,p=c.val;if((Tn&f&&Nn&f||Xn&f&&!(Nn&f))&&(n=m(t&&null!=t.last?t.last:m(s.count(e,t),c.I),n)),"tuple"===d){if(a=e[0],!a)return[];for(o=new Array(a),u=e[1],r=n,i=a-1;i>=0;i--)l=h(r,u),r=_(r,u),o[i]=p(l)}else{if(a=e.length,!a)return[];for(o=new Array(a),r=n,i=a-1;i>=0;i--)u=e[i],l=h(r,u),r=_(r,u),o[i]=p(l)}return o=s.DUAL(o,e,t)},cascade:vn.cascade,output:vn.output,gray:f,product:N,component:function(n,e){var t,r,u,i,l,o,a=[],s=e,c=s.length;for(t=0;c>t;t++)if(u=s[t],l=n[t],i=u[l],i instanceof Array)for(r=0,o=i.length;o>r;r++)a.push(i[r]);else a.push(i);return a}}}),yn=Mn.Permutation=$n(vn,{constructor:function Yn(n,e){var r=this;return r instanceof Yn?(e=e||{},e.type=String(e.type||"permutation").toLowerCase(),n=n||1,n instanceof vn&&(e.sub=n,n=e.sub.dimension()),e.base=e.dimension=n,e.rand={derangement:1,involution:1,connected:1},"multiset"===e.type&&(e.multiplicity=t(e.multiplicity)&&e.multiplicity.length?e.multiplicity.slice():l(n,1,0),e.multiplicity=e.multiplicity.concat(l(n-i(x,0,e.multiplicity),1,0)),e.base=e.multiplicity.length,e.multiset=G(e.multiplicity,n)),void vn.call(r,"Permutation",n,e)):new Yn(n,e)},__static__:{C:vn.C,P:vn.P,T:vn.T,DUAL:vn.DUAL,count:function(n,e){var t=Mn.Arithmetic.O,r=Mn.Math.factorial,u=e&&e.type?e.type:"permutation";return 0>=n?t:"cyclic"===u?Mn.Arithmetic.N(n):"multiset"===u?r(n,e.multiplicity):"derangement"===u?2>n?t:r(n,!1):"involution"===u?r(n,!0):r("connected"===u?n-1:n)},initial:function(n,e,t){var r,u=this,i=e&&e.type?e.type:"permutation",o=e&&null!=-e.order?e.order:Tn;if(0>=n)return null;if(t=-1===t?-1:1,(Tn&o&&Nn&o||Xn&o&&!(Nn&o))&&(t=-t),"cyclic"===i)r=0>t?[n-1].concat(l(n-1,0,1)):l(n,0,1);else if("derangement"===i){if(2>n)return null;if(1&n){var a=In.floor(n/2);r=0>t?l(n-a-1,n-1,-1).concat([a-1,a]).concat(l(a-1,a-2,-1)):l(n-3,function(n){return 1&n?n-1:n+1}).concat([n-2,n-1,n-3])}else r=0>t?l(n,n-1,-1):l(n,function(n){return 1&n?n-1:n+1})}else r="multiset"===i?0>t?e.multiset.slice().reverse():e.multiset.slice():"connected"===i?null:0>t?l(n,n-1,-1):l(n,0,1);return r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,i){if(!t||0>=t||null==n)return null;var l=r&&r.type?r.type:"permutation";return u=-1===u?-1:1,mn(n,t,u,l,r&&null!=r.order?r.order:Tn,r&&null!=r.base?r.base:null,i)},rand:function(n,e){var t,r=Mn.Math.rndInt,u=this,i=e&&e.type?e.type:"permutation";if("cyclic"===i){var o=r(0,n-1);t=o>0?l(n-o,o,1).concat(l(o,0,1)):l(n,0,1)}else if("derangement"===i){t=new Array(n);var a,s,c,f=!1;do{for(a=0;n>a;a++)t[a]=a;for(a=n-1,f=!1;a>=0;){if(c=r(0,a),t[c]===a){f=!0;break}s=t[a],t[a]=t[c],t[c]=s,a--}f=f||0===t[0]}while(f)}else if("involution"===i){t=l(n,0,1);for(var d,m,h,_,s,p,b=Mn.Math.rnd,g=.5,y=1,x=n,A=[1].concat(l(n-1,function(){var n=g;return y+=1,g=1/(1+y*g),n})),w=l(n,0,1);x>=2;)d=x-1,m=w[d],--x,g=A[x],s=b(),s>g&&(h=r(0,x-1),_=w[h],--x,p=w[h],w[h]=w[x],w[x]=p,p=t[_],t[_]=t[m],t[m]=p)}else t="multiset"===i?v(e.multiset.slice()):"connected"===i?v(l(n,0,1),!0):v(l(n,0,1));return t=u.DUAL(t,n,e)},randu:vn.rand,rank:function(n,t,r){var u,i,l,o,a,s=this,c=Mn.Arithmetic,f=r&&r.type?r.type:"permutation",d=r&&null!=-r.order?r.order:Tn,m=c.sub,h=c.add,_=c.mul,p=c.div,b=c.O,v=c.I;if(t=t||n.length,!t)return c.J;if(n=s.DUAL(n,t,r),"cyclic"===f)b=c.NUM(n[0]);else{if("derangement"===f||"involution"===f||"connected"===f)return e();if("multiset"===f)for(a=r.multiplicity.slice(),o=r&&null!=r.count?r.count:Mn.Math.factorial(t,a),l=t-1,u=0;l>u&&c.gt(o,v);u++)i=n[u],b=h(b,p(_(o,w(a,0,i-1,1)),t-u)),o=p(_(o,a[i]),t-u),a[i]--;else for(n=K(null,n),l=t-1,u=0;l>u;u++)b=h(_(b,t-u),n[u])}return(Tn&d&&Nn&d||Xn&d&&!(Nn&d))&&(b=m(r&&null!=r.last?r.last:m(s.count(t,r),v),b)),b},unrank:function(n,t,r){var u,i,o,a,s,c,f,d,m=this,h=Mn.Arithmetic,_=r&&r.type?r.type:"permutation",p=r&&null!=-r.order?r.order:Tn,b=h.mod,v=h.div,g=h.mul,y=h.sub,x=h.val;if(!t)return[];if((Tn&p&&Nn&p||Xn&p&&!(Nn&p))&&(n=y(r&&null!=r.last?r.last:y(m.count(t,r),h.I),n)),"cyclic"===_)n=x(n),u=l(t,function(e){return(n+e)%t});else{if("derangement"===_||"involution"===_||"connected"===_)return e();if("multiset"===_)for(d=r.multiplicity.slice(),u=l(t),f=r&&null!=r.count?r.count:Mn.Math.factorial(t,d),o=0;t>o;o++){for(s=0,a=0,i=x(v(g(n,t-o),f));a=0;o--)s=t-o,c=b(i,s),i=v(i,s),u[o]=x(c);Q(u,u)}}return u=m.DUAL(u,t,r)},cascade:vn.cascade,output:vn.output,permute:function(n,e,t){var r,u,i,o=n.length;for(!0===t?(u=l(o),i=n):(u=n,i=n.slice()),r=0;o>r;r++)u[r]=i[e[r]];return u},shuffle:function(n,e){if("cyclic"===e){var t=n.length,r=Mn.Math.rndInt(0,t-1);return r>0&&n.push.apply(n,n.splice(0,r)),n}return"connected"===e?v(n,!0):v(n)},compose:function(){return tn(Ln.call(arguments))},concatenate:function(){return rn(Ln.call(arguments))},cycles:function(n,e){return-1===e?cycles2permutation(n):Z(n)},swaps:function(n,e){return-1===e?en(n):nn(n)},inversion:function(n,e){return-1===e?Q(null,n):K(null,n)},inverse:function(n){return z(null,n)},multiset:function(n,e,t){return n===+n?G(e,n,-1===t?-1:1):-1===t?B(n):Y(n,e)},matrix:function(n,e,t){return-1===t?F(null,n,e):H(null,n,e)},parity:e,is_permutation:un,is_identity:ln,is_cyclic:cn,is_derangement:sn,is_involution:on,is_kthroot:an,is_connected:fn}}),xn=Mn.Combination=$n(vn,{constructor:function zn(n,e,r){var u=this;if(!(u instanceof zn))return new zn(n,e,r);if(t(n)?(r=e||{},e=n[1]||1,n=n[0]||1):(r=r||{},n=n||1,e=e||1),r.type=String(r.type||"unordered").toLowerCase(),-1t?l(n[1],n[0]-1,0):l(n[1],0,0):"ordered"===i?0>t?l(n[1],n[0]-1,-1):l(n[1],0,1):0>t?l(n[1],n[0]-n[1],1):l(n[1],0,1),r=u.DUAL(r,n,e)},succ:function(n,e,t,r,u,i){return!t||!t[0]||0>=t[0]||null==n?null:(u=-1===u?-1:1,_n(n,t,u,r&&r.type?r.type:"unordered",r&&null!=r.order?r.order:Tn,i))},rand:function(n,e){var t,r,u,i,o=this,a=e&&e.type?e.type:"unordered",s=n[1],c=Mn.Math.rndInt;return n=n[0],r=n-s,u=n-1,"repeated"===a||"ordered+repeated"===a?(t=1===s?[c(0,u)]:l(s,function(){return c(0,u)}),s>1&&"repeated"===a&&b(t)):"ordered"===a?(i={},t=1===s?[c(0,u)]:n===s?v(l(s,0,1)):l(s,function(){for(var e=c(0,u);1===i[e];)e=(e+1)%n;return i[e]=1,e})):(i={},t=1===s?[c(0,u)]:n===s?l(s,0,1):s>r?_(null,n,b(l(r,function(){for(var e=c(0,u);1===i[e];)e=(e+1)%n;return i[e]=1,e}))):b(l(s,function(){for(var e=c(0,u);1===i[e];)e=(e+1)%n;return i[e]=1,e}))),t=o.DUAL(t,n,e)},randu:vn.rand,rank:function(n,e,t){var r,u,i,l,o,a=this,s=Mn.Arithmetic,c=s.add,f=s.sub,d=s.mul,m=s.O,h=s.I,_=m,p=e[1],b=t&&null!=t.order?t.order:Tn,v=t&&t.type?t.type:"unordered",g=Mn.Math.factorial;if(n=a.DUAL(n,e,t),"ordered+repeated"===v)for(l=e[0],r=0;p>r;r++)_=c(d(_,l),n[r]);else if("repeated"===v){for(l=e[0]+p-1,o=t&&t.count?t.count:g(l,p),r=1;p>=r;r++)u=l-1-n[r-1]-r+1,i=p+1-r,u>=i&&(_=c(_,g(u,i)));_=f(f(o,h),_)}else if("ordered"===v)for(l=e[0],n=K(null,n,l),r=0;p>r;r++)_=c(d(_,l-r),n[r]);else{for(l=e[0],o=t&&t.count?t.count:g(l,p),r=1;p>=r;r++)u=l-1-n[r-1],i=p+1-r,u>=i&&(_=c(_,g(u,i)));_=f(f(o,h),_)}return(Tn&b&&Nn&b||Xn&b&&!(Nn&b))&&(_=f(t&&null!=t.last?t.last:f(a.count(e,t),h),_)),_},unrank:function(n,e,t){var r,u,i,o,a,s,c,f=this,d=Mn.Arithmetic,m=d.O,h=d.I,_=d.sub,p=d.div,b=d.mod,v=d.mul,g=d.lte,y=d.gt,x=d.val,A=e[1],w=t&&t.type?t.type:"unordered",M=t&&null!=t.order?t.order:Tn;if(e=e[0],(Tn&M&&Nn&M||Xn&M&&!(Nn&M))&&(n=_(t&&null!=t.last?t.last:_(f.count(e,t),d.I),n)),r=l(A),"ordered+repeated"===w)for(o=n,s=A-1;s>=0;s--)a=b(o,e),o=p(o,e),r[s]=x(a);else if("ordered"===w){for(o=n,s=A-1;s>=0;s--)i=e-s,a=b(o,i),o=p(o,i),r[s]=x(a);Q(r,r,i)}else{c="repeated"===w,i=c?e+A-1:e,u=t&&t.count?t.count:Mn.Math.factorial(i,A),n=_(_(u,h),n),u=p(v(u,i-A),i),a=i-A+1,o=A,s=i-1;do g(u,n)?(r[A-o]=c?i-a-A+1:i-a-o+1,y(u,m)&&(n=_(n,u),u=p(v(u,o),s)),o--,s--):(u=p(v(u,s-o),s),a--,s--);while(o>0)}return r=f.DUAL(r,e,t)},cascade:vn.cascade,output:function(n,e,t,r){return null==n?null:n=r&&"binary"===r.type?P(n,t[0]):n.slice()},complement:function(n,e,t){return!0===t?v(_(null,e,b(n))):_(null,e,n)},binary:function(n,e,t){return-1===t?T(n,e):P(n,e)},pick:function(n,e,t){return e>0&&n.length?g(n,e,"ordered+repeated"!==t&&"ordered"!==t,"ordered+repeated"===t||"repeated"===t,new Array(e)):[]},choose:function(n,e){return l(e.length,function(t){return n[e[t]]})}},_update:function(){var n=this;return n.item__=hn(n.__item,n.n[0],n.n[1],n.$.order,n.$.type),n}}),xn.conjugate=xn.complement,xn.project=xn.choose,An=Mn.Powerset=Mn.Subset=$n(vn,{constructor:function Kn(n,e){var t=this;return t instanceof Kn?(e=e||{},e.type=e.type||"subset",n=n||1,n instanceof vn&&(e.sub=n,n=e.sub.base()),e.base=n,e.dimension=n,e.mindimension=0,e.maxdimension=n,void vn.call(t,"Subset",n,e)):new Kn(n,e)},__static__:{C:function(n,e){return _(null,e,n)},P:vn.P,T:vn.T,DUAL:function(n,e,t){if(null==n)return null;var r=this,u=r.C,i=r.P,l=(r.T,t&&null!=t.order?t.order:Tn);return n=qn&l?Pn&l?n:i(n):jn&l?Pn&l?n:i(n):Xn&l?Pn&l?i(u(n,e)):u(n,e):Pn&l?n:i(n)},count:function(n,e){return Mn.Math.pow2(n)},initial:function(n,e,t){var r,u=this,i=e&&null!=e.order?e.order:Tn;return t=-1===t?-1:1,(Tn&i&&Nn&i||Xn&i&&!(Nn&i))&&(t=-t),r=0>t?l(n,0,1):[],r=u.DUAL(r,n,e)},succ:vn.succ,rand:function(n,e){for(var t,r=this,u=Mn.Math.rndInt,i=null,o=n-1;o>=0;o--)u(0,1)&&(i={len:i?i.len+1:1,k:o,next:i});return t=i?l(i.len,function(n){var e=i.k;return i=i.next,e}):[],t=r.DUAL(t,n,e)},randu:vn.rand,rank:function(n,e,t){var r=this,u=Mn.Arithmetic,i=u.O,l=u.I,o=u.add,a=u.shl,s=u.sub,c=t&&null!=t.order?t.order:Tn,f=i,d=0,m=n.length;for(n=r.DUAL(n,e,t);m>d;)f=o(f,a(l,n[d++]));return(Tn&c&&Nn&c||Xn&c&&!(Nn&c))&&(f=s(t&&null!=t.last?t.last:s(r.count(e,t),l),f)),f},unrank:function(n,e,t){var r=this,u=Mn.Arithmetic,i=u.O,l=u.J,o=u.band,a=u.shr,s=u.gt,c=u.sub,f=t&&null!=t.order?t.order:Tn,d=[],m=0,h=t&&t.count?t.count:r.count(e,t);if(!u.inside(n,l,h))return null;for((Tn&f&&Nn&f||Xn&f&&!(Nn&f))&&(n=c(t&&null!=t.last?t.last:c(h,u.I),n));s(n,i);)s(o(n,1),i)&&d.push(m),s(o(n,2),i)&&d.push(m+1),s(o(n,4),i)&&d.push(m+2),s(o(n,8),i)&&d.push(m+3),s(o(n,16),i)&&d.push(m+4),s(o(n,32),i)&&d.push(m+5),s(o(n,64),i)&&d.push(m+6),s(o(n,128),i)&&d.push(m+7),m+=8,n=a(n,8);return d=r.DUAL(d,e,t)},cascade:vn.cascade,output:function(n,e,t,r){if(null==n)return null;var u=n;return r&&"binary"===r.type&&(n=P(n,t)),u===n?n.slice():n},binary:function(n,e,t){return-1===t?T(n,e):P(n,e)}}}),wn=Mn.Partition=$n(vn,{constructor:function Qn(n,e){var t=this;if(!(t instanceof Qn))return new Qn(n,e);e=e||{},e.type=e.type||"partition",n=n||1,n instanceof vn&&(e.sub=n,n=e.sub.base());var r=e["max="]?0|e["max="]:null,u=e["parts="]?0|e["parts="]:null,i=u?u:r?n-r+1:n,l=u?u:r?In.ceil(n/r):1;e.base=n,e.dimension=u||n,e.mindimension=In.min(l,i),e.maxdimension=In.max(l,i),e.rand={partition:1,composition:1,packed:1},vn.call(t,"Partition",n,e)},__static__:{C:function(n,e,t){var r=t&&t.type?t.type:"partition",u=t&&null!=t.order?t.order:Tn,i=t&&t["max="]?0|t["max="]:null,l=t&&t["parts="]?0|t["parts="]:null;return"composition"!==r&&Xn&u&&s(n,n),l||i?n:q("composition"===r,n,1)},P:vn.P,T:vn.T,DUAL:vn.DUAL,count:function(n,e){var t=e&&e["max="]?0|e["max="]:null,r=e&&e["parts="]?0|e["parts="]:null,u=e&&e.type?e.type:"partition";return"composition"===u?Mn.Math.compositions(n,r,t):Mn.Math.partitions(n,r,t)},initial:function(n,e,t){var r,u,i,o=this,a=e&&e.type?e.type:"partition",c=e&&e["max="]?0|e["max="]:null,f=e&&e["parts="]?0|e["parts="]:null,d=e&&null!=e.order?e.order:Tn,m="composition"===a;return 0>=n||f&&c&&(f+c>n+1||n>f*c)||f&&f>n||c&&c>n?null:(t=-1===t?-1:1,(Tn&d&&Nn&d||Xn&d&&Nn&d)&&(t=-t),f&&c?(1===f&&(r=[c]),m?(i=In.min(c,In.ceil((n-c)/(f-1))),r=[(n-c)%i||i].concat(l(f-2,i,0)).concat([c]),0>t&&s(r,r)):0>t?(u=In.min(f,In.floor(n/c)||1),n-=u*c,f-=u,0===n&&f>0&&(u--,f++,n+=c),r=[c].concat(l(u-1,c,0)).concat(n>0&&f>0?[n-f+1].concat(l(f-1,1,0)):[])):(i=In.min(c,In.ceil((n-c)/(f-1))),r=[c].concat(l(f-2,i,0).concat([(n-c)%i||i])))):f?m?(r=l(f-1,1,0).concat([n-f+1]),0>t&&s(r,r)):(i=In.ceil(n/f),u=n%i||i,r=0>t?[n-f+1].concat(l(f-1,1,0)):l(f-1,i,0).concat([u])):c?m?(r=l(n-c,1,0).concat([c]),0>t&&s(r,r)):(u=In.floor(n/c),i=n%c,r=0>t?l(u,c,0).concat(i?[i]:[]):[c].concat(l(n-c,1,0))):r=0>t?[n]:l(n,1,0),r=o.DUAL(r,n,e))},succ:function(n,e,t,r,u,i){if(null==t||null==n)return null;var l=r&&r.type?r.type:"partition",o=r&&r["max="]?0|r["max="]:null,a=r&&r["parts="]?0|r["parts="]:null;return 0>=t||a&&o&&(a+o>t+1||t>a*o)||a&&a>t||o&&o>t?null:(u=-1===u?-1:1,"composition"===l?bn(n,t,u,a,o,r&&null!=r.order?r.order:Tn,i):pn(n,t,u,a,o,r&&null!=r.order?r.order:Tn,i))},rand:function(n,e){var t,r,u,i,o,a=this,s=Mn.Math.rndInt,c=e&&e.type?e.type:"partition",f=(e&&null!=e.order?e.order:Tn,e&&e["max="]?0|e["max="]:null),d=e&&e["parts="]?0|e["parts="]:null,m=0,h=0,_="composition"===c,p=!1;if(0>=n||d&&f&&(d+f>n+1||n>d*f)||d&&d>n||f&&f>n)return null;if(d&&f)return null;if(f&&!d&&(d=f,p=!0),d)1===d?(r=[n],h=n):n===d?(r=l(d,1,0),h=n):(t={},u=n-2,l(r=b(l(d-1,function(){for(o=s(0,u);1===t[o];)o=(o+1)%(u+1);return t[o]=1,o+1})),function(n){return o=r[n],i=o-m,m=o,h+=i,i}));else{for(t=null,o=1;n>o;o++)s(0,1)&&(i=o-m,m=o,h+=i,t={len:t?t.len+1:1,x:i,next:t});r=t?l(t.len,function(){return i=t.x,t=t.next,i}):[]}return n>h&&(r.length?r.splice(s(0,r.length-1),0,n-h):r.push(n-h)),_?p&&(r=v(q(0,b(r,-1)))):(r=b(r,-1),p&&(r=q(0,r))),r=a.DUAL(r,n,e)},randu:vn.rand,rank:e,unrank:e,cascade:vn.cascade,output:function(n,e,t,r){if(null==n)return null;var u=n,i=r&&r.type?r.type:"partition",l=r&&null!=r.order?r.order:Tn,o=Pn&l?-1:1;r&&r["max="]?0|r["max="]:null,r&&r["parts="]?0|r["parts="]:null;return"composition"===i||"packed"===i&&(n=J(n,o)),u===n?n.slice():n},conjugate:function(n,e){return q("composition"===e,n)},subset:function(n,e){return-1===e?j(n):X(n)},pack:function(n,e){return-1===e?V(n):J(n)}}}),wn.transpose=wn.conjugate,Mn}); \ No newline at end of file