fork download
  1. function twoLines(a,b,c,d,t) {
  2. let l1o = b - a
  3. let l2o = d - c
  4. let r = 0
  5. for(let l1=1;l1<=l1o;l1++) {
  6. for(let i=0;i<=(l1o-l1);i++) {
  7. let a_ = a + i
  8. let b_ = a_ + l1
  9. let l2 = (l1o + l2o - l1)
  10. for(let c_ = Math.max(b_ + 1, c - (l2 - l2o)); c_<=c && c_+l2<=t;c_++) {
  11. let d_ = c_ + l2
  12. //console.log(`${a_}--${b_} (${b_-a_}) ${c_}--${d_} (${d_ - c_})`)
  13. r++
  14. }
  15. }
  16. }
  17. return r
  18. }
  19. let mul = 1500
  20. let abcd = Array.from({length:4}).map((e,i) => Math.floor(mul * i + Math.random()*mul))
  21. let r = twoLines(...abcd,mul*4)
  22. console.log(`a,b,c,d = ${abcd.join(',')} -> ${r} перестановок`)
Success #stdin #stdout 0.43s 17380KB
stdin
Standard input is empty
stdout
a,b,c,d = 564,2357,3552,5832 -> 248432704 перестановок