CodeForces 333A Secrets

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.

题意:首先有一堆硬币,面值是3的n次方:1,3,9,27……,然后给你一个数n,让你用硬币去换,条件是:

你用硬币要尽可能的大于n并且最接近n;

你用的硬币数量要尽可能的多;

问要多少个硬币换取。

题解:首先1面值的不能用,然后找到第一个不能整除3的面值,用除数+1就是结果。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
int main()
{
    long long int n,t;
    long long int ans;
    while(~scanf("%lld",&n))
    {
        if(n%3!=0)
        {
            ans=n/3+1;
        }
        else if(n%3==0)
        {
            t=3*3;
            while(t)
            {
                if(t==n)
                {
                    ans=1;
                    break;
                }
                else if(n%t!=0)
                {
                    ans=n/t+1;
                    break;
                }
                else
                t=t*3;
            }
        }
        printf("%lld\n",ans);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值