Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/big"
- )
- var big_zero = big.NewInt(0)
- var big_one = big.NewInt(1)
- func count_exchanges(sum int, coins []int) *big.Int {
- cache := make([]*big.Int, (sum + 1) * (len(coins) + 1))
- var internal_count func (sum int, coin_index int) *big.Int
- internal_count = func (sum int, coin_index int) *big.Int {
- switch {
- case sum < 0: return big_zero
- case sum == 0: return big_one
- case coin_index < 0 || coin_index >= len(coins): return big_zero
- }
- cached_index := sum + (sum + 1) * coin_index
- cached_value := cache[cached_index]
- if cached_value != nil {
- return cached_value
- }
- for coin_index < len(coins) && sum < coins[coin_index] {
- coin_index++
- }
- result := big.NewInt(0).Add(
- internal_count(sum - coins[coin_index], coin_index),
- internal_count(sum, coin_index + 1))
- cache[cached_index] = result
- return result
- }
- return internal_count(sum, 0)
- }
- func main() {
- fmt.Printf("%d\n", count_exchanges(1000000, []int { 50, 25, 10, 5, 1 }))
- }
Add Comment
Please, Sign In to add comment