codechef - Bytelandian gold coins 题解

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?

Input

The input will contain several test cases (not more than 10). Each

testcase is a single line with a number n, 0 <= n <= 1 000 000 000.

It is the number written on your coin.

Output

For each test case output a single line, containing the maximum amount

of American dollars you can make.

Example

Input:
12
2

Output:
13
2

本题使用动态规划法, 或者记忆法,加上递归法。
不加上递归法,好像很麻烦,因为我们不知道其初始值,只知道其最终值,所以只能往下递推了,这样使用记忆法就比动态规划要方便了。
使用二维表设计其递归记忆表,防止重复计算。还是十分困难的,动态规划法有时候不一定比记忆法要好。
递归记忆法的学名: top-down with memoization; Introduction to Algorithm的Dynamic programming 这章有介绍

#include <stdio.h>
#include <vector>
#include <cmath>
using namespace std;

class Bytelandiangoldcoins
{
        long long calMax(vector<vector<long long> > &tbl, int n, int r, int c)
	{
		if (n < 12) return n;
		if (tbl[r][c] == 0)
		{
			tbl[r][c] = calMax(tbl, n>>1, r+1, c) + calMax(tbl, n>>2, r+2, c)
				+ calMax(tbl, n/3, r, c+1);
		}
		return tbl[r][c];
	}
public:
	Bytelandiangoldcoins()
	{
		int r = (int)ceil(log(1E9)/log(2.0));
		int c = (int)ceil(log(1E9)/log(3.0));
		int n;
		while (scanf("%d", &n) != EOF)
		{
			vector<vector<long long> > tbl(r, vector<long long>(c, 0));
			printf("%lld\n", calMax(tbl, n, 0, 0));
		}
	}
};




codechef - Bytelandian gold coins 题解,布布扣,bubuko.com

时间: 2024-10-14 19:39:24

codechef - Bytelandian gold coins 题解的相关文章

SPOJ Problem 346:Bytelandian gold coins

有一种价值n的硬币能换成n/2,n/3,n/4的三个硬币,要求硬币价值的和尽可能多. 前十万打表,后面就BFS... #include<cstdio> #include<cstring> int a[100005]; int q[100005],l,r,t,i,n; long long ans; int main(){ for (i=0;i<=100000;i++){ a[i]=a[i/2]+a[i/3]+a[i/4]; if (i>a[i])a[i]=i; } whi

Baskets of Gold Coins

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1862    Accepted Submission(s): 1108 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

hdoj 2401 Baskets of Gold Coins

Baskets of Gold Coins Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 1104 Problem Description You are given N baskets of gold coins. The baskets are numbered fro

HDOJ(HDU) 2401 Baskets of Gold Coins(数列、)

Problem Description You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the

hust 1170 - Baskets of Gold Coins

题目描述 You are given N baskets of gold coins. The baskets are numbered from 1 to N. In all except one of the baskets, each gold coin weighs w grams. In the one exceptional basket, each gold coin weighs w-d grams. A wizard appears on the scene and takes

Codechef Maximum Weight Difference题解

Maximum Weight Difference Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams. Chef's son insists on helping his father in carrying the items. He wants hi

H - Gold Coins(2.4.1)

H - Gold Coins(2.4.1) Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Description The king pays his loyal knight in gold coins. On the first day of his service, the knight receives one gold coin. On each of

codechef Ciel and Receipt题解

Tomya is a girl. She loves Chef Ciel very much. Tomya like a positive integer p, and now she wants to get a receipt of Ciel's restaurant whose total price is exactly p. The current menus of Ciel's restaurant are shown the following table. Name of Men

codechef The Lead Game 题解

The game of billiards involves two players knocking 3 balls around on a green baize table. Well, there is more to it, but for our purposes this is sufficient. The game consists of several rounds and in each round both players obtain a score, based on