pre-calculation approach for exchange number problem