Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 15, 2018 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/95f98d7e8ba069c270095a36064dfb9e to your computer and use it in GitHub Desktop.
Save completejavascript/95f98d7e8ba069c270095a36064dfb9e to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
const int MAX = 10000000;
unsigned long int store[MAX];
unsigned long int coins(unsigned long int n)
{
unsigned long int a=n/2, b=n/3, c=n/4;
// Nếu đổi thành 3 đồng xu nhỏ hơn
// mà giá trị thu được lớn hơn thì đổi
if (a + b + c > n)
{
unsigned long int x,y,z;
// Nếu giá trị đổi ra mà >= MAX hoặc < MAX
// mà nó chưa được tính thì ta phải tính.
// Ngược lại thì ta lấy luôn giá trị lưu trong mảng.
if(a >= MAX || store[a] == 0)
{
x = coins(a);
if(a < MAX) store[a] = x;
}
else x = store[a];
if(b >= MAX || store[b] == 0)
{
y = coins(b);
if(b < MAX) store[b] = y;
}
else y = store[b];
if(c >= MAX || store[c] == 0)
{
z = coins(c);
if(c < MAX) store[c] = z;
}
else z = store[c];
return x + y + z;
}
// Nếu không thì đổi luôn thành đồng xu.
return n;
}
int main()
{
//freopen("input.txt","r",stdin);
unsigned long int n;
for(int i = 0; i < MAX; i++)
store[i] = 0;
while(cin >> n)
{
cout << coins(n) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment