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

A. Secrets
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<cstdio>

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值