Skip to content

added solution to problem Bytelandian gold coins of codechef with exp… #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions DP/Bytelandian_gold_coins_problem_codechef/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<bits/stdc++.h>
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<<ans<<endl;
}
return 0;
}