Codeforces Round #194 (Div. 1) / 333A Secrets(贪心)

A. Secrets
http://codeforces.com/problemset/problem/333/A
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.

One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?

The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.

Input

The single line contains a single integer n (1 ≤ n ≤ 1017).

Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cincout streams or the %I64dspecifier.

Output

In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.

Sample test(s)
input
1
output
1
input
4
output
2


题目是这个意思:

让面额总数大于n而又最接近n,且所用硬币总数还最多。

首先1分不能用,假设可以用,则有1+x>n,所以x>=n,如果x=n,则不符合题意,所以x>n,这说明x就能满足要求,所以1分是不能用的。

同理可以证明,当n=3k时,3分是不能用的;当n=9k时,9分是不能用的;……

所以判断n最多是3的多少次方的倍数就行。


完整代码:

/*30ms,0KB*/

#include

int main(void)
{
	__int64 n, deno = 3; //denomination
	scanf("%I64d", &n);
	for (; n % deno == 0; deno *= 3)
		;
	printf("%I64d", n / deno + 1);
	return 0;
}


你可能感兴趣的:(Codeforces,贪心)