Created
April 14, 2017 19:40
-
-
Save json2d/c3275ea3560d27a81261324b1a1ae10d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const solveWith = require('./lib/solve-with') | |
const bigInt = require('jsbn').BigInteger | |
var stall = (input) => { | |
var [N,K] = input.split(' ') | |
// N = new bigInt(N) | |
K = new bigInt(K) | |
var empties = {[N]: bigInt.ONE} | |
while(K.compareTo(bigInt.ZERO) > 0) { | |
var best = Object.keys(empties).reduce((longestChain,chain) => longestChain.max(new bigInt(chain)),bigInt.ZERO) | |
var count = empties[best] | |
delete empties[best] | |
var left = best.subtract(bigInt.ONE); | |
var a = left.divide(new bigInt('2')); | |
var b = left.subtract(a); | |
var max = a.max(b) | |
var min = a.min(b) | |
console.log('count:',''+count,'max:',''+max,'min:',''+min) | |
if(K.compareTo(count) <= 0) { | |
return max + " " + min | |
} | |
K = K.subtract(count); | |
empties[max]===undefined ? empties[max] = count : empties[max] = empties[max].add(count); | |
empties[min]===undefined ? empties[min] = count : empties[min] = empties[min].add(count); | |
} | |
console.error('no solution') | |
} | |
solveWith(stall,process.argv[2],process.argv[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment