From 463ae4285a8041f751d48a882e83e398c2db24b6 Mon Sep 17 00:00:00 2001 From: Raghav Agarwal Date: Fri, 2 Oct 2020 16:28:00 +0530 Subject: [PATCH] added solution to problem Bytelandian gold coins of codechef with explanation --- .../code.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DP/Bytelandian_gold_coins_problem_codechef/code.cpp diff --git a/DP/Bytelandian_gold_coins_problem_codechef/code.cpp b/DP/Bytelandian_gold_coins_problem_codechef/code.cpp new file mode 100644 index 00000000..79031422 --- /dev/null +++ b/DP/Bytelandian_gold_coins_problem_codechef/code.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +#define ull unsigned long long int +/* +Problem Statement:In Byteland they have a very strange monetary system. + +Each Bytelandian gold coin has an integer number written on it. A coin n can be exchanged in a bank into three coins: n/2, n/3 and n/4. +But these numbers are all rounded down (the banks have to make a profit). + +You can also sell Bytelandian coins for American dollars. The exchange rate is 1:1. But you can not buy Bytelandian coins. + +You have one gold coin. What is the maximum amount of American dollars you can get for it? + +Example: input = 12 output = 13 + -> you can change 12 into 6 , 4 , 3 which sums up to 13. + +Explanation: + let input = n + so output = max(n,( (output of n/2) + (output of n/3) + (output of n/4) )) + to store the output of n/2 , n/3 , n/4 , i use an array to reduce the time for multiple calculation of output of n/2 , n/3 , n/4 in sub steps. + +*/ + +ull check(ull n,ull *a) +{ + if(n<12) + return n; + if(a[n]) + return a[n]; + return a[n]= max(n,check((n/2),a)+check((n/3),a)+check((n/4),a)); + +} +int main() +{ + ull n; + while(cin>>n) + { + ull *a = new ull[n+1]; + ull ans= check(n,a); + cout<