// Reproduce with Node.js. BigInt keeps all counts exact. function choose(n, k) { let value = 1n; for (let j = 1; j <= k; j++) value = value * BigInt(n - j + 1) / BigInt(j); return value; } function formula(limit) { const d = [1n]; for (let n = 1; n <= limit; n++) { let value = 2n ** BigInt(n); for (let j = 2; j <= n; j++) value += choose(2*n, 2*j) * BigInt(j-1) * 2n ** BigInt(j) * d[n-j]; d.push(value); } return d; } // Independent subset DP. Store the used labels and the unordered last // column. The factor two at each step orders the entries within a column. function fromTables(n) { const labels = 2*n, pairs = []; for (let a = 0; a < labels; a++) for (let b = a+1; b < labels; b++) pairs.push([a,b,(1<c)+Number(a>d)+Number(b>c)+Number(b>d); sum += (inversions % 2 ? -2n : 2n) * extend(mask|bits, i); } memo.set(key,sum); return sum; } return pairs.reduce((s,p,i) => s+2n*extend(p[2],i), 0n); } const values = formula(9); for (let n=1; n<=6; n++) { const check = fromTables(n); if (values[n] !== check) throw Error("Mismatch at " + n); } console.log(values.map(String).join(", "));