
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to count number of operations needed to reach n by paying coins
Suppose we have five numbers, N, A, B, C, D. We start with a number 0 and end at N. We can change a number by certain number of coins with the following operations −
- Multiply the number by 2, paying A coins
- Multiply the number by 3, paying B coins
- Multiply the number by 5, paying C coins
- Increase or decrease the number by 1, paying D coins.
We can perform these operations any number of times in any order. We have to find the minimum number of coins we need to reach N
So, if the input is like N = 11; A = 1; B = 2; C = 2; D = 8, then the output will be 19, because Initially x is 0.
For 8 coins to increase x by 1 (x=1).
For 1 coin, multiply x by 2 (x=2).
For 2 coins, multiply x by 5 (x=10).
For 8 coins, increase it by 1 (x=11).
Steps
To solve this, we will follow these steps −
Define one map f for integer type key and value Define one map vis for integer type key and Boolean type value Define a function calc, this will take n if n is zero, then: return 0 if n is in vis, then: return f[n] vis[n] := 1 res := calc(n / 2) + n mod 2 * d + a if n mod 2 is non-zero, then: res := minimum of res and calc((n / 2 + 1) + (2 - n mod 2)) * d + a) res := minimum of res and calc(n / 3) + n mod 3 * d + b if n mod 3 is non-zero, then: res := minimum of res and calc((n / 3 + 1) + (3 - n mod 3)) * d + b) res := minimum of res and calc(n / 5) + n mod 5 * d + c if n mod 5 is non-zero, then: res := minimum of res and calc((n / 5 + 1) + (5 - n mod 5)) if (res - 1) / n + 1 > d, then: res := n * d return f[n] = res From the main method, set a, b, c and d, and call calc(n)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int a, b, c, d; map<long, long> f; map<long, bool> vis; long calc(long n){ if (!n) return 0; if (vis.find(n) != vis.end()) return f[n]; vis[n] = 1; long res = calc(n / 2) + n % 2 * d + a; if (n % 2) res = min(res, calc(n / 2 + 1) + (2 - n % 2) * d + a); res = min(res, calc(n / 3) + n % 3 * d + b); if (n % 3) res = min(res, calc(n / 3 + 1) + (3 - n % 3) * d + b); res = min(res, calc(n / 5) + n % 5 * d + c); if (n % 5) res = min(res, calc(n / 5 + 1) + (5 - n % 5) * d + c); if ((res - 1) / n + 1 > d) res = n * d; return f[n] = res; } int solve(int N, int A, int B, int C, int D){ a = A; b = B; c = C; d = D; return calc(N); } int main(){ int N = 11; int A = 1; int B = 2; int C = 2; int D = 8; cout << solve(N, A, B, C, D) << endl; }
Input
11, 1, 2, 2, 8
Output
19
- Related Articles
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to count number of minimum coins needed to get sum k
- C++ program to count expected number of operations needed for all node removal
- Program to find number of operations needed to decrease n to 0 in C++
- Program to find number of combinations of coins to reach target in Python
- C++ Program to count number of operations needed to place elements whose index is smaller than value
- Program to find number of coins needed to make the changes with given set of coins in Python
- Program to find number of coins needed to make the changes in Python
- C++ program to count minimum number of binary digit numbers needed to represent n
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Finding minimum number of required operations to reach n from m in JavaScript
- Program to find number of given operations required to reach Target in Python
- C++ program to count at least how many operations needed for given conditions
- C++ program to find minimum number of punches are needed to make way to reach target

Advertisements